一个工厂可以生产很多的产品,各种各样五花八门的产品。这些产品是通过工厂生产出来的,一个工厂可以生产这些产品。这样我们可以联想到自己所写的代码。工厂模式解决了创建实例化的过程。典型的例子就是计算器的问题。面试人员同样很喜欢把这样的题目考察应聘者。看是否有一种熟悉的面向对象思维,把代码封装,实现代码的重用性,继承基类。扩展自己所要实现的功能。
面向对象的三大特性:封装性,继承,多态
像as3.0那样,我们可以实现一些简单的做法,例如我要计算面积这样的方法作为基类。然后我们用继续的方法继承基类原本的方法,但是我们要计算三角形面积,或者正方形面积,这些都是很简单,继承了基类的方法,并不是我们所要的结果,这时候我们需要同过override 声明来重写方法,做到继承所要的方法。
//基类
package {
public class Operation {
private var StrNumA:Number=0;
private var StrNumB:Number=0;
public function set SetNumA(Num:Number):void {
StrNumA=Num;
}
public function set SetNumB(Num:Number):void {
StrNumB=Num;
}
public function get GetNumA():Number {
return StrNumA;
}
public function get GetNumB():Number {
return StrNumB;
}
public function Result():Number {
var Myresult:Number=0;
return Myresult;
}
}
}//运算
//加法继承基类
package {
public class Add extends Operation {
private var NumA:Number;
private var NumB:Number;
override public function set SetNumA(Num:Number):void {
NumA=Num;
}
override public function set SetNumB(Num:Number):void {
NumB=Num;
}
override public function get GetNumA():Number {
return NumA;
}
override public function get GetNumB():Number {
return NumB;
}
override public function Result():Number {
var Myresult:Number=0;
Myresult=NumA+NumB;
return Myresult;
}
}
}
//减法继承基类
package{
public class Sub extends Operation{
private var NumA:Number;
private var NumB:Number;
override public function set SetNumA(Num:Number):void {
NumA=Num;
}
override public function set SetNumB(Num:Number):void {
NumB=Num;
}
override public function get GetNumA():Number {
return NumA;
}
override public function get GetNumB():Number {
return NumB;
}
override public function Result():Number{
var Myresult;
Myresult=NumA-NumB;
return Myresult;
}
}
}
//继承基类
package{
public class Mul extends Operation{
private var NumA:Number;
private var NumB:Number;
override public function set SetNumA(Num:Number):void {
NumA=Num;
}
override public function set SetNumB(Num:Number):void {
NumB=Num;
}
override public function get GetNumA():Number {
return NumA;
}
override public function get GetNumB():Number {
return NumB;
}
override public function Result():Number{
var Myresult;
Myresult=NumA*NumB;
return Myresult;
}
}
}
//继承基类
package{
public class Div extends Operation{
private var NumA:Number;
private var NumB:Number;
override public function set SetNumA(Num:Number):void {
NumA=Num;
}
override public function set SetNumB(Num:Number):void {
NumB=Num;
}
override public function get GetNumA():Number {
return NumA;
}
override public function get GetNumB():Number {
return NumB;
}
override public function Result():Number{
var Myresult:Number=0;
if(NumB==0)trace("cuowo");//这里还要处理,以防除数为o
Myresult=NumA/NumB;
return Myresult;
}
}
}
//工厂
package {
public class OperationFactory {
public static function CreateOperate(str:String):Operation {
var op:Operation;
switch (str) {
case "+" :
op=new Add();
trace("+");
break;
case "-" :
op=new Sub();
break;
case "*" :
op=new Mul();
break;
case "/" :
op=new Div();
break;
}
return op;
}
}
}
还有一个客户端:这里还要做其他处理才可以完成我们的效果
package {
import flash.display.MovieClip;
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class Main extends MovieClip {
var oper:Operation;
public function Main() {
oper=OperationFactory.CreateOperate("-");
oper.SetNumA=2;
oper.SetNumB=3;
trace(oper.GetNumB);
var Myresult2:Number=oper.Result();
trace(Myresult2);
}
}