简单工厂
简单工厂由工厂类,抽象产品类和具体产品类组成
1.抽象产品接口
package
{
public interface IGraph
{
function draw():void
}
}
2.产品工厂类
package
{
public class GrapFactory
{
public function GrapFactory()
{
}
public static function factory(shape:String):IGraph
{
switch (shape)
{
case "circle":
return new Circle();
break;
case "triangle":
return new Triangle();
break;
default:
throw new Error("shape is not exit");
break;
}
}
}
}
3.具体产品类
package
{
import flash.display.Shape;
public class Circle extends Shape implements IGraph
{
public var radius:Number
public function Circle()
{
}
public function draw():void
{
this.graphics.lineStyle(1);
this.graphics.drawCircle(0,0,radius);
}
}
}
package
{
import flash.display.Shape;
import flash.geom.Point;
public class Triangle extends Shape implements IGraph
{
public var p1:Point;
public var p2:Point;
public var p3:Point;
public var p4:Point;
public function Triangle()
{
}
public function draw():void
{
this.graphics.lineStyle(1);
this.graphics.moveTo(p1.x,p1.y);
this.graphics.moveTo(p2.x,p2.y);
this.graphics.moveTo(p3.x,p3.y);
this.graphics.moveTo(p1.x,p1.y);
}
}
}
4.调用
package
{
import flash.display.Sprite;
public class shejimoshi extends Sprite
{
public function shejimoshi()
{
var circle:Circle = GrapFactory.factory("circle") as Circle;
circle.radius = 20;
circle.draw();
addChild(circle);
}
}
}

浙公网安备 33010602011771号