设计模式4 - 构建者模式Builder Design Pattern
来源:互联网 发布:卖家加入淘宝客的条件 编辑:程序博客网 时间:2024/04/19 01:35
1 Introduction
Builder pattern is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.
Complex Object Construction
For example, you can consider construction of a home. Home is the final end product (object) that is to be returned as the output of the construction process. It will have many steps, like basement construction, wall construction and so on roof construction. Finally the whole home object is returned. Here using the same process you can build houses with different properties.
GOF says,
“Separate the construction of a complex object from its representation so that the same construction process can create different representations” [GoF 94]
What is the difference between abstract factory and builder pattern?
Abstract factory may also be used to construct a complex object, then what is the difference with builder pattern? In builder pattern emphasis is on ‘step by step’. Builder pattern will have many number of small steps. Those every steps will have small units of logic enclosed in it. There will also be a sequence involved. It will start from step 1 and will go on upto step n and the final step is returning the object. In these steps, every step will add some value in construction of the object. That is you can imagine that the object grows stage by stage. Builder will return the object in last step. But in abstract factory how complex the built object might be, it will not have step by step object construction.2 Sample Java Source Code for Builder Pattern
Following is the interface, that will be returned as the product from the builder.
public interface HousePlan { public void setBasement(String basement); public void setStructure(String structure); public void setRoof(String roof); public void setInterior(String interior);}
Concrete class for the above interface. The builder constructs an implementation for the following class.
public class House implements HousePlan { private String basement; private String structure; private String roof; private String interior; public void setBasement(String basement) { this.basement = basement; } public void setStructure(String structure) { this.structure = structure; } public void setRoof(String roof) { this.roof = roof; } public void setInterior(String interior) { this.interior = interior; }}
Builder interface. We will have multiple different implementation of this interface in order to facilitate, the same construction process to create different representations.
public interface HouseBuilder { public void buildBasement(); public void buildStructure(); public void buildRoof(); public void buildInterior(); public House getHouse();}
First implementation of a builder.
public class IglooHouseBuilder implements HouseBuilder { private House house; public IglooHouseBuilder() { this.house = new House(); } public void buildBasement() { house.setBasement("Ice Bars"); } public void buildStructure() { house.setStructure("Ice Blocks"); } public void buildRoof() { house.setRoof("Ice Dome"); } public void buildInterior() { house.setInterior("Ice Carings"); } public House getHouse() { return this.house; } }
public class TipiHouseBuilder implements HouseBuilder { private House house; public TipiHouseBuilder() { this.house = new House(); } public void buildBasement() { house.setBasement("Wooden Poles"); } public void buildStructure() { house.setStructure("Wood and Ice"); } public void buildRoof() { house.setRoof("Wood, caribou and seal skins"); } public void buildInterior() { house.setInterior("Fire Wood"); } public House getHouse() { return this.house; }}
Following class constructs the house and most importantly, this maintains the building sequence of object.
public class CivilEngineer { private HouseBuilder houseBuilder; public CivilEngineer(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } public House getHouse() { return this.houseBuilder.getHouse(); } public void constructHouse() { this.houseBuilder.buildBasement(); this.houseBuilder.buildStructure(); this.houseBuilder.buildRoof(); this.houseBuilder.buildInterior(); }}
Testing the sample builder design pattern.
public class BuilderSample { public static void main(String[] args) { HouseBuilder iglooBuilder = new IglooHouseBuilder(); CivilEngineer engineer = new CivilEngineer(iglooBuilder); engineer.constructHouse(); House house = engineer.getHouse(); System.out.println("Builder constructed: " + house); }}
- 设计模式4 - 构建者模式Builder Design Pattern
- design pattern Builder 建造者设计模式
- Design Pattern: Builder 模式
- 生成器设计模式(Builder Design Pattern)
- 思维盛宴之设计模式-构建者模式Builder Pattern
- 浅谈 java 设计模式--构建模式(Builder pattern)
- 设计模式—构建器模式(Builder Pattern)
- 设计模式----建造者模式(Builder Pattern)
- 设计模式---建造者模式(Builder Pattern)
- 设计模式【建造者模式Builder Pattern】
- Design pattern设计模式
- Design Pattern, 设计模式
- 设计模式(Design Pattern)
- Design Pattern: Builder Pattern(建造者模式)
- 设计模式之构建者Builder模式
- 设计模式之Builder构建者模式
- 【Design Pattern】Builder模式的一种实现
- 设计模式(Design Pattern)
- linux磁盘管理命令之dd命令讲解
- linux 性能监控命令5——w,who,whoami,whois
- linux系统/var/log目录下的信息详解
- 出现bad interpreter:No such file or directory的原因+ 编译信息重定向问题
- VC 修改窗口属性 GetWindowLong SetWindowLong
- 设计模式4 - 构建者模式Builder Design Pattern
- 动态区间最大值最小值区间和查询,支持区间设置,线段树
- LIBPCAP
- 突破linux内核模块验证
- C# String.Format格式说明
- 一, 监控缓冲区的命中率
- NEFU要崛起——第1场 A - Theatre Square
- 安装VirtualBox虚拟网卡并任意修改其物理地址(MAC)
- PhotoShop与css3的结缘,自动生成css3样式的ps插件css3ps