抽象工厂(八) ◆ 实现 ■ Factories as singletons,每个product族往往只需 要一个factory,对象就可以了 Creating the products,对于product,族比较多的情 况,可以使用prototype模式来实现这些factories, 而不必对于每一个具有细微差别的product)族都使用 一个concrete factory class Defining extensible factories,Evaluation 中提到的缺点,通过参数化技术提高factory的适应 能力和扩展性 ·问题在于,返回给客户什么样的类型? Object Oriented Analysis and Design 16
Object Oriented Analysis and Design 16 w 实现 § Factories as singletons, 每个product族往往只需 要一个factory对象就可以了 § Creating the products, 对于product族比较多的情 况,可以使用prototype模式来实现这些factories, 而不必对于每一个具有细微差别的product族都使用 一个concrete factory class § Defining extensible factories,针对Evaluation 中提到的缺点,通过参数化技术提高factory的适应 能力和扩展性 • 问题在于,返回给客户什么样的类型? 抽象工厂(八)
Builder(一) ◆ 意图 将复杂对象的构建过程与它的表示相分离,使得相同的 ■ 构建过程可以创建不同的表示。 ◆ 动机 ■ 在复杂对象的构造过程中,允许同样的构造过程能够加 入新的被构造元素 ·“结构化构造过程” ◆ 适用性,在下列情况适用Builder模式: ■ the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled. the construction process must allow different representations for the object that's constructed. Object Oriented Analysis and Design 17
Object Oriented Analysis and Design 17 Builder (一) w 意图 § 将复杂对象的构建过程与它的表示相分离,使得相同的 构建过程可以创建不同的表示。 w 动机 § 在复杂对象的构造过程中,允许同样的构造过程能够加 入新的被构造元素 § “结构化构造过程” w 适用性,在下列情况适用 Builder 模式: § the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled. § the construction process must allow different representations for the object that's constructed
Builder(二) ◆结构 Director builder Buider Constructo BuildPart() For all objects in structure ConcreteBuilder Product builder->BuildP arto BuildP arto GetResult ◆ 参与者 ·Director、Builder、ConcreteBuilder、Product Object Oriented Analysis and Design 18
Object Oriented Analysis and Design 18 Builder (二) w 结构 w 参与者 § Director、 Builder、ConcreteBuilder、 Product
Builder(三) ◆协作 aClient aDirector aConcreteBuilder new ConcreteBuilder new Director(aConcreteBuilder) Construct() BuildPartA() BuildPartB() BuildPartC() GetResult() Object Oriented Analysis and Design 19
Object Oriented Analysis and Design 19 Builder (三) w 协作
Builder(四) ◆ 评估 It lets you vary a product's internal representation It isolates code for construction and representation It gives you finer control over the construction process ◆实现 Builder interface(Assembly and construction) Why no abstract class for products? Empty methods as default in Builder. ◆ 相关模式 Abstract Factory ·区别:()builder重在构造过程,最后一步返回结果; (2)builder构造许多复杂对象 Object Oriented Analysis and Design 20
Object Oriented Analysis and Design 20 Builder (四) w 评估 § It lets you vary a product's internal representation § It isolates code for construction and representation § It gives you finer control over the construction process w 实现 § Builder interface(Assembly and construction) § Why no abstract class for products? § Empty methods as default in Builder. w 相关模式 § Abstract Factory • 区别:(1) builder重在构造过程,最后一步返回结果; (2) builder构造许多复杂对象