静态工厂模式,抽象工厂模式,建造者模式

静态工厂模式

ublic class FruitFactory {


	public static Fruit getFruit(String name) {
		Fruit f=null;
		switch (name)
		{
		case "APPLE":{
			f=new Apple();
		}
		case "BANANA":{
			f=new Banana();
		}
		default :{System.out.println("Unknown Fruit");}
		}
		return f;
		
	}
}

静态工厂模式,抽象工厂模式,建造者模式_第1张图片抽象工厂模式

静态工厂模式,抽象工厂模式,建造者模式_第2张图片 建造者模式


public class MealBuilder {
public Meal suMeal() {
	Meal m=new Meal();
	m.additeam(new SuBuger());
	m.additeam(new bing());
	return m;
}
	public Meal rouMeal() {
		Meal m=new Meal();
		m.additeam(new bing());
		m.additeam(new RouBuger());
		m.additeam(new RouBuger());
		m.additeam(new RouBuger());
		return m;
	}
	public Meal haohua(){
		Meal m=new Meal();
		m.additeam(new bing());
		m.additeam(new RouBuger());
		m.additeam(new RouBuger());
		m.additeam(new SuBuger());
		return m;
	}
}
public class Meal {
private ArrayList iteams=new ArrayList();
public void additeam(iteam i) {
	iteams.add(i);
}
public double getCost() {

	return iteams.stream().mapToDouble(iteam::price).sum();
}
public void showItems() {
	iteams.stream().forEach(
		e->{
			System.out.println(e.name());
			e.packing().pack();
			System.out.println(e.price());
		}
	);
}
}
public interface iteam {
String name();
Packing packing();
double price();
} 

public class Main {
    public static void main(String[] args) {
        MealBuilder builder=new MealBuilder();
        Meal haohua=builder.haohua();
        double cost=haohua.getCost();
        System.out.println("total"+cost);
        haohua.showItems();
    }
}
public abstract class ClodDrink  implements iteam{
	@Override
	public Packing packing() {
		return new Bottle();
	}
}
public interface Packing {
 void pack();
}

public class Wapper implements Packing{
	

	@Override
	public void pack() {
		// TODO Auto-generated method stub
		System.out.println("用纸包装");
	}

}
public class bing extends ClodDrink{

	@Override
	public String name() {
		// TODO Auto-generated method stub
		return "北冰洋";
	}

	@Override
	public double price() {
		return 3.0;
	}

}

total15.5
北冰洋
用瓶子包装
3.0
肉的
用纸包装
5.0
肉的
用纸包装
5.0
素的
用纸包装
2.5
 

你可能感兴趣的:(抽象工厂模式,建造者模式,java)