java的23种设计模式之建造者模式
设计模式遵循的几大原则
1:职责单一
2:接口隔离
3:依赖倒转
4:里氏替换
5:开闭原则
6:迪米特法则
设计模式的发展是依据以上原则而来,所以学习设计模式主要在于能够区分每个设计模式的共同点以及不同点,有些设计模式非常相似,只是侧重的点不同
学习他们需要学会总结,例如桥接模式和装饰模式
建造者模式:
首先了解什么样的场景下能够使用到建造者模式,现在有个需求,盖房子(经典的例子),现在假设盖房子需要的2个步骤,1:材料,2:大小
因为材料和大小的不一致,可以造出不同房型的房子(例如A房型:材料=【钢筋】, 大小=【大房子】; 例如B房型:材料=【砖块】, 大小=【小房子】)
问题分析:
造2个房型的房子,A房型和B房型,传统思想,创建一个House接口,让A房型实现House接口,让B房型也实现House接口就可以了。
但是从拓展的角度(偏向这个设计模式而言),现在我们还有其他房型的房子,例如 :材料=【钢筋】, 大小=【小房子】的C房型,从简单
设计上讲,我们可以再让C房型实现House接口,实际建造者强调的是建造过程(这里如果不用材料和大小更好解释:例如盖房子的过程:
打地基、铺水泥、贴瓷砖、盖房顶等,不同房子建造的过程不一样,有些毛坯可能不会贴瓷砖、有些不用打地基),最终我们是需要得到
这个房子,建造者模式侧重的产品和建造过程解耦
house类;
Materials材料接口:有钢铁、有砖块;
Style风格大小接口:有大房子、有小房子
产品总体UML图:
建造者总体UML图:
通过实现Builder(抽象建造者)接口,A房型和B房型的建造者创建出来了,Director(指挥者):通过指挥建造者创建合适的房子
总体UML图:
以下为代码:
House :
package com.yangmin.builder;
public class House {
private Materials materials;
private Style style;
public Materials getMaterials() {
return materials;
}
public void setMaterials(Materials materials) {
this.materials = materials;
}
public Style getStyle() {
return style;
}
public void setStyle(Style style) {
this.style = style;
}
@Override
public String toString() {
return "House [materials=" + materials.getMaterials() + ", style=" + style.getStyle() + "]";
}
}
Materials :
package com.yangmin.builder;
public interface Materials {
public String getMaterials();
}
Style :
package com.yangmin.builder;
public interface Style {
public String getStyle();
}
Steel :
package com.yangmin.builder;
public class Steel implements Materials {
@Override
public String getMaterials() {
return "钢筋";
}
}
Brick :
package com.yangmin.builder;
public class Brick implements Materials {
@Override
public String getMaterials() {
return "砖块";
}
}
Big:
package com.yangmin.builder;
public class Big implements Style {
@Override
public String getStyle() {
return "大房子";
}
}
Small :
package com.yangmin.builder;
public class Small implements Style {
@Override
public String getStyle() {
return "小房子";
}
}
Builder :
package com.yangmin.builder;
public interface Builder {
public void makeMater();
public void makeStyle();
public House getHouse();
}
HouseABuiler :
package com.yangmin.builder;
public class HouseABuiler implements Builder {
private House house;
public HouseABuiler(House house) {
super();
this.house = house;
}
@Override
public void makeMater() {
house.setMaterials(new Steel());
}
@Override
public void makeStyle() {
house.setStyle(new Big());
}
@Override
public House getHouse() {
return this.house;
}
}
HouseBBuilder :
package com.yangmin.builder;
public class HouseBBuilder implements Builder {
private House house;
public HouseBBuilder(House house) {
super();
this.house = house;
}
@Override
public void makeMater() {
house.setMaterials(new Brick());
}
@Override
public void makeStyle() {
house.setStyle(new Small());
}
@Override
public House getHouse() {
return this.house;
}
}
Director :
package com.yangmin.builder;
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public House houseBuilder() {
builder.makeMater();
builder.makeStyle();
return builder.getHouse();
}
}
Client :
package com.yangmin.builder;
public class Client {
public static void main(String[] args) {
//户型A
Director director = new Director(new HouseABuiler(new House()));
House house = director.houseBuilder();
System.out.println("户型A:"+house);
//户型B
Director directorB = new Director(new HouseBBuilder(new House()));
House houseB = directorB.houseBuilder();
System.out.println("户型B:"+houseB);
}
}
结果输出:
户型A:House [materials=钢筋, style=大房子]
户型B:House [materials=砖块, style=小房子]