定 义:将一个复杂对象的构建于它的表示分离,使得同样的构建过程可以创建不同的表示
优 点:
1、封装性
2、建造者独立,容易扩充
3、便于控制细节风险
使用场景:
1、相同的方法,不同的执行顺序,产生不同的时间结果时,可以采用建造者模式
2、多个部件或零件,都可以装配到一个对象中,但是产生的运行结果又不相同时,则可以使用该模式
3、产品类非常复杂,或者产品类中的调用顺序不同产生了不同的效能,这个时候使用建造者模式非常适合
应用案例:
首先来看看建造者模式通用类图,建造类为抽象类,对于具体的实现用子类实现
建造者模式在现实生活中应用应该是很广泛的,举个例子来说,一个企业有很多生产线,有生产手机的各个零部件的,有些零部件是从外面采购进来的,然后通过一个组装部门进行组装后,就成了一部手机,对外而言只要调用这个组装部门类即可,例如老板说,我现在要生产一批大屏幕的类似于iPhone的手机,于是组装部门根据相关零件边组装一台机器。
想想这样设计有什么好处?市场部门不需要任何组装手机知识,我只要通知对应的组装部门,让其组装手机就行,这样设计方式有很好的扩展性能,如果今后公司又创新了Mini97手机,我只要新增一个子类,更改市场部信息即可。
下面来看看一个组装汽车的代码实现例子:
Public MustInherit Class CarBuilder Public m_CarModel As CarModel标签:Me,End,Sub,sequence,利剑,建造,._,设计模式,Public From: https://blog.51cto.com/u_15870687/5837382
'''
''' <param name="_sequence"></param>
Public MustOverride Sub setSequence(ByVal _sequence As ArrayList)
Public Overridable Function getCarModel() As CarModel
getCarModel = Nothing
End Function
End Class ' CarBuilder
Public Class BenzBuilder
Inherits CarBuilder
Private Benz As BenzModel = New BenzModel()
'''
''' <param name="_sequence"></param>
Public Overrides Sub setSequence(ByVal _sequence As ArrayList)
Me.Benz.setSequence(_sequence)
End Sub
Public Overrides Function getCarModel() As CarModel
Return Benz
End Function
End Class ' BenzBuilder
Public Class BMWBuilder
Inherits CarBuilder
Private bmw As BMWModel = New BMWModel()
'''
''' <param name="_sequence"></param>
Public Overrides Sub setSequence(ByVal _sequence As ArrayList)
bmw.setSequence(_sequence)
End Sub
Public Overrides Function getCarModel() As CarModel
Return bmw
End Function
End Class ' BMWBuilder
Public MustInherit Class CarModel Private _sequence As ArrayList = New ArrayList
Public MustOverride Sub start()
Public MustOverride Sub stop1()
Public MustOverride Sub alarm()
Public MustOverride Sub engineBoom()
Public Sub run()
For i As Integer = 0 To _sequence.Count - 1
Dim actionName As String = _sequence.Item(i).ToString()
If actionName = "start" Then
start()
ElseIf actionName = "stop" Then
stop1()
ElseIf actionName = "alarm" Then
alarm()
ElseIf actionName = "engineBoom" Then
engineBoom()
End If
Next
End Sub
'''
''' <param name="_sequence"></param>
Public Sub setSequence(ByVal __sequence As ArrayList)
_sequence = __sequence
End Sub
End Class ' CarModel
Public Class BenzModel
Inherits CarModel
Public Overrides Sub start()
Console.WriteLine("Benz 开动了")
End Sub
Public Overrides Sub stop1()
Console.WriteLine("Benz 停止了")
End Sub
Public Overrides Sub alarm()
Console.WriteLine("Benz 鸣喇叭")
End Sub
Public Overrides Sub engineBoom()
Console.WriteLine("Benz 引擎声音")
End Sub
End Class ' BenzModel
Public Class BMWModel
Inherits CarModel
Public Overrides Sub start()
Console.WriteLine("BMW 启动")
End Sub
Public Overrides Sub stop1()
Console.WriteLine("BMW 停止")
End Sub
Public Overrides Sub alarm()
Console.WriteLine("BMW 鸣笛")
End Sub
Public Overrides Sub engineBoom()
Console.WriteLine("引擎声音")
End Sub
End Class ' BMWModel
Public Class Director Public m_CarBuilder As CarBuilder
Private _sequence As ArrayList = New ArrayList()
Private _benzBulider As BenzBuilder = New BenzBuilder()
Private _bmwBulider As BMWBuilder = New BMWBuilder()
Public Function getABenzModel() As CarModel
Me._sequence.Clear()
Me._sequence.Add("start")
Me._sequence.Add("stop")
Me._benzBulider.setSequence(Me._sequence)
Return Me._benzBulider.getCarModel()
End Function
Public Function betBBenzModel() As CarModel
Me._sequence.Clear()
Me._sequence.Add("start")
Me._sequence.Add("engineBoom")
Me._sequence.Add("alarm")
Me._sequence.Add("stop")
Me._benzBulider.setSequence(Me._sequence)
Return Me._benzBulider.getCarModel()
End Function
Public Function getCBMWModel() As CarModel
Me._sequence.Clear()
Me._sequence.Add("start")
Me._sequence.Add("stop")
Me._bmwBulider.setSequence(Me._sequence)
Return Me._bmwBulider.getCarModel()
End Function
Public Function getDBMWModel() As CarModel
Me._sequence.Clear()
Me._sequence.Add("start")
Me._sequence.Add("engineBoom")
Me._sequence.Add("alarm")
Me._sequence.Add("stop")
Me._bmwBulider.setSequence(Me._sequence)
Return Me._bmwBulider.getCarModel()
End Function
End Class ' Director
Sub Main() Dim _director As Director = New Director
Console.WriteLine("第一辆车出场了:")
_director.getABenzModel().run()
Console.WriteLine("第二辆车出场了:")
_director.betBBenzModel().run()
Console.WriteLine("第三辆车出场了:")
_director.getCBMWModel().run()
Console.WriteLine("第四辆车出场了:")
_director.getDBMWModel().run()
Console.Read()
End Sub