标签:模式 factor 设计模式 print extend rac als abstract 更改
package DesignPattern;
public class AbstractFactory {
public static class Dough{}
public static class Sauce{}
public static class Veggies{}
public static class Cheese{}
public static class Pepperoni{}
public static class Clams{}
//披萨
public static abstract class Pizza{
String name;
Dough dough;
Sauce sauce;
Veggies[] veggies;
Cheese cheese;
Pepperoni pepperoni;
Clams clams;
abstract void prepare();
public void bake(){
System.out.println("Bake for 25 minutes at 350");
}
public void cut(){
System.out.println("Cutting the pizza into diagonal slices");
}
public void box(){
System.out.println("Place pizza in official PizzaStore box");
}
public void setName(String name){this.name=name;}
public String getName(){
return name;
}
}
public static class CheesePizza extends Pizza{
PizzaIngredientFactory ingredientFactory;
public CheesePizza(PizzaIngredientFactory pizzaIngredientFactory){
this.ingredientFactory=pizzaIngredientFactory;
}
void prepare(){
System.out.println("Preparing "+name);
dough=ingredientFactory.createDough();
sauce=ingredientFactory.createSauce();
cheese=ingredientFactory.createCheese();
}
}
public static class ClamPizza extends Pizza{
PizzaIngredientFactory ingredientFactory;
public ClamPizza(PizzaIngredientFactory pizzaIngredientFactory){
this.ingredientFactory=pizzaIngredientFactory;
}
void prepare(){
System.out.println("Preparing "+name);
dough=ingredientFactory.createDough();
sauce=ingredientFactory.createSauce();
cheese=ingredientFactory.createCheese();
}
}
//配方工厂
public interface PizzaIngredientFactory{
Dough createDough();
Sauce createSauce();
Cheese createCheese();
Veggies[] createVegies();
Pepperoni createPeperoni();
Clams createClams();
}
//纽约附近的原料
public static class ThinCrustDough extends Dough{}
public static class MarinaraSauce extends Sauce{}
public static class RegginaoCheese extends Cheese{}
public static class SlicedPepperoni extends Pepperoni{}
public static class FreshClams extends Clams{}
//蔬菜
public static class Gralic extends Veggies{}
public static class Onion extends Veggies{}
public static class Mushroom extends Veggies{}
public static class RedPeper extends Veggies{}
//纽约原料工厂
public static class NYPizzaIngredientFactory implements PizzaIngredientFactory{
@Override
public Dough createDough() {
return new ThinCrustDough();
}
@Override
public Sauce createSauce() {
return new MarinaraSauce();
}
@Override
public Cheese createCheese() {
return new RegginaoCheese();
}
@Override
public Veggies[] createVegies() {
Veggies[] veggies ={new Gralic(),new Onion(),new Mushroom(),new RedPeper()};
return veggies;
}
@Override
public Pepperoni createPeperoni() {
return new SlicedPepperoni();
}
@Override
public Clams createClams() {
return new FreshClams();
}
}
//披萨店
public static abstract class PizzaStore{
public Pizza orderPizza(String type){
Pizza pizza=createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
abstract Pizza createPizza(String type);
}
public static class NYPizzaStore extends PizzaStore{
@Override
Pizza createPizza(String type) {
Pizza pizza=null;
PizzaIngredientFactory pizzaIngredientFactory= new NYPizzaIngredientFactory();
if(type.equals("cheese")){
pizza = new CheesePizza(pizzaIngredientFactory);
pizza.setName("New York Style Cheese Pizza");
}//此处可添加其他类型的披萨
return pizza;
}
}
public static void main(String[] args) {
PizzaStore nyStore = new NYPizzaStore();
Pizza pizza=nyStore.orderPizza("cheese");
System.out.println(pizza.name);
}
/*
抽象工厂模式要点在工厂模式分离创建方法和加工流程的基础上,进一步降低原料和加工流程的耦合
使得在不同的地点,只需扩展某类原料实现,不用更改原有的原料代码,提高了代码的可重用性
或者说抽象工厂模式允许客户接口创建一组相关的产品(比如原料工厂创建一套原料的备料过程)
*/
}
标签:模式 factor 设计模式 print extend rac als abstract 更改
原文地址:https://www.cnblogs.com/zhouyu0-0/p/10724359.html