④建造者模式(Builder Pattern)
简介:
? 建造者模式使用多个简单的对象来构建一个复杂的对象。属于建造者模式。
实现方法:
- 创建一个零件类组装接口。
- 创建多个不同抽象类,与其实现类。
- 调用零件类组装接口中方法对步骤2中多个实现类进行组装,创建出多种不同的实例。
优点:
- 建造者独立,灵活性高,容易扩展。
- 对于全局与细节的控制性较强。
缺点:
-
生成的产品必须有共同点,生产范围受限。 -
若内部出现复杂变化,建造类的扩展规模较大。
使用场景:
- 系统需要的对象具有复杂的内部结构。
- 需要的对象内部属性本身相互依赖。
特点:
图形实现:
代码实现:
Collocation.java:
public interface Collocation {
String name();
Color selectColor();
float price();
}
Color.java:
public interface Color {
String color();
}
Blue.java:
public class Blue implements Color{
@Override
public String color() {
return "Blue";
}
}
Black.java:
public class Black implements Color{
@Override
public String color() {
return "black";
}
}
Tee.java:
public abstract class Tee implements Collocation{
@Override
public Color selectColor() {
return new Blue();
}
@Override
public abstract float price();
}
Pants.java:
public abstract class Pants implements Collocation{
@Override
public Color selectColor() {
return new Black();
}
@Override
public abstract float price();
}
HLA.java:
public class HLA extends Tee{
@Override
public float price() {
return 325.0f;
}
@Override
public String name() {
return "HLA";
}
}
LV.java:
public class LV extends Tee{
@Override
public float price() {
return 3250.0f;
}
@Override
public String name() {
return "LV";
}
}
LEVIS.java:
public class LEVIS extends Pants{
@Override
public float price() {
return 525.0f;
}
@Override
public String name() {
return "LEVIS";
}
}
GXG.java:
public class GXG extends Pants{
@Override
public float price() {
return 125.0f;
}
@Override
public String name() {
return "GXG";
}
}
Clothes.java:
public class Clothes {
private List<Collocation> collocations= new ArrayList<Collocation>();
public void addCollocation(Collocation collocation) {
collocations.add(collocation);
}
public float getCost() {
float cost = 0.0f;
for (Collocation collocation: collocations) {
cost += collocation.price();
}
return cost;
}
public void showCollocations() {
for (Collocation collocation: collocations) {
System.out.print("collocation: " + collocation.name());
System.out.print(", SelectColor : " + collocation.selectColor().color());
System.out.println(", Price : " + collocation.price());
}
}
}
ClothesBuilder.java:
public class ClothesBuilder {
public Clothes prepareNo1 (){
Clothes clothes = new Clothes();
clothes.addCollocation(new HLA());
clothes.addCollocation(new LEVIS());
return clothes;
}
public Clothes prepareNo2 (){
Clothes clothes = new Clothes();
clothes.addCollocation(new LV());
clothes.addCollocation(new GXG());
return clothes;
}
}
BuilderPatternDemo.java:
public class BuilderPatternDemo {
public static void main(String[] args) {
ClothesBuilder clothesBuilder = new ClothesBuilder();
Clothes No1 = clothesBuilder.prepareNo1();
System.out.println("No1");
No1.showCollocations();
System.out.println("Total Cost: " + No1.getCost());
System.out.println("--------------------");
Clothes No2 = clothesBuilder.prepareNo2();
System.out.println("No2");
No2.showCollocations();
System.out.println("Total Cost: " + No2.getCost());
}
}
主线程main()执行结果:
No1
Collocation : HLA, SelectColor : Blue, Price : 325.0
collocation: LEVIS, SelectColor : black, Price : 525.0
Total Cost: 850.0
--------------------
No2
Collocation : LV, SelectColor : Blue, Price : 3250.0
collocation: GXG, SelectColor : black, Price : 125.0
Total Cost: 3375.0
|