TypeScript mediator mediator

//Intermediary Moderator Mode

interface mediatorInterface {

    selfMethod():void; //User's self-issued behavior

    depMethod():void; //Dependent behavior. The user wants to do things through this method.

}

//user

class AAA   {

    private mediator:Mediator = null ;

    public constructor(mediator:Mediator) {

        this.mediator = mediator;

        YBLog.log("beProxy", "  用户A  ");

    }

    public dodoA(): void {

        YBLog.log("beProxy", " 用户A  dodoA ");

        this.mediator.depMethod();

    }

 

 

}

//user

class BBB  {

    private mediator:Mediator = null ;

    public constructor(mediator:Mediator) {

        this.mediator = mediator;

        YBLog.log("beProxy", "  用户B  ");

    }

    public dodoB(): void {

        YBLog.log("BBB", " 用户B dodoB   ");

        this.mediator.depMethod();

    }

}

 

class  Mediator implements mediatorInterface

{

    private _a: AAA = null;

    public getA (): AAA

    return this._a;

    }

    public setA (value: AAA)

    this._a = value;

    }

    private _b: BBB = null;

    public getB(): BBB {

        return this._b;

    }

    public setB(value: BBB) {

        this._b = value;

    }

 

    public  constructor()

    {

 

    }

    public selfMethod () 

    {

        YBLog.log("Mediator", "What does the mediator let the user do");

        this._b.dodoB();

        this._a.dodoA();

    } 

    //User's self-issued behavior

    public depMethod() //Dependent behavior. The user wants to do things through this method.

    {

        YBLog.log("Mediator", "What does the user ask the intermediary to do");

    }

}

let mediator:Mediator  =  new Mediator();

let a = new AAA(mediator);

let b = new BBB(mediator);

mediator.setA(a);

mediator.setB(b);

mediator.selfMethod ();

One advantage: it reduces dependence, becomes one-to-one, and reduces coupling. 

Two disadvantages: this class will expand and the logic will be complicated.

Guess you like

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