TypeScript proxy mode/delegation mode

interface proxyInterface {

    dofirst();

}

interface player {

    dodoA(): void;

}

 

class beProxy implements player {

    public constructor() {

        YBLog.log("beProxy", "I am the person being proxied");

    }

    public dodoA(): void {

        YBLog.log("beProxy", "I am the person being proxied dodoA");

    }

 

 

}

//Agent

class proxy implements player,proxyInterface {

    private beProxy:player = null; //The person acting as my agent

    public constructor() {

        YBLog.log("proxy", "I am a proxy to create a proxy");

        this.beProxy = new beProxy ();

    }

    public dodoA(): void {    

        this.dofirst();

        YBLog.log("proxy", "I am a proxy dodoA uses a personalized method and calls the proxy method at the same time");

        this.beProxy.dodoA ()

    }

    public dofirst(): void {

        YBLog.log("proxy", "I am a proxy, so I can do my own thing first");

    }

}

new proxy().dodoA();

1. Advantages: High expansion, can keep the things of beProxy unchanged, and directly extend the proxy to complete some additional work.

Two disadvantages: the memory is large.

Guess you like

Origin blog.csdn.net/ting100/article/details/108626781