设计模式-创建型模式-工厂模式(工厂三兄弟) TypeScript

栏目: 后端 · 发布时间: 7年前

内容简介:定义一个接口,三个具体类。然后书写如下,通过选择,生产出相应的对象编译后的js如下即,将工厂拆分

定义一个接口,三个具体类。然后书写如下,通过选择,生产出相应的对象

// 定义Shape接口
interface Shape {
	draw():void;
}

// 下面为产品类
// 产品 Circle
class Circle implements Shape{
	public constructor(){

	}

	public draw():void{

	}
}

// 产品Rectangle
class Rectangle implements Shape{
	public constructor(){

	}
	public draw():void{

	}
}

// 下面为生产产品的工厂,根据选择,生产出不同的产品
class ShapeFactory {
	constructor(){

	}
	public static getShape(typeShape:string):Shape{
		if(typeShape === "Circle"){
			return new Circle();
		}

		if (typeShape === "Rectangle"){
			return new Rectangle();
		}

		if (typeShape === null){
			return null;
		}

		return null;
	}
}

// 下面编写测试
let test:Shape = ShapeFactory.getShape("Circle");
// 调用draw方法
test.draw();

复制代码

编译后的js如下

// 下面为产品类
// 产品 Circle
var Circle = /** @class */ (function () {
    function Circle() {
    }
    Circle.prototype.draw = function () {
    };
    return Circle;
}());
// 产品Rectangle
var Rectangle = /** @class */ (function () {
    function Rectangle() {
    }
    Rectangle.prototype.draw = function () {
    };
    return Rectangle;
}());
// 下面为生产产品的工厂,根据选择,生产出不同的产品
var ShapeFactory = /** @class */ (function () {
    function ShapeFactory() {
    }
    ShapeFactory.getShape = function (typeShape) {
        if (typeShape === "Circle") {
            return new Circle();
        }
        if (typeShape === "Rectangle") {
            return new Rectangle();
        }
        if (typeShape === null) {
            return null;
        }
        return null;
    };
    return ShapeFactory;
}());
// 下面编写测试
var test = ShapeFactory.getShape("Circle");
// 调用draw方法
test.draw();

复制代码

利用反射改进

class ShapeFactory1 {
	constructor(){

	};
	public static getShape<T extends Shape>(c:{new ():T}):T{	// C类型为类
		return new c();
	}
}
let test = ShapeFactory1.getShape(Circle);
test.draw();
复制代码
var ShapeFactory1 = /** @class */ (function () {
    function ShapeFactory1() {
    }
    ;
    ShapeFactory1.getShape = function (c) {
        return new c();
    };
    return ShapeFactory1;
}());
var test = ShapeFactory1.getShape(Circle);
test.draw();
复制代码

工厂方法

即,将工厂拆分

// 工厂方法
class CircleFactory{
	constructor(){

	}
	public static getShape():Shape{
		return new Circle();
	}
}
class RectangleFactory{
	constructor(){

	}
	public static getShape():Shape{
		return new Rectangle();
	}
}
let test = CircleFactory.getShape();
test.draw();
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Web Designer's Idea Book

The Web Designer's Idea Book

Patrick Mcneil / How / 2008-10-6 / USD 25.00

The Web Designer's Idea Book includes more than 700 websites arranged thematically, so you can find inspiration for layout, color, style and more. Author Patrick McNeil has cataloged more than 5,000 s......一起来看看 《The Web Designer's Idea Book》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具