typescript 设计模式--装饰器模式

作用

动态为一个对象添加新功能。同时不改变现有的功能。

示例

示例

interface IStream{
    read();
    write();
}
class FileStream implements IStream{
    public read() {
    }
    public write() {

    }
}
abstract class Decorate implements IStream {
    protected stream: IStream;
    constructor(stream: IStream) {
        this.stream = stream;
    }
    public abstract  read():any;
    public abstract write(): any;
}

class CryptoStream extends Decorate{
    constructor(stream: IStream) {
        super(stream);
    }
    public read() {
        // ...
        this.stream.read();
        // ...
    }
    public write() {
        this.stream.write();
    }
}
let fileStr = new FileStream();
let cryptoFile = new CryptoStream(fileStr);

你可能感兴趣的:(typescript)