设计模式(五):Builder模式
来源:互联网 发布:刷机后还能恢复数据吗 编辑:程序博客网 时间:2024/06/16 09:39
Builder模式主要解决的问题是:
分离复杂对象的构建过程(construction)和表示(representation),以使得相同的构建过程能够产生不同的表示。通常Builder模式会和Composite模式一起使用。
![](http://upload.wikimedia.org/wikipedia/en/6/6e/Builder2.png)
图中:
Builder是创建产品的抽象接口
ConcreteBuilder是Builder的具体实现,由它负责构建或者组装产品。
Director的职责就是控制对象创建过程,它接受一个ConcreteBuilder作为参数,并对其进行相应的操作。
Product就是被构建的复杂的对象。
就拿制作比萨饼为例,为了满足不同消费者的口味,比萨饼的用料,风味都是不同的,当时比萨饼的制造过程却是相同的。利用Builder模式提供的线索,我们可以认为比萨饼(Pizza)本身就是一个复杂对象,并且需要我们解决复杂对象的构建过程(制作比萨饼)和表示(不同用料,风味)进行分离,以便于相同的构建过程(制作比萨饼)能够产生出不同的表示(不同的用料,风味)。
在这里我们又清晰的看到了面向对象设计原则中的一条原则:“找出应用中变化的部分,并将它与不变的部分进行隔离”。
在此,制作比萨饼的过程是没有发生变化的,所以我们将它分离出来形成了Director,该类中的Constuct方法即为制作比萨饼的过程,这一过程是稳定的。
而对于比萨饼的用料和风味是变化的,这里有HawaiianPizza和SpicyPizza,以后还会随着比萨饼店的生意越来越好而推出更多不同用料和风味的比萨饼。所以ConcreteBuilder是可以有多种的。
下面就来看java示例:
/** "Product" */
class Pizza {
private String dough = "";
private String sauce = "";
private String topping = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void setDough (String dough) { this.dough = dough; }
public void setSauce (String sauce) { this.sauce = sauce; }
public void setTopping (String topping) { this.topping = topping; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/** "Abstract Builder" */
abstract class PizzaBuilder {
protected Pizza pizza;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Pizza getPizza() { return pizza; }
public void createNewPizzaProduct() { pizza = new Pizza(); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public abstract void buildDough();
public abstract void buildSauce();
public abstract void buildTopping();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/** "ConcreteBuilder" */
class HawaiianPizzaBuilder extends PizzaBuilder {
public void buildDough() { pizza.setDough("cross"); }
public void buildSauce() { pizza.setSauce("mild"); }
public void buildTopping() { pizza.setTopping("ham+pineapple"); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/** "ConcreteBuilder" */
class SpicyPizzaBuilder extends PizzaBuilder {
public void buildDough() { pizza.setDough("pan baked"); }
public void buildSauce() { pizza.setSauce("hot"); }
public void buildTopping() { pizza.setTopping("pepperoni+salami"); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/** "Director" */
class Waiter {
private PizzaBuilder pizzaBuilder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void setPizzaBuilder (PizzaBuilder pb) { pizzaBuilder = pb; }
public Pizza getPizza() { return pizzaBuilder.getPizza(); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void constructPizza() {
pizzaBuilder.createNewPizzaProduct();
pizzaBuilder.buildDough();
pizzaBuilder.buildSauce();
pizzaBuilder.buildTopping();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/** A customer ordering a pizza. */
class BuilderExample {
public static void main(String[] args) {
Waiter waiter = new Waiter();
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
waiter.setPizzaBuilder ( hawaiianPizzaBuilder );
waiter.constructPizza();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Pizza pizza = waiter.getPizza();
}
}
分离复杂对象的构建过程(construction)和表示(representation),以使得相同的构建过程能够产生不同的表示。通常Builder模式会和Composite模式一起使用。
![](http://upload.wikimedia.org/wikipedia/en/6/6e/Builder2.png)
图中:
Builder是创建产品的抽象接口
ConcreteBuilder是Builder的具体实现,由它负责构建或者组装产品。
Director的职责就是控制对象创建过程,它接受一个ConcreteBuilder作为参数,并对其进行相应的操作。
Product就是被构建的复杂的对象。
就拿制作比萨饼为例,为了满足不同消费者的口味,比萨饼的用料,风味都是不同的,当时比萨饼的制造过程却是相同的。利用Builder模式提供的线索,我们可以认为比萨饼(Pizza)本身就是一个复杂对象,并且需要我们解决复杂对象的构建过程(制作比萨饼)和表示(不同用料,风味)进行分离,以便于相同的构建过程(制作比萨饼)能够产生出不同的表示(不同的用料,风味)。
在这里我们又清晰的看到了面向对象设计原则中的一条原则:“找出应用中变化的部分,并将它与不变的部分进行隔离”。
在此,制作比萨饼的过程是没有发生变化的,所以我们将它分离出来形成了Director,该类中的Constuct方法即为制作比萨饼的过程,这一过程是稳定的。
而对于比萨饼的用料和风味是变化的,这里有HawaiianPizza和SpicyPizza,以后还会随着比萨饼店的生意越来越好而推出更多不同用料和风味的比萨饼。所以ConcreteBuilder是可以有多种的。
下面就来看java示例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
- 设计模式(五):Builder模式
- 设计模式(五):Builder模式
- 设计模式(五)创建者模式(Builder)-创建型
- 设计模式(五)创建者模式(Builder)
- java设计模式(五)--建造者模式(Builder)
- 设计模式(五)之Builder - 建造者模式
- Java设计模式(五):建造者模式Builder
- 设计模式(Builder模式)
- 五、建造(Builder)模式
- Builder设计模式(生成器设计模式)
- 设计模式:Builder模式
- 设计模式 -- BUILDER模式
- 【设计模式】Builder模式
- 设计模式-Builder模式
- 设计模式Builder模式
- 设计模式-Builder模式
- 设计模式 - Builder模式
- 设计模式 - Builder模式
- 较好的重新启动数据库的步骤
- 使用DatabaseMetaData显示数据库的综合信息;
- 使用ResultSetMetaData显示字段的名字等信息
- 测试
- java中事务的处理
- 设计模式(五):Builder模式
- 创建并使用CallableStatement对象;
- struts中validation.xml模版
- Book-SQLSERVER数据库脚本
- 顶尖黑客榜
- 博客是一种新的履历
- JDBC常用类和方法
- 苹果电池问题比想像中严重 被质疑隐瞒真相
- 大阪第44天——也说谷歌和搜狗输入法