建造者模式 生成器模式 创建型 设计模式(五)
2018-11-20 03:19:24来源:博客园 阅读 ()
从抽象工厂演化
示例
代码
package builder; public interface MainBoard { String desc(); }
package builder; public class DellMainBoard implements MainBoard { @Override public String desc() { return "DELL mainBoard"; } }
package builder; public class AsusMainBoard implements MainBoard { @Override public String desc() { return "ASUS mainBoard"; } }
package builder; public interface DisplayDevice { String Desc(); }
package builder; public class DellDisplayDevice implements DisplayDevice { @Override public String Desc() { return "DELL display device"; } }
package builder; public class AsusDisplayDevice implements DisplayDevice { @Override public String Desc() { return "ASUS display device"; } }
package builder; public interface Creator { MainBoard createMainBoard(); DisplayDevice createDisplayDevice(); }
package builder; public class ConcreateCreatorDell implements Creator { @Override public MainBoard createMainBoard() { return new DellMainBoard(); } @Override public DisplayDevice createDisplayDevice() { return new DellDisplayDevice(); } }
package builder; public class ConcreateCreatorAsus implements Creator { @Override public MainBoard createMainBoard() { return new AsusMainBoard(); } @Override public DisplayDevice createDisplayDevice() { return new AsusDisplayDevice(); } }
package builder; public class Computer { private MainBoard mainBoard; private DisplayDevice displayDevice; public MainBoard getMainBoard() { return mainBoard; } public void setMainBoard(MainBoard mainBoard) { this.mainBoard = mainBoard; } public DisplayDevice getDisplayDevice() { return displayDevice; } public void setDisplayDevice(DisplayDevice displayDevice) { this.displayDevice = displayDevice; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Computer{"); sb.append("mainBoard=").append(mainBoard.desc()); sb.append(", displayDevice=").append(displayDevice.Desc()); sb.append('}'); return sb.toString(); } }
抽象工厂的问题
package builder; public class AssembleComputer { Creator creator; AssembleComputer(Creator creator){ this.creator = creator; } public Computer getComputer(){ Computer computer = new Computer(); MainBoard mainBoard = creator.createMainBoard(); DisplayDevice displayDevice = creator.createDisplayDevice(); computer.setMainBoard(mainBoard); computer.setDisplayDevice(displayDevice); return computer; } }
封装下的重构
package builder; public interface CreatorRefactor { void assembleMainBoard(); void assembleDisplayDevice(); Computer getComputer(); }
package builder; public class ConcreateCreatorDellRefactor implements CreatorRefactor { private Computer computer = new Computer(); @Override public void assembleMainBoard() { computer.setMainBoard(new DellMainBoard()); } @Override public void assembleDisplayDevice() { computer.setDisplayDevice(new DellDisplayDevice()); } @Override public Computer getComputer() { return computer; } }
package builder; public class ConcreateCreatorAsusRefactor implements CreatorRefactor { private Computer computer = new Computer(); @Override public void assembleMainBoard() { computer.setMainBoard(new AsusMainBoard()); } @Override public void assembleDisplayDevice() { computer.setDisplayDevice(new AsusDisplayDevice()); } @Override public Computer getComputer() { return computer; } }
package builder; public class AssembleComputerRefactor { CreatorRefactor creatorRefactor; AssembleComputerRefactor(CreatorRefactor creatorRefactor){ this.creatorRefactor = creatorRefactor; } public Computer getComputer(){ creatorRefactor.assembleMainBoard(); creatorRefactor.assembleDisplayDevice(); return creatorRefactor.getComputer(); } }
小结
意图
结构
给出一个抽象接口,以规范产品对象各个组成部分之间的构造
这个抽象的接口给出来构造一个产品的所有步骤以及最终产品的获取协议(就是其中定义的方法)(上面示例中的CreatorRefactor)
指挥产品的整个建造过程,不涉及具体产品的细节,只关注抽象建造者角色Builder定义的各个步骤的组织安排
代码示例
package buildPattern; public interface Builder { void buildPart1(); void buildPart2(); Product buildProduct(); }
package buildPattern; public class ConcreateBuilder implements Builder { private Product product = new Product(); @Override public void buildPart1() { //... } @Override public void buildPart2() { //... } @Override public Product buildProduct() { return product; } }
package buildPattern; public class Product { }
package buildPattern;
public class Director {
private Builder builder;
Director(Builder builder) {
this.builder = builder;
}
public Product getProduct() {
builder.buildPart1();
builder.buildPart2();
return builder.buildProduct();
}
}
注意事项
与抽象工厂对比
使用场景
简化形式
建造者与构造方法
package simplebuilder; /** * Created by noteless on 2018/10/17. * Description:假定有一个MyObject类,有7个属性前面两个v1 和 v2 是必选,其余可选 * * @author noteless */ public class MyObject { private int v1;//必选 private int v2;//必选 private int v3;//可选 private int v4;//可选 private int v5;//可选 private int v6;//可选 private int v7;//可选 private static class Builder { private int v1; private int v2; private int v3 = 0; private int v4 = 0; private int v5 = 0; private int v6 = 0; private int v7 = 0; public Builder(int v1, int v2) { this.v1 = v1; this.v2 = v2; } public Builder setV3(int v3) { this.v3 = v3; return this; } public Builder setV4(int v4) { this.v4 = v4; return this; } public Builder setV5(int v5) { this.v5 = v5; return this; } public Builder setV6(int v6) { this.v6 = v6; return this; } public Builder setV7(int v7) { this.v7 = v7; return this; } public MyObject build() { return new MyObject(this); } } private MyObject(Builder builder) { v1 = builder.v1; v2 = builder.v2; v3 = builder.v3; v4 = builder.v4; v5 = builder.v5; v6 = builder.v6; v7 = builder.v7; } @Override public String toString() { final StringBuilder sb = new StringBuilder("MyObject{"); sb.append("v1=").append(v1); sb.append(", v2=").append(v2); sb.append(", v3=").append(v3); sb.append(", v4=").append(v4); sb.append(", v5=").append(v5); sb.append(", v6=").append(v6); sb.append(", v7=").append(v7); sb.append('}'); return sb.toString(); } public static void main(String[] args) { MyObject my = new MyObject.Builder(1, 2). setV3(3).setV4(4).setV5(5).setV6(6).setV7(7).build(); System.out.println(my.toString()); } }
public MyObject build() {
return new MyObject(this);
}
总结
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 设计模式-委派/策略模式 2020-06-09
- 深入理解:设计模式中的七大设计原则 2020-06-07
- 设计模式---类之间的关系知多少 2020-06-07
- 你与面试官所了解的单例模式并不一样! 2020-06-06
- 高手眼中的观察者模式有什么不一样 2020-06-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash