Builder模式
Builder模式帮你把一个不同类的大工程分开成不同的部分,最后在把这些合并起来
python的例子
制造一辆车的工序是很繁琐和浩大的,我们可以定一个车辆的描述
class Vehicle(object):
def __init__(self, type_name):
self.type = type_name
self.wheels = None
self.doors = None
self.seats = None
def view(self):
print(
"This vehicle is a " +
self.type +
" with; " +
str(self.wheels) +
" wheels, " +
str(self.doors) +
" doors, and " +
str(self.seats) +
" seats."
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
这里我要把制造齿轮,车门,座位的工序抽象出来,用不同的制造部门去做
class VehicleBuilder(object):
__metadata__ = abc.ABCMeta
@abc.abstractmethod
def make_wheels(self):
raise
@abc.abstractmethod
def make_doors(self):
raise
@abc.abstractmethod
def make_seats(self):
raise
然后设想我要制造汽车和自行车, 每个都要继承这个VehicleBuilder基类,但是齿轮,车门,座位都是不一样的
class CarBuilder(VehicleBuilder):
def __init__(self):
self.vehicle = Vehicle("Car ")
def make_wheels(self):
self.vehicle.wheels = 4
def make_doors(self):
self.vehicle.doors = 3
def make_seats(self):
self.vehicle.seats = 5
class BikeBuilder(VehicleBuilder):
def __init__(self):
self.vehicle = Vehicle("Bike")
def make_wheels(self):
self.vehicle.wheels = 2
def make_doors(self):
self.vehicle.doors = 0
def make_seats(self):
self.vehicle.seats = 2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
下面是最重要的环节:制造的Director
class VehicleManufacturer(object):
def __init__(self):
self.builder = None
def create(self):
assert not self.builder is None, "No defined builder"
self.builder.make_wheels()
self.builder.make_doors()
self.builder.make_seats()
return self.builder.vehicle
看起来是不是有点和以前的模式不好分辨? 主要下面的调用方式
if __name__ == "__main__":
manufacturer = VehicleManufacturer()
manufacturer.builder = CarBuilder()
car = manufacturer.create()
car.view()
manufacturer.builder = BikeBuilder()
bike = manufacturer.create()
bike.view()