工厂模式代码及类图

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

抽象工厂类图:

代码:

public interface Sauce {

}

public class MarinaraSauce implements Sauce{

public MarinaraSauce(){

System.out.println("Add MarinaraSauce...");

}

} public class PlumTomatoSauce implements Sauce{

public PlumTomatoSauce(){

System.out.println("Add PlumTomatoSauce...");

}

}

public interface Dough {

}

public class ThickCrustDough implements Dough{

public ThickCrustDough(){

System.out.println("Add ThickCrustDough...");

}

}

public class ThinCrustDough implements Dough{

public ThinCrustDough(){

System.out.println("Add ThinCrustDough...");

}

}

public interface Veggies {

}

public class Onion implements Veggies{ public Onion(){

System.out.println("Add Onion...");

}

}

public class Mushroom implements Veggies{

public Mushroom(){

System.out.println("Add Mushroom...");

}

}

public class Garlic implements Veggies{

public Garlic(){

System.out.println("Add Garlic...");

}

}

public class Eggplant implements Veggies{

public Eggplant(){

System.out.println("Add Eggplant...");

}

}

public class BlackOlives implements Veggies{

public BlackOlives(){

System.out.println("Add BlackOlives...");

}

}

public abstract class Pizza {

String name;

Dough dough;

Sauce sauce;

Veggies veggies[];

abstract void prepare();

void bake(){

System.out.println("Bake for 25 minutes at 350");

}

void cut(){

System.out.println("Cutting the pizza into diagonal slices");

}

void box(){

System.out.println("Place pizza in official PizzaStore box");

}

void setName(String name){

=name;

}

String getname(){

return name;

}

}

public interface PizzaIngredientFactory {

public Dough createDough();

public Sauce createSauce();

public Veggies[] createVeggies();

}

public class NYPizzaIngredientFactory implements PizzaIngredientFactory{ public Dough createDough(){

return new ThinCrustDough();

}

public Sauce createSauce(){

return new MarinaraSauce();

}

public Veggies[] createVeggies(){

Veggies veggies[]={new Garlic(),new Onion(),new Mushroom()};

return veggies;

}

}

public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory{ public Dough createDough(){

return new ThickCrustDough();

}

public Sauce createSauce(){

return new PlumTomatoSauce();

}

public Veggies[] createVeggies(){

Veggies veggies[]={new BlackOlives(),new Eggplant()};

return veggies;

}

}

public class VeggiesPizza extends Pizza{

PizzaIngredientFactory ingredientFactory;

public VeggiesPizza(PizzaIngredientFactory ingredientFactory){ this.ingredientFactory=ingredientFactory;

}

void prepare(){

System.out.println("Preparing "+name);

dough=ingredientFactory.createDough();

sauce=ingredientFactory.createSauce();

veggies=ingredientFactory.createVeggies();

}

}

public class CheesePizza extends Pizza{

PizzaIngredientFactory ingredientFactory;

public CheesePizza(PizzaIngredientFactory ingredientFactory){ this.ingredientFactory=ingredientFactory;

}

void prepare(){

System.out.println("Preparing "+name);

dough=ingredientFactory.createDough();

sauce=ingredientFactory.createSauce();

}

相关文档
最新文档