抽象工厂模式读书笔记

提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。抽象工厂允许客户使用抽象的接口来创建一组相关产品,而不需要关心具体实际产出的产品是什么。
总结 :[list=1]
[*]所有工厂都是用来封装对象的创建。
[*] 简单工厂,虽然不是真正的设计模式,但是仍然不失为一个简单的方法,可以把客户程序从具体类解耦。
[*]工厂方法使用继承:把对象的创建委托给子类,子类实现工厂方法来创建对象。
[*]抽象工厂使用对象组合:对象的创建被实现在工厂接口所暴露出来的方法 中。
[*]所有工厂模式都是通过减少应用程序和具体类之间的依赖关系促进松耦合。
[*]工厂方法允许类将实例化延迟到子类进行。
[*]抽象工厂创建相关的家族,而不需要依赖他们的具体类。
[*]依赖倒置原则,指导我们避免依赖具体类型,而要尽量依赖抽象。
[*]工厂是很有威力的技巧,帮助我们针对抽象编成,而不是针对具体类编成。
[/list]

[img]http://wangpx.iteye.com/topics/download/4455feff-2221-3c4e-9a84-90e993df06c8[/img]

package pattern;

import java.util.ArrayList;

public abstract class PizzaStore {
SimplePizzaFactory factory;

public PizzaStore() {
}

public PizzaStore(SimplePizzaFactory factory) {
this.factory = factory;
}

public Pizza orderPizza(String type) {
Pizza pizza;
pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}

abstract Pizza createPizza(String type);
}

class NYPizzaStore extends PizzaStore {
Pizza createPizza(String item) {
Pizza pizza=null;
PizzaIngredientFactory ingredientFactory=new NYPizzaIngredientFctory();
if (item.equals("cheese")) {
pizza= new CheesePizza(ingredientFactory);
pizza.setName("New York Style Cheese Pizza");
} else if (item.equals("pepperoni")) {
pizza=new PepperoniPizza(ingredientFactory);
pizza.setName("New York Style pepperoni Pizza");
} else if (item.equals("clam")) {
pizza=new ClamPizza(ingredientFactory);
pizza.setName("New York Style Clame Pizza");
} else if (item.equals("veggie")) {
pizza=new VeggiePizza(ingredientFactory);
pizza.setName("New York Style Veggie Pizza");
}
return pizza;
}
}

class ChicagoPizzaStore extends PizzaStore {
Pizza createPizza(String item) {
Pizza pizza=null;
PizzaIngredientFactory ingredientFactory=new NYPizzaIngredientFctory();
if (item.equals("cheese")) {
pizza= new CheesePizza(ingredientFactory);
pizza.setName("New York Style Cheese Pizza");
} else if (item.equals("pepperoni")) {
pizza=new PepperoniPizza(ingredientFactory);
pizza.setName("New York Style pepperoni Pizza");
} else if (item.equals("clam")) {
pizza=new ClamPizza(ingredientFactory);
pizza.setName("New York Style Clame Pizza");
} else if (item.equals("veggie")) {
pizza=new VeggiePizza(ingredientFactory);
pizza.setName("New York Style Veggie Pizza");
}
return pizza;
}
}

class CaliforniaPizzaStore extends PizzaStore {
Pizza createPizza(String item) {
Pizza pizza=null;
PizzaIngredientFactory ingredientFactory=new NYPizzaIngredientFctory();
if (item.equals("cheese")) {
pizza= new CheesePizza(ingredientFactory);
pizza.setName("New York Style Cheese Pizza");
} else if (item.equals("pepperoni")) {
pizza=new PepperoniPizza(ingredientFactory);
pizza.setName("New York Style pepperoni Pizza");
} else if (item.equals("clam")) {
pizza=new ClamPizza(ingredientFactory);
pizza.setName("New York Style Clame Pizza");
} else if (item.equals("veggie")) {
pizza=new VeggiePizza(ingredientFactory);
pizza.setName("New York Style Veggie Pizza");
}
return pizza;
}
}

abstract class Pizza {
String name;
Dough dough;
Sauce sauce;
Veggies veggies[];
Cheese cheese;
Pepperoni pepperoni;
Clams clam;
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 String getName() {
return name;
}
void setName(String name){
this.name=name;
}
}

class CheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class NYStyleCheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public NYStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class ChicagoStyleCheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public ChicagoStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class CaliforniaStyleCheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CaliforniaStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class PepperoniPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public PepperoniPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class NYStylePepperoniPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public NYStylePepperoniPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class ChicagoStylePepperoniPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public ChicagoStylePepperoniPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class CaliforniaStylePepperoniPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CaliforniaStylePepperoniPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
}
}

class ClamPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public ClamPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}

class NYStyleClamPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public NYStyleClamPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}

class ChicagoStyleClamPizz extends Pizza {
PizzaIngredientFactory ingredientFactory;
public ChicagoStyleClamPizz(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}

class CaliforniaStyleClamPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CaliforniaStyleClamPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}

class VeggiePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public VeggiePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}

class NYStyleVeggiePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public NYStyleVeggiePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}

class ChicagoStyleVeggiePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public ChicagoStyleVeggiePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}

class CaliforniaStyleVeggiePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CaliforniaStyleVeggiePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory=ingredientFactory;
}
void prepare(){
System.out.println("Preparing " + name);
dough=this.ingredientFactory.createDough();
sauce=this.ingredientFactory.createSauce();
cheese=this.ingredientFactory.createCheese();
clam=this.ingredientFactory.createClam();
}
}
interface Dough{

}
class ThickCrustDough implements Dough{
public ThickCrustDough(){
System.out.println("ThickCrustDough");
}
}
class ThinCrustDough implements Dough{
public ThinCrustDough(){
System.out.println("ThinCrustDough");
}
}
interface Clams{

}
class FrozenClams implements Clams{
public FrozenClams(){
System.out.println("FrozenClams");
}
}
class FreshClams implements Clams{
public FreshClams(){
System.out.println("FreshClams");
}
}
interface Sauce{

}
class PlumTomatoSauce implements Sauce{
public PlumTomatoSauce(){
System.out.println("PlumTomatoSauce");
}
}
class MarinaraSauce implements Sauce{
public MarinaraSauce(){
System.out.println("MarinaraSauce");
}
}

interface Cheese{
}
class MozzarellaCheese implements Cheese{
public MozzarellaCheese(){
System.out.println("MozzarellaCheese");
}
}
class ReggianoCheese implements Cheese{
public ReggianoCheese(){
System.out.println("ReggianoCheese");
}
}
interface Pepperoni{
}
class SlicedPepperoni implements Pepperoni{
public SlicedPepperoni(){
System.out.println("SlicedPepperoni");
}
}
interface Veggies{
}
class Garlic implements Veggies{
public Garlic(){
System.out.println("Garlic");
}
}
class Onion implements Veggies{
public Onion(){
System.out.println("Onion");
}
}
class Mushroom implements Veggies{
public Mushroom(){
System.out.println("Mushroom");
}
}
class RedPepper implements Veggies{
public RedPepper(){
System.out.println("RedPepper");
}
}
class BlackOlives implements Veggies{
public BlackOlives(){
System.out.println("BlackOlives");
}
}
class Spinach implements Veggies{
public Spinach(){
System.out.println("Spinach");
}
}
class Eggplant implements Veggies{
public Eggplant(){
System.out.println("Eggplant");
}
}
interface PizzaIngredientFactory{
public Dough createDough();
public Sauce createSauce();
public Cheese createCheese();
public Veggies[] createVeggies();
public Pepperoni createPepperoni();
public Clams createClam();
}
class NYPizzaIngredientFctory implements PizzaIngredientFactory{
public Dough createDough(){
return new ThinCrustDough();
}
public Sauce createSauce(){
return new MarinaraSauce();
}
public Cheese createCheese(){
return new ReggianoCheese();
}
public Veggies[] createVeggies(){
Veggies veggies[]={new Garlic(),new Onion(),new Mushroom(),new RedPepper()};
return veggies;
}
public Pepperoni createPepperoni(){
return new SlicedPepperoni();
}
public Clams createClam(){
return new FreshClams();
}
}

class ChicagoPizzaIngredientFctory implements PizzaIngredientFactory{
public Dough createDough(){
return new ThinCrustDough();
}
public Sauce createSauce(){
return new PlumTomatoSauce();
}
public Cheese createCheese(){
return new MozzarellaCheese();
}
public Veggies[] createVeggies(){
Veggies veggies[]={new BlackOlives(),new Spinach(),new Eggplant()};
return veggies;
}
public Pepperoni createPepperoni(){
return new SlicedPepperoni();
}
public Clams createClam(){
return new FrozenClams();
}
}



package pattern;

public class PizzaTestDrive {
public static void main(String[] args) {
PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();
Pizza pizza = nyStore.orderPizza("cheese");
System.out.println("Ethan ordered a " + pizza.getName() + "\n");
pizza = chicagoStore.orderPizza("cheese");
System.out.println("Joel ordered a " + pizza.getName() + "\n");
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值