package {
//cake是抽象类
public class Cake {
protected var _name:String;
public function Cake() {
}
internal function prepare():void {
trace("Preparing " + name);
}
internal function bake():void {
trace("Baking " + name);
}
internal function box():void {
trace("Boxing " + name);
}
public function get name():String {
return _name;
}
}
}
package {
//抽象类
public class CakeShop {
public function CakeShop() {
}
public function orderCake(type:String):Cake {
var cake:Cake = createCake(type);
cake.prepare();
cake.bake();
cake.box();
return cake;
}
//工厂方法
protected function createCake(type:String):Cake {
return null;
}
}
}
package {
public class ShanghaiCreamCake extends Cake {
public function ShanghaiCreamCake() {
_name = "Shanghai cream cake";
}
}
}
package {
public class ShanghaiChocolateCake extends Cake {
public function ShanghaiChocolateCake() {
_name = "Shanghai chocolate cake";
}
}
}
package {
public class ShanghaiStrawberryCake extends Cake {
public function ShanghaiStrawberryCake() {
_name = "Shanghai strawberry cake";
}
}
}
package {
public class BeijingStrawberryCake extends Cake {
public function BeijingStrawberryCake() {
_name = "Beijing strawberry cake";
}
}
}
package {
public class BeijingCreamCake extends Cake {
public function BeijingCreamCake() {
_name = "Beijing cream cake";
}
}
}
package {
public class BeijingChocolateCake extends Cake {
public function BeijingChocolateCake() {
_name = "Beijing chocolate cake";
}
}
}
package {
public class ShanghaiCakeShop extends CakeShop {
public function ShanghaiCakeShop() {
}
//工厂方法
override protected function createCake(type:String):Cake {
var cake:Cake;
if(type == "chocolateCake") {
cake = new ShanghaiChocolateCake();
}else if(type == "creamCake") {
cake = new ShanghaiCreamCake();
}else if(type == "strawberryCake") {
cake = new ShanghaiStrawberryCake();
}
return cake;
}
}
}
package {
public class BeijingCakeShop extends CakeShop {
public function BeijingCakeShop() {
}
override protected function createCake(type:String):Cake {
var cake:Cake;
if(type == "chocolateCake") {
cake = new BeijingChocolateCake();
}else if(type == "creamCake") {
cake = new BeijingCreamCake();
}else if(type == "strawberryCake") {
cake = new BeijingStrawberryCake();
}
return cake;
}
}
}
package {
import flash.display.Sprite;
public class FactoryMethodTest extends Sprite {
public function FactoryMethodTest() {
var cakeShop:CakeShop = new BeijingCakeShop();
var cake:Cake = cakeShop.orderCake("chocolateCake");
trace("We ordered a " + cake.name);
cake = cakeShop.orderCake("creamCake");
trace("We ordered a " + cake.name);
cake = cakeShop.orderCake("strawberryCake");
trace("We ordered a " + cake.name);
trace();
cakeShop = new ShanghaiCakeShop();
cake = cakeShop.orderCake("chocolateCake");
trace("We ordered a " + cake.name);
cake = cakeShop.orderCake("creamCake");
trace("We ordered a " + cake.name);
cake = cakeShop.orderCake("strawberryCake");
trace("We ordered a " + cake.name);
}
}
}
package { //cake是抽象类 public class Cake { protected var _name:String; public function Cake() { } internal function prepare():void { trace("Preparing " + name); } inte