简单的设计模式

69 阅读2分钟

工厂模式

export {}
abstract class AmericaCoffee{}
abstract class LatteCoffee{}
abstract class CappuccinoCoffee{}

class StarBuckAmericanCoffee extends AmericanCoffee {}
class LuckinAmericanCoffee extends AmericaCoffee {}
class StarBuckLetteCoffee extends LatteCoffee{}
class LuckinLatteeCoffee extends LatteCoffee{}
class LuckinCappuccinoCoffeeCoffee extends LatteCoffee{}
class StarBuckCappuccinoCoffee extends CappuccinoCoffee{}

// 抽象工厂方法

abstract class CafeFactory{
    abstract createAmericanCoffee():AmericaCoffee
    abstract createLatteeCoffee():LatteCoffee
    abstract createCappuccinoCoffeeCoffee():CappuccinoCoffee
}

class StarBuckCafeFactory extends CafeFactory{
    createAmericanCoffee(): AmericaCoffee {
        return new StarBuckAmericanCoffee("")
    }
    createLatteeCoffee(): LatteCoffee {
        return new StarBuckLetteCoffee()
    }
    createCappuccinoCoffeeCoffee(): CappuccinoCoffee {
        return new StarBuckCappuccinoCoffee()
    }   
}


class LuckinCafeFactory extends CafeFactory{
    createAmericanCoffee(): AmericaCoffee {
        return new LuckinAmericanCoffee()
    }
    createLatteeCoffee(): LatteCoffee {
        return new LuckinLatteeCoffee()
    }
    createCappuccinoCoffeeCoffee(): CappuccinoCoffee {
        return new LuckinCappuccinoCoffeeCoffee()
    }   
}



let luckinCafeFactory=new LuckinCafeFactory()

console.log(luckinCafeFactory.createAmericanCoffee()) // 创建Luckin美式咖啡
    

适配器模式

// 这是需要被适配的类
class Socket{
    output(){
        return '220V'
    }
}

abstract class Power{
    abstract charge():string
}



class PowerAdaptor extends Power{
    constructor(public socket:Socket){
        super();
    }
    charge(): string {
        return this.socket.output()+'转换为24V'
    }
}

let adaptor=new PowerAdaptor(new Socket())


console.log(adaptor.charge());

装饰器模式

abstract class Shape{
    abstract draw():void
}

class Circle extends Shape{
    draw(): void {
        console.log('绘制圆形');
    }
}
    
class Rectangle extends Shape{
    draw(): void {
        console.log('绘制矩形');
        
    }
}

// 装饰器模式的


abstract class ColorfulShape extends Shape{
    constructor(public shape:Shape){
        super()
    }
    abstract draw(): void;
}

class RedColorfulShape extends ColorfulShape{
    draw(): void {
        this.shape.draw()
        console.log('把边框涂成红色');
    }
    
}

class GreenColorfulShape extends ColorfulShape{
    draw(): void {
        this.shape.draw()
        console.log('把边框涂成绿色');
    }
    
}
// 画一个红色的圆形

let redColorfulShape=new RedColorfulShape(new Circle())
redColorfulShape.draw()


namespace a{
    interface Animal{
        swings:number
        fly():void
    }
    // 如果说装饰器函数是用来修饰类的话 那么target就是类的构造函数本身
    function flyable(target){
        console.log(target);
        target.prototype.swings=2
        target.prototype.fly=function(){
            console.log('我能飞');
            
        }
    }
    @flyable
    class Animal{
        constructor(){}
    }
    let animal:Animal=new Animal
    console.log(animal.swings);
    animal.fly()
}

namespace b{
    // 实例属性的时候target是类的原型对象,key是属性的名字
    function instancePropertyDecorator(target:any,key: any) {
        target.protoName="我是类的原型上的属性"
        console.log('instancePropertyDecorator',target,key);
    }
    function classPropertyDecorator(target:any,key: any) {
        // 类的静态属性的时候target是类的构造函数,key是属性名
        console.log('classPropertyDecorator',target,key);
    }
        
    // 如果是实例的方法的话target是类的原型对象 key是方法名 descriptor属性描述符
    function instanceMethodDecorator(target:any,key: any,descriptor:any) {
        console.log('instanceMethodDecorator',target,key,descriptor);
    }
    // 如果是类的静态方法的话target是类的构造函数  key是方法名 descriptor属性描述符
    function classMethodDecorator(target:any,key: any,descriptor:any) {
        console.log('classMethodDecorator',target,key,descriptor);
    }
    class Person{
        @instancePropertyDecorator
        instanceProperty:string //实例属性
        @classPropertyDecorator
        static classProperty:string //类的静态属性
        @instanceMethodDecorator
        instanceMethod(){} //实例的方法
        @classMethodDecorator
        static classMethod(){

        } //类的静态方法
    }
}