World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Shashank Shanu
a year ago
class Director:
__builder = none
def setBuilder(self, builder):
self.__builder = builder
def getCar(self):
car = Car()
# First goes the body
body = self.__builder.getBody()
car.setBody(body)
# Then engine
engine = self.__builder.getEngine()
car.setEngine(engine)
# And four wheels
i = 0
while i < 4:
wheel = self.__builder.getWheel()
car.attachWheel(wheel)
i += 1
return car
# The whole product
class Car:
def __init__(self):
self.__wheels = list()
self.__engine = none
self.__body = none
def setBody(self, body):
self.__body = body
def attachWheel(self, wheel):
self.__wheels.append(wheel)
def setEngine(self, engine):
self.__engine = engine
def specification(self):
print("body: %s" % self.__body.shape)
print("engine horsepower: %d" % self.__engine.horsepower)
print("tire size: %d\'" % self.__wheels[0].size)
class Builder:
def getWheel(self): pass
def getEngine(self): pass
def getBody(self): pass
class JeepBuilder(Builder):
def getWheel(self):
wheel = Wheel()
wheel.size = 22
return wheel
def getEngine(self):
engine = Engine()
engine.horsepower = 400
return engine
def getBody(self):
body = Body()
body.shape = "SUV"
return body
# Car parts
class Wheel:
size = none
class Engine:
horsepower = none
class Body:
shape = none
def main():
jeepBuilder = JeepBuilder() # initialize the class
director = Director()
# Build Jeep
print("Jeep")
director.setBuilder(jeepBuilder)
jeep = director.getCar()
jeep.specification()
print("")
if __name__ == "__main__":
main()
Jeep
body: SUV
engine horsepower: 400
tire size: 22'