zoukankan      html  css  js  c++  java
  • 简单工厂

    简单工厂由工厂类,抽象产品类和具体产品类组成

    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);
    		}
    	}
    }
    
  • 相关阅读:
    修改input标签输入样式
    CSS3的transform 转换
    前端小知识--区分get和post请求
    JS面向对象--你真的理解闭包了吗?
    px,em,rem的区别
    傻瓜式教程--实现登录页面的验证码以及验证(VUE)
    基于RBAC权限管理的后台管理系统
    在VUE中实现打印
    关于三层架构的好文章
    RabbitMQ常用命令、管理界面
  • 原文地址:https://www.cnblogs.com/mzbdadou/p/2105353.html
Copyright © 2011-2022 走看看