package com.pizza {
import com.pizza.ingredient.Cheese;
import com.pizza.ingredient.Dough;
import com.pizza.ingredient.Sauce;
//Pizza是抽象类
public class Pizza {
protected var _name:String;
protected var _dough:Dough;
protected var _sauce:Sauce;
protected var _cheese:Cheese;
public function Pizza() {
}
public function prepare():void {
trace("Preparing " + name);
}
public function bake():void {
trace("Baking " + name);
}
public function box():void {
trace("Boxing " + name);
}
public function get name():String {
return _name;
}
}
}
package com.pizza.ingredient {
public interface Dough {
}
}
package com.pizza.ingredient {
public interface Cheese {
}
}
package com.pizza.ingredient {
public interface Sauce {
}
}
package com.pizza.ingredient {
public class BeijingDough implements Dough {
public function BeijingDough() {
}
public function get name():String {
return "BeijingDough";
}
}
}
package com.pizza.ingredient {
public class BeijingCheese implements Cheese {
public function BeijingCheese() {
}
public function get name():String {
return "BeijingCheese";
}
}
}
package com.pizza.ingredient {
public class BeijingSauce implements Sauce {
public function BeijingSauce() {
}
public function get name():String {
return "BeijingSauce";
}
}
}
package com.pizza.ingredient {
public class ShanghaiCheese implements Cheese {
public function ShanghaiCheese() {
}
public function get name():String {
return "ShanghaiCheese";
}
}
}
package com.pizza.ingredient {
public class ShanghaiSauce implements Sauce {
public function ShanghaiSauce() {
}
public function get name():String {
return "ShanghaiSauce";
}
}
}
package com.pizza.ingredient {
public class ShanghaiDough implements Dough {
public function ShanghaiDough() {
}
public function get name():String {
return "ShanghaiDough";
}
}
}
package com.pizza.ingredient {
public interface PizzaIngredientFactory {
function createDough():Dough;
function createSauce():Sauce;
function createCheese():Cheese;
}
}
package com.pizza.ingredient {
public class BeijingPizzaIngredientFactory implements PizzaIngredientFactory {
public function BeijingPizzaIngredientFactory() {
}
public function createDough():Dough {
return new BeijingDough();
}
public function createSauce():Sauce {
return new BeijingSauce();
}
public function createCheese():Cheese {
return new BeijingCheese();
}
}
}
package com.pizza.ingredient {
public class ShanghaiPizzaIngredientFactory implements PizzaIngredientFactory {
public function ShanghaiPizzaIngredientFactory() {
}
public function createDough():Dough {
return new ShanghaiDough();
}
public function createSauce():Sauce {
return new ShanghaiSauce();
}
public function createCheese():Cheese {
return new ShanghaiCheese();
}
}
}
package com.pizza {
import com.pizza.ingredient.PizzaIngredientFactory;
public class SaucePizza extends Pizza {
private var pizzaIngredientFactory:PizzaIngredientFactory;
public function SaucePizza(pizzaIngredientFactory:PizzaIngredientFactory) {
_name = "SaucePizza";
this.pizzaIngredientFactory = pizzaIngredientFactory;
}
override public function prepare():void {
trace("Preparing " + name);
_cheese = pizzaIngredientFactory.createCheese();
_dough = pizzaIngredientFactory.createDough();
_sauce = pizzaIngredientFactory.createSauce();
}
}
}
package com.pizza {
import com.pizza.ingredient.PizzaIngredientFactory;
public class DoughPizza extends Pizza {
private var pizzaIngredientFactory:PizzaIngredientFactory;
public function DoughPizza(pizzaIngredientFactory:PizzaIngredientFactory) {
_name = "DoughPizza";
this.pizzaIngredientFactory = pizzaIngredientFactory;
}
override public function prepare():void {
trace("Preparing " + name);
_cheese = pizzaIngredientFactory.createCheese();
_dough = pizzaIngredientFactory.createDough();
_sauce = pizzaIngredientFactory.createSauce();
}
}
}
package com.pizza {
import com.pizza.ingredient.PizzaIngredientFactory;
public class CheesePizza extends Pizza {
private var pizzaIngredientFactory:PizzaIngredientFactory;
public function CheesePizza(pizzaIngredientFactory:PizzaIngredientFactory) {
_name = "CheesePizza";
this.pizzaIngredientFactory = pizzaIngredientFactory;
}
override public function prepare():void {
trace("Preparing " + name);
_cheese = pizzaIngredientFactory.createCheese();
_dough = pizzaIngredientFactory.createDough();
_sauce = pizzaIngredientFactory.createSauce();
}
}
}
package com.pizza.store {
import com.pizza.Pizza;
//抽象类
public class PizzaStore {
public function PizzaStore() {
}
public function orderPizza(type:String):Pizza {
var pizza:Pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.box();
return pizza;
}
//工厂方法
protected function createPizza(type:String):Pizza {
return null;
}
}
}
package com.pizza.store {
import com.pizza.CheesePizza;
import com.pizza.DoughPizza;
import com.pizza.Pizza;
import com.pizza.SaucePizza;
import com.pizza.ingredient.BeijingPizzaIngredientFactory;
public class BeijingPizzaStore extends PizzaStore {
public function BeijingPizzaStore() {
}
override protected function createPizza(type:String):Pizza {
if(type == "cheese") {
return new CheesePizza(new BeijingPizzaIngredientFactory());
}else if(type == "sauce") {
return new SaucePizza(new BeijingPizzaIngredientFactory());
}else if(type == "dough") {
return new DoughPizza(new BeijingPizzaIngredientFactory());
}
return null;
}
}
}
package com.pizza.store {
import com.pizza.CheesePizza;
import com.pizza.DoughPizza;
import com.pizza.Pizza;
import com.pizza.SaucePizza;
import com.pizza.ingredient.ShanghaiPizzaIngredientFactory;
public class ShanghaiPizzaStore extends PizzaStore {
public function ShanghaiPizzaStore() {
}
override protected function createPizza(type:String):Pizza {
if(type == "cheese") {
return new CheesePizza(new ShanghaiPizzaIngredientFactory());
}else if(type == "sauce") {
return new SaucePizza(new ShanghaiPizzaIngredientFactory());
}else if(type == "dough") {
return new DoughPizza(new ShanghaiPizzaIngredientFactory());
}
return null;
}
}
}
package {
import com.pizza.Pizza;
import com.pizza.store.BeijingPizzaStore;
import com.pizza.store.PizzaStore;
import com.pizza.store.ShanghaiPizzaStore;
import flash.display.Sprite;
public class AbstractFactoryTest extends Sprite {
public function AbstractFactoryTest() {
var pizzaStore:PizzaStore = new ShanghaiPizzaStore();
var pizza:Pizza = pizzaStore.orderPizza("cheese");
pizza = pizzaStore.orderPizza("sauce");
pizza = pizzaStore.orderPizza("dough");
pizzaStore = new BeijingPizzaStore();
pizza = pizzaStore.orderPizza("cheese");
pizza = pizzaStore.orderPizza("sauce");
pizza = pizzaStore.orderPizza("dough");
}
}
}