Simple Factory Animal as package public interface Animal
简单 厂模式Simple Factory 动物接口(Animal. as) package { public interface Animal { function eat ( ) : void ; //所有动物类必须实现eat方法 } } main 接口 Animal +eat : void +main : void Simple. Factory Tiger Dolphin +create : Animal +eat : void +run : void +eat : void +swim : void
简单 厂模式Simple Factory 老虎类 (Tiger. as) package { public class Tiger implements Animal { // implements表示实现接口 public function run ( ) : void { trace ( "老虎会跑" ) ; } public function eat ( ) : void { //实现接口 trace ( "老虎会吃" ) ; } } } main 接口 Animal +eat : void +main : void Simple. Factory Tiger Dolphin +create : Animal +eat : void +run : void +eat : void +swim : void
简单 厂模式Simple Factory 海豚类(Dolphin. as) package { public class Dolphin implements Animal { public function swim ( ) : void { trace ( "海豚会游泳" ) ; } public function eat ( ) : void { //实现接口 trace ( "海豚会吃" ) ; } } } main 接口 Animal +eat : void +main : void Simple. Factory Tiger Dolphin +create : Animal +eat : void +run : void +eat : void +swim : void
简单 厂模式Simple Factory 简单 厂类(Sample. Factory. as) package { public class Sample. Factory { public static function create ( animal. Name : String ) : Animal { //使用静态方法的目的,是无须实例化对象 //根据参数创建类 if ( animal. Name == "Tiger" ) { return new Tiger ; } else if ( animal. Name == "Dolphin" ) { return new Dolphin ; }} } main 接口 Animal } } +eat : void +main : void Simple. Factory Tiger Dolphin +create : Animal +eat : void +run : void +eat : void +swim : void
简单 厂模式Simple Factory 文档类(main. as) package { import flash. display. Sprite ; public class main extends Sprite { public function main ( ) { var A : Animal ; A = Sample. Factory. create ( "Tiger" ) ; A. eat ( ) ; //以下用到了向下转换,先判断类型,再执行转换 if ( A is Tiger ) { ( A as Tiger ). run ( ) ; } A = Sample. Factory. create ( "Dolphin" ) ; A. eat ( ) ; if ( A is Dolphin ) { ( A as Dolphin ). swim ( ) ; } } } main 接口 Animal } +eat : void +main : void Simple. Factory Tiger Dolphin +create : Animal +eat : void +run : void +eat : void +swim : void
厂方法模式Factory Method 动物接口(Animal. as) package { public interface Animal { function eat ( ) : void ; //所有动物类必须实现eat方法 } } 接口 Animal +eat : void 接口 Factory Dolphin类 +create : Animal +eat : void +swim : void Dolphin. Factory Tiger. Factory +create : Animal Tiger类 +eat : void +run : void
厂方法模式Factory Method 老虎类 (Tiger. as) package { public class Tiger implements Animal { public function run ( ) : void { trace ( "老虎会跑" ) ; } public function eat ( ) : void { //实现接口 trace ( "老虎会吃" ) ; 接口 Animal } } +eat : void } 接口 Factory Dolphin类 +create : Animal +eat : void +swim : void Dolphin. Factory Tiger. Factory +create : Animal Tiger类 +eat : void +run : void
厂方法模式Factory Method 海豚类(Dolphin. as) package { public class Dolphin implements Animal { public function swim ( ) : void { trace ( "海豚会游泳" ) ; } public function eat ( ) : void { //实现接口 trace ( "海豚会吃" ) ; 接口 Animal } } +eat : void } 接口 Factory Dolphin类 +create : Animal +eat : void +swim : void Dolphin. Factory Tiger. Factory +create : Animal Tiger类 +eat : void +run : void
厂方法模式Factory Method 抽象 厂接口(Factory. as) package { public interface Factory { function create ( ) : Animal ; } } 接口 Animal +eat : void 接口 Factory Dolphin类 +create : Animal +eat : void +swim : void Dolphin. Factory Tiger. Factory +create : Animal Tiger类 +eat : void +run : void
厂方法模式Factory Method 老虎 厂(Tiger. Factory. as) package { public class Tiger. Factory implements Factory { public function create ( ) : Animal { return new Tiger ( ) ; } } } 接口 Animal +eat : void 接口 Factory Dolphin类 +create : Animal +eat : void +swim : void Dolphin. Factory Tiger. Factory +create : Animal Tiger类 +eat : void +run : void
厂方法模式Factory Method 海豚 厂(Dolphin. Factory. as) package { public class Dolphin. Factory implements Factory { public function create ( ) : Animal { return new Dolphin ( ) ; } } } 接口 Animal +eat : void 接口 Factory Dolphin类 +create : Animal +eat : void +swim : void Dolphin. Factory Tiger. Factory +create : Animal Tiger类 +eat : void +run : void
厂方法模式Factory Method 文档类(main. as) package { import flash. display. Sprite ; public class main extends Sprite { public function main ( ) { var f : Factory ; var A : Animal ; //定义为抽象类型 f = new Tiger. Factory ( ) ; A = f. create ( ) ; //具体化 厂对象并创建动物对象 接口 Animal if ( A is Tiger ) { ( A as Tiger ). run ( ) ; A. eat ( ) ; } f = new Dolphin. Factory ( ) ; A = f. create ( ) ; +eat : void if ( A is Dolphin ) { ( A as Dolphin ). swim ( ) ; A. eat ( ) ; } } } 接口 Factory Dolphin类 +create : Animal } +eat : void +swim : void Dolphin. Factory Tiger. Factory +create : Animal Tiger类 +eat : void +run : void
抽象 厂模式Abstract Factory 动物接口(Animal. as) package { public interface Animal { 接口 Animal function eat ( ) : void ; +eat : void } } Dolphin 接口 Factory Tiger +create. A: Animal +create. E: Environment +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tiger. Factory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 老虎类(Tiger. as) package { public class Tiger implements Animal { 接口 Animal public function run ( ) : void { +eat : void trace ( "老虎会跑" ) ; } Dolphin public function eat ( ) : void { trace ( "老虎会吃" ) ; } } 接口 Factory Tiger +create. A: Animal +create. E: Environment } +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tige. Factory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 海豚类(Dolphin. as) package { public class Dolphin implements Animal { 接口 Animal public function swim ( ) : void { +eat : void trace ( "海豚会游泳" ) ; } Dolphin public function eat ( ) : void { trace ( "海豚会吃" ) ; } } 接口 Factory Tiger +create. A: Animal +create. E: Environment } +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tige. Factory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 环境接口(Environment. as) package { public interface Environment { 接口 Animal function EName ( ) : void ; +eat : void } } Dolphin 接口 Factory Tiger +create. A: Animal +create. E: Environment +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tiger. Factory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 陆地类(load. as) package { public class land implements Environment { 接口 Animal public function EName ( ) : void { +eat : void trace ( "陆地" ) ; } Dolphin } } 接口 Factory Tiger +create. A: Animal +create. E: Environment +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tigerfactory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 海洋类(sea. as) package { public class sea implements Environment { 接口 Animal public function EName ( ) : void { +eat : void trace ( "海洋" ) ; } Dolphin } } 接口 Factory Tiger +create. A: Animal +create. E: Environment +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tigerfactory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 抽象 厂接口(Factory. as) package { public interface Factory { 接口 Animal //同时创建动物类和对应的环境类 +eat : void function create. A ( ) : Animal ; function create. E ( ) : Environment ; Dolphin } } 接口 Factory Tiger +create. A: Animal +create. E: Environment +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tigerfactory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 老虎 厂(Tiger. Factory. as) package { public class Tiger. Factory implements Factory { 接口 Animal public function create. A ( ) : Animal { +eat : void return new Tiger ( ) ; } Dolphin public function create. E ( ) : Environment { return new land ( ) ; } } 接口 Factory Tiger +create. A: Animal +create. E: Environment } +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tigerfactory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 海豚 厂(Dolphin. Factory. as) package { public class Dolphin. Factory implements Factory { 接口 Animal public function create. Al ( ) : Animal { +eat : void return new Dolphin ( ) ; } Dolphin public function create. E ( ) : Environment { return new sea ( ) ; } } 接口 Factory Tiger +create. A: Animal +create. E: Environment } +eat: void +run : void +eat : void +swim : void 接口 Environment +EName : void Tigerfactory Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
抽象 厂模式Abstract Factory 文档类(main. as) package { import flash. display. Sprite ; 接口 Animal public class main extends Sprite { +eat : void var f : Factory ; var A : Animal ; var B : Environment ; public function main ( ) : void { Dolphin f = new Tiger. Factory ( ) ; A = f. create. A ( ) ; A. eat ( ) ; B = f. create. E ( ) ; 接口 Factory +create. A: Animal +create. E: Environment trace ( B. Ename ) ; } +eat: void +run : void 接口 Environment +EName : void Tigerfactory } } Tiger +eat : void +swim : void Dolphin. Factory +create. A: Animal +create. E: Environment land +create. A: Animal +create. E: Environment +EName : void sea +EName : void
创建者模式Builder 车身接口(car. as) package { public interface car { function c. Name ( ) : void ; Director } } +Director : void +group : void 接口 car +c. Name : void QQ +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder 奔驰车身类(Benz. as) package { public class Benz implements car { public function c. Name ( ) : void { Director trace ( "奔驰车身" ) ; } } +Director : void +group : void 接口 car +c. Name : void } QQ +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder QQ车身类(QQ. as) package { public class QQ implements car { public function c. Name ( ) : void { Director trace ( "QQ车身" ) ; } } +Director : void +group : void 接口 car +c. Name : void } QQ +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder 车轮类(wheel. as) package { //这里因为是举例,所以两种汽车只设计了一种车轮 //如果要设计两种车轮,就应该先提供车轮接口,再分别具体实现不同的车轮 Director public class wheel { public function wheel ( ) : void { trace ( "车轮" ) ; +Director : void +group : void 接口 car +c. Name : void } } QQ } +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder 抽象 厂接口(Factory. as) package { public interface Factory { function create. C ( ) : car ; //建立车身 Director function create. W ( ) : wheel ; //建立车轮 } } +Director : void +group : void 接口 car +c. Name : void QQ +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder 奔驰 厂(Benz. Factory. as) package { public class Benz. Factory implements Factory { public function create. C ( ) : car { Director trace ( "创建奔驰车身" ) ; return new Benz ; +Director : void +group : void } 接口 car +c. Name : void public function create. W ( ) : wheel { return new wheel ; QQ } } } +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder QQ 厂(QQFactory. as) package { public class QQFactory implements Factory { public function create. C ( ) : car { Director trace ( "创建QQ车身" ) ; return new QQ ; +Director : void +group : void } 接口 car +c. Name : void public function create. W ( ) : wheel { return new wheel ; QQ } } } +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder 组装者类(Director. as) package { public class Director { private var _factory : Factory ; public function Director ( f : Factory ) : void { //选择对应的车型 } public function group ( ) : void { 接口 car Director _factory = f ; +Director : void +group : void +c. Name : void //负责组装 QQ var c : car = _factory. create. C ( ) ; var i : uint = 0 ; var w : wheel ; for ( i = 0 ; i < 4 ; i ++ ) { +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void w = _factory. create. W ( ) ; } } QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
创建者模式Builder 文档类(main. as) package { import flash. display. Sprite ; public class main extends Sprite { Director public function main ( ) : void { var d : Director ; //组装奔驰汽车 +Director : void +group : void 接口 car +c. Name : void d= new Director ( new Benz. Factory ( ) ) ; d. group ( ) ; QQ } } } +c. Name : void 接口 Factory Benz +create. C : car +create. W: wheel Wheel +c. Name : void +Wheel : void QQFactory +create. C : car +create. W: wheel Benz. Factory +create. C : car +create. W: wheel
原型模式Prototype 普通动物类(Animal. as) package { public class Animal { //这里是示意,所以没有设计公共接口和抽象类 public var legs : uint ; //这里是示意,所以没有用 setter / getter 方法 public function Animal ( n : uint ) : void { legs = n; } public function change. Legs ( n : uint ) : void { legs = n ; } public function clone ( ) : Animal { //必须在这里也实现克隆方法 return new Animal ( legs ) ; } } }
原型模式Prototype 克隆动物类(Animal. Clone. as) package { public class Animal. Clone { public var legs : uint ; public var A : Animal = new Animal( 4 ) ; public function clone ( ) : Animal. Clone { return new Animal. Clone; } } }
原型模式Prototype 文档类(main. as) package { import flash. display. Sprite ; public class main extends Sprite { public function main ( ) { var A : Animal = new Animal ( 4 ) ; var AC : Animal. Clone = new Animal. Clone ( ) ; AC. legs = 6 ; trace ( "克隆前原来的普通动物腿数" , AC. A. legs ) ; trace ( "克隆前原来的克隆动物腿数" , AC. legs ) ; var AC 1 : Animal. Clone = Animal. Clone ( AC. clone ( ) ) ; AC 1. legs = 2 ; trace ( "克隆后原来的普通动物腿数" , AC. A. legs ) ; trace ( "克隆后原来的克隆动物腿数“ , AC. legs ) ; trace ( "克隆后新普通动物腿数" , AC 1. A. legs ) ; trace ( "克隆后新克隆动物腿数" , AC 1. legs ) ; } } }
饿汉式单例类(Singleton 1. as) package { //饿汉式单例 //初始化类时就创建了自己的对象 import flash. errors. * ; public class Singleton 1 { public static var n: uint=0; private var _Error: Error = new Error ( "不能创建对象的实例" , 99 ) ; public function Singleton 1 ( ) : void { throw _Error ; //试图建立对象时抛出一个错误 } }
懒汉式单例类(Singleton 2. as) package { //懒汉式单例 //试图再次创建时才抛出错误 import flash. errors. * ; public class Singleton 2 { private static var _n : uint = 0 ; //也可以用布尔型变量记录是否曾经被实例化 public var n : uint = 0 ; private var _Error : Error = new Error ( "不能再次创建对象的实例" , 99 ) ; public function Singleton 2 ( ) : void { if ( _n== 0 ) { _n = 1 ; } else { throw _Error ; } }
文档类(main. as) package { import flash. display. Sprite ; public class main 1 extends Sprite { trace ( Singleton 1. n ) ; Singleton 1. n = 10 ; trace ( Singleton 1. n ) ; //静态属性 var s 1 : Singleton 1 = new Singleton 1 ( ) ; //引发自定义错误 var s 2 : Singleton 2 = new Singleton 2 ( ) ; //可以创建唯一的对象实例 trace ( s 2. n ) ; //对象属性 var s 3 : Singleton 2 = new Singleton 2 ( ) ; //引发自定义错误 } }
- Slides: 51