定义:
建造者模式:将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
角色:
在这样的设计模式中,有以下几个角色:
- Builder:为创建一个产品对象的各个部件指定抽象接口。
- ConcreteBuilder:实现Builder的接口以构造和装配该产品的各个部件,定义并明确它所创建的表示,并提供一个检索产品的接口。
- Director:构造一个使用Builder接口的对象,指导构建过程。
- Product:表示被构造的复杂对象。ConcreteBuilder创建该产品的内部表示并定义它的装配过程,包含定义组成部件的类,包括将这些部件装配成最终产品的接口。
实例:
(不得不说自己画的UML还真是蠢,还是直接上代码吧……)
package Builder;
public interface Item {
String name();
int price();
Packing packing();
}
package Builder;
public abstract class Burger implements Item {
public abstract String name();
public abstract int price();
public Packing packing(){return new Wrapper();}
}
class Vegburger extends Burger{
public String name(){return "vegburger";}
public int price(){return 10;}
}
class Checkburger extends Burger{
public String name(){return "checkburger";}
public int price(){return 20;}
}
package Builder;
public abstract class Drink implements Item{
public abstract String name();
public abstract int price();
public Packing packing(){return new Buttle();}
}
class ColdDrink extends Drink{
public String name(){return "colddrink";}
public int price(){return 3;}
}
class HotDrink extends Drink{
public String name(){return "hotdrink";}
public int price(){return 6;}
}
package Builder;
public interface Packing {
String pack();
}
class Wrapper implements Packing{
public String pack(){return "wrapper";}
}
class Buttle implements Packing{
public String pack(){return "buttle";}
}
package Builder;
import java.util.ArrayList;
import java.util.List;
public class Meal {
List<Item> list = new ArrayList<Item>();
public void show(){
for(Item it:list){
System.out.print(it.name());
System.out.print("," +it.price());
System.out.print(","+it.packing().pack());
System.out.println();
}
}
}
package Builder;
public class MealBuilder {
static int totalcost;
public void vegmeal() throws InstantiationException, IllegalAccessException{
Meal meal = new Meal();
int cost=0;
meal.list.add(new Vegburger());
cost += Vegburger.class.newInstance().price();
meal.list.add(new Vegburger());
cost += Vegburger.class.newInstance().price();
meal.list.add(new HotDrink());
cost += HotDrink.class.newInstance().price();
System.out.println("show vegmeal");
meal.show();
System.out.println("vegmeal花了"+ cost);
System.out.println();
totalcost += cost;
}
public void nonvegmeal() throws InstantiationException, IllegalAccessException{
Meal meal = new Meal();
int cost=0;
meal.list.add(new Checkburger());
cost += Checkburger.class.newInstance().price();
meal.list.add(new Checkburger());
cost += Checkburger.class.newInstance().price();
meal.list.add(new ColdDrink());
cost += ColdDrink.class.newInstance().price();
System.out.println("show nonvegmeal");
meal.show();
System.out.println("nonvegmeal花了"+ cost);
System.out.println();
totalcost += cost;
}
}
package Builder;
public class Client {
public static void main(String[] args) throws InstantiationException, IllegalAccessException{
MealBuilder mb = new MealBuilder();
mb.vegmeal();
mb.vegmeal();
mb.nonvegmeal();
System.out.println("一共花了"+mb.totalcost +"元");
}
}
//输出结果:
show vegmeal
vegburger,10,wrapper
vegburger,10,wrapper
hotdrink,6,buttle
vegmeal花了26
show vegmeal
vegburger,10,wrapper
vegburger,10,wrapper
hotdrink,6,buttle
vegmeal花了26
show nonvegmeal
checkburger,20,wrapper
checkburger,20,wrapper
colddrink,3,buttle
nonvegmeal花了43
一共花了95元