轻量级前端框架助力开发者提升项目效率与性能
574
2022-11-03
设计模式(12) -- 建造者模式
文章目录
需求
传统写法优缺点
建造者模式
介绍建 造者模式的四个角 色改进方案类图代码
类似建造者模式的jdk源码总结源码地址
需求
传统写法
AbstractHouse
public abstract class AbstractHouse { //打地基 public abstract void buildBasic(); //砌墙 public abstract void buildWalls(); //封顶 public abstract void roofed(); public void build() { buildBasic(); buildWalls(); roofed(); }}
CommonHouse
public class CommonHouse extends AbstractHouse { @Override public void buildBasic() { // TODO Auto-generated method stub System.out.println(" 普通房子打地基 "); } @Override public void buildWalls() { // TODO Auto-generated method stub System.out.println(" 普通房子砌墙 "); } @Override public void roofed() { // TODO Auto-generated method stub System.out.println(" 普通房子封顶 "); }}
测试
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub CommonHouse commonHouse = new CommonHouse(); commonHouse.build(); }}
优缺点
建造者模式
介绍
建 造者模式的四个角 色
改进方案类图
代码
House
//产品->Productpublic class House { private String baise; private String wall; private String roofed; public String getBaise() { return baise; } public void setBaise(String baise) { this.baise = baise; } public String getWall() { return wall; } public void setWall(String wall) { this.wall = wall; } public String getRoofed() { return roofed; } public void setRoofed(String roofed) { this.roofed = roofed; }}
HouseBuilder
// 抽象的建造者public abstract class HouseBuilder { protected House house = new House(); //将建造的流程写好, 抽象的方法 public abstract void buildBasic(); public abstract void buildWalls(); public abstract void roofed(); //建造房子好, 将产品(房子) 返回 public House buildHouse() { return house; }
CommonHouse
public class CommonHouse extends HouseBuilder { @Override public void buildBasic() { // TODO Auto-generated method stub System.out.println(" 普通房子打地基5米 "); } @Override public void buildWalls() { // TODO Auto-generated method stub System.out.println(" 普通房子砌墙10cm "); } @Override public void roofed() { // TODO Auto-generated method stub System.out.println(" 普通房子屋顶 "); }}
HighBuilding
public class HighBuilding extends HouseBuilder { @Override public void buildBasic() { // TODO Auto-generated method stub System.out.println(" 高楼的打地基100米 "); } @Override public void buildWalls() { // TODO Auto-generated method stub System.out.println(" 高楼的砌墙20cm "); } @Override public void roofed() { // TODO Auto-generated method stub System.out.println(" 高楼的透明屋顶 "); }}
HouseDirector
//指挥者,这里去指定制作流程,返回产品public class HouseDirector { HouseBuilder houseBuilder = null; //构造器传入 houseBuilder public HouseDirector(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //通过setter 传入 houseBuilder public void setHouseBuilder(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //如何处理建造房子的流程,交给指挥者 public House constructHouse() { houseBuilder.buildBasic(); houseBuilder.buildWalls(); houseBuilder.roofed(); return houseBuilder.buildHouse(); }}
测试
public class Client { public static void main(String[] args) { //盖普通房子 CommonHouse commonHouse = new CommonHouse(); //准备创建房子的指挥者 HouseDirector houseDirector = new HouseDirector(commonHouse); //完成盖房子,返回产品(普通房子) House house = houseDirector.constructHouse(); //System.out.println("输出流程"); System.out.println("--------------------------"); //盖高楼 HighBuilding highBuilding = new HighBuilding(); //重置建造者 houseDirector.setHouseBuilder(highBuilding); //完成盖房子,返回产品(高楼) houseDirector.constructHouse(); }}
类似建造者模式的jdk源码
建 造者模式在 JDK 的应用和源码分 析
总结
源码地址
https://github.com/hufanglei/java-design/tree/builder
完
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~