建造者模式
定义:
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。具体点说,就是考虑用相同的原料制作出不同的东西出来。例如:快餐店里的食物都是那几样,汉堡、薯条、咖啡,牛奶等,但是能搭配出不同得套餐。
案例代码:
项目目录
|—builder-pattern
|—pom.xml
|—src
|—main
|—java
|—org
|—peko
|—design
|—pattern
|—Builder.java
|—fast_food
|—FastFood.java
|—impl
|—Burger.java
|—Chips.java
|—Coffee.java
|—Milk.java
|—impl
|—PackageImpl.java
|—Package.java
|—resources
|—test
|—java
|—org
|—peko
|—design
|—pattren
|—Test01.java
FastFood.java
package org.peko.design.pattern.fast_food;
import java.math.BigDecimal;
public interface FastFood {
String food();
BigDecimal price();
}
Burger.java
package org.peko.design.pattern.fast_food.impl;
import org.peko.design.pattern.fast_food.FastFood;
import java.math.BigDecimal;
public class Burger implements FastFood {
@Override
public String food() {
return "汉堡";
}
@Override
public BigDecimal price() {
return new BigDecimal("10");
}
}
Chips.java
package org.peko.design.pattern.fast_food.impl;
import org.peko.design.pattern.fast_food.FastFood;
import java.math.BigDecimal;
public class Chips implements FastFood {
@Override
public String food() {
return "薯条";
}
@Override
public BigDecimal price() {
return new BigDecimal("5");
}
}
Coffee.java
package org.peko.design.pattern.fast_food.impl;
import org.peko.design.pattern.fast_food.FastFood;
import java.math.BigDecimal;
public class Coffee implements FastFood {
@Override
public String food() {
return "咖啡";
}
@Override
public BigDecimal price() {
return new BigDecimal("4");
}
}
Milk.java
package org.peko.design.pattern.fast_food.impl;
import org.peko.design.pattern.fast_food.FastFood;
import java.math.BigDecimal;
public class Milk implements FastFood {
@Override
public String food() {
return "牛奶";
}
@Override
public BigDecimal price() {
return new BigDecimal("6");
}
}
Package.java
package org.peko.design.pattern;
import org.peko.design.pattern.fast_food.FastFood;
public interface Package {
Package addFood(FastFood fastFood);
Package addDrink(FastFood fastFood);
String getDetail();
}
PackageImpl.java
package org.peko.design.pattern.impl;
import org.peko.design.pattern.Package;
import org.peko.design.pattern.fast_food.FastFood;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class PackageImpl implements Package {
private final List<FastFood> list = new ArrayList<>();
private String packageName;
private BigDecimal cost = BigDecimal.ZERO;
private PackageImpl() {
}
public PackageImpl(String packageName){
this.packageName = packageName;
}
public Package addFood(FastFood fastFood){
this.list.add(fastFood);
this.cost = this.cost.add(fastFood.price());
return this;
}
public Package addDrink(FastFood fastFood){
this.list.add(fastFood);
this.cost = this.cost.add(fastFood.price());
return this;
}
@Override
public String getDetail() {
StringBuilder detail = new StringBuilder("\r\n-----------------------------------------\r\n" +
"套餐名称:"+ this.packageName + "\r\n" +
"套餐价格:"+ this.cost + "\r\n" +
"套餐内容:"+ "\r\n");
for(FastFood fastFood: list){
detail.append(" 主食:").append(fastFood.food()).append(" 价格:").append(fastFood.price()).append("\r\n");
}
return detail.toString();
}
}
Builder.java
package org.peko.design.pattern;
import org.peko.design.pattern.fast_food.impl.Burger;
import org.peko.design.pattern.fast_food.impl.Chips;
import org.peko.design.pattern.fast_food.impl.Coffee;
import org.peko.design.pattern.fast_food.impl.Milk;
import org.peko.design.pattern.impl.PackageImpl;
public class Builder {
public Package PackageOne(){
return new PackageImpl("套餐1")
.addFood(new Burger())
.addDrink(new Coffee());
}
public Package PackageTwo(){
return new PackageImpl("套餐2")
.addFood(new Chips())
.addDrink(new Milk());
}
}
Test01.java
package org.peko.design.pattren;
import org.junit.Test;
import org.peko.design.pattern.Builder;
public class Test01 {
@Test
public void test01(){
Builder builder = new Builder();
System.out.println(builder.PackageOne().getDetail());
System.out.println(builder.PackageTwo().getDetail());
}
}
运行结果
-----------------------------------------
套餐名称:套餐1
套餐价格:14
套餐内容:
主食:汉堡 价格:10
主食:咖啡 价格:4
-----------------------------------------
套餐名称:套餐2
套餐价格:11
套餐内容:
主食:薯条 价格:5
主食:鸡肉 价格:6
通过组合不同的对象,建造出不同的方案,这也是个组合优先于继承的案例