java设计模式【创建模式】之建造(Bulider)模式
来源:互联网 发布:剑灵天族捏脸数据大全 编辑:程序博客网 时间:2024/06/06 07:28
Builder模式:分步骤构建一个复杂对象,实现复杂对象的创建过程和对象的表示相分离.
不管在软件系统还是其他领域,有时候会遇到 一个复杂的工程或者对象的创建,而这个创建可能有各个子模块或对象用一定的逻辑或者算法组成,由于各种原因这个复杂工程或对象各个部分可能需要经常的更新或变化,但是整个组合体或者说这个工程或对象要保证足够的稳定性.【不会因为子模块变化造成对整个工程巨大影响】 --- this is Bulider's Application environment;
以车为例吧
假设
车 = 轮子 + 外壳 + 发动机
代码实现:
//车的部件生产和组装规范
public interface Builder {
void makeWheel();
void makeShell();
void makeEngine();
Car buildCar();
}
//车
public class Car {
public Component getWheel() {
return wheel;
}
public void setWheel(Component wheel) {
this.wheel = wheel;
}
public Component getShell() {
return shell;
}
public void setShell(Component shell) {
this.shell = shell;
}
public Component getEngine() {
return engine;
}
public void setEngine(Component engine) {
this.engine = engine;
}
private Component wheel;
private Component shell;
private Component engine;
public String toString(){return wheel.getColor()+":"+engine.getColor()+":"+shell.getColor();}
}
//车的组成部件
public class Component {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
//负责车的组装
public class CarFactory {
private Builder builder;
CarFactory(Builder builder){
this.builder = builder;
}
public void buildCar(){
builder.makeWheel();
builder.makeEngine();
builder.makeShell();
}
}
//负责宝马车的生产
public class BMWFactory implements Builder {
Component wheel,shell,engine;
public void makeWheel() {
wheel = new Component();
wheel.setColor("red");
}
public void makeShell() {
shell = new Component();
shell.setColor("magnificent");
}
public void makeEngine() {
engine = new Component();
engine.setColor("BMW");
}
public Car buildCar() {
Car bmw = new Car();
bmw.setEngine(engine);
bmw.setShell(shell);
bmw.setWheel(wheel);
return bmw;
}
}
//负责奥迪车的生产
public class AudiFactory implements Builder{
Component wheel,shell,engine;
public void makeWheel() {
wheel = new Component();
wheel.setColor("blue");
}
public void makeShell() {
shell = new Component();
shell.setColor("changpeng");
}
public void makeEngine() {
engine = new Component();
engine.setColor("dongfeng");
}
public Car buildCar() {
Car audi = new Car();
audi.setEngine(engine);
audi.setShell(shell);
audi.setWheel(wheel);
return audi;
}
}
//来买车吧
public class CarShop {
private static Car get(Builder builder){
CarFactory factory = new CarFactory(builder);
factory.buildCar();
return builder.buildCar();
}
/**
* @param args
*/
public static void main(String[] args) {
AudiFactory audiFactory = new AudiFactory();
BMWFactory bmwFactory = new BMWFactory();
Car audi = get(audiFactory);
System.out.println(audi);
Car bmw = get(bmwFactory);
System.out.println(bmw);
}
}
这样不管车的部件不管变成什么样,你可以修改make***的内部逻辑,不会影响到 buildCar ..make***和buildCar 是分离的,
最后总能保证车的稳定生产.
- java设计模式【创建模式】之建造(Bulider)模式
- 设计模式之bulider------java
- 设计模式 创建模式之建造模式
- JAVA设计模式创建型模式之建造模式
- java 设计模式-创建模式之建造者模式
- Java设计模式之创建型模式--建造者模式
- 设计模式--创建模式--建造模式--java
- 设计模式----bulider
- 【创建型模式】建造者模式(Builder)之23种java设计模式
- Java 设计模式之建造者模式的详解(创建模式)
- Java设计模式之创建型模式-建造者模式(Builder)
- java 设计模式之建造者模式(Builder)模式
- 创建型:设计模式之建造模式(四)
- java设计模式之建造者模式
- java设计模式之建造者模式
- java设计模式之建造者模式
- 【Java设计模式】之建造者模式
- java设计模式笔记之建造模式
- Dotproject 测试地址
- 大整数加法
- malloc/free与new/delete的区别
- malloc()、free()、calloc() 與 realloc()
- gwt2.0国际化
- java设计模式【创建模式】之建造(Bulider)模式
- VC++中在对话框中动态显示图片
- :java continue的应用
- BW标准数据源
- java之applet
- IT巨头风格
- TextField的BUG
- Oprofile在Android中的应用
- PHP中利用pcntl实现多进程(模拟多线程)实例