18. Broker Mode (mediator pattern)

Source: Internet
Author: User

usingSystem;namespaceconsoleapplication9{classProgram {/// <summary>        ///a mediator pattern that defines a mediation object to encapsulate the interaction between a series of objects. ///intermediaries do not need to explicitly reference each other between objects, which reduces coupling and can independently change the interaction between them. /// </summary>        /// <param name= "args" ></param>        Static voidMain (string[] args) {Abstractcardpartner A=NewPartera (); Abstractcardpartner B=NewParterb (); //Initial MoneyA.moneycount = -; B.moneycount= -; Abstractmediator Mediator=NewMediatorpater (A, B); //a won.A.changecount (5, mediator); Console.WriteLine ("A now the money is: {0}", A.moneycount); //it should be .Console.WriteLine ("B Now the money is: {0}", B.moneycount); //it should be .//B won.B.changecount (Ten, mediator); Console.WriteLine ("A now the money is: {0}", A.moneycount); //it should be .Console.WriteLine ("B Now the money is: {0}", B.moneycount); //it should be .Console.read (); }    }    //Abstract Card friend class     Public Abstract classAbstractcardpartner { Public intMoneycount {Get;Set; }  PublicAbstractcardpartner () {Moneycount=0; }         Public Abstract voidChangecount (intCount, abstractmediator mediator); }    //brand friend a class     Public classPartera:abstractcardpartner {//dependency and abstract mediator objects         Public Override voidChangecount (intCount, abstractmediator mediator) {Mediator.        Awin (Count); }    }    //brand Friend Class B     Public classParterb:abstractcardpartner {//dependency and abstract mediator objects         Public Override voidChangecount (intCount, abstractmediator mediator) {Mediator.        BWin (Count); }    }    //Abstract Mediator Class     Public Abstract classAbstractmediator {protectedAbstractcardpartner A; protectedAbstractcardpartner B;  PublicAbstractmediator (Abstractcardpartner A, Abstractcardpartner b) {a=A; B=b; }         Public Abstract voidAwin (intcount);  Public Abstract voidBWin (intcount); }    //Specific Intermediary class     Public classMediatorpater:abstractmediator { PublicMediatorpater (Abstractcardpartner A, Abstractcardpartner b):Base(A, b) {} Public Override voidAwin (intcount) {A.moneycount+=count; B.moneycount-=count; }         Public Override voidBWin (intcount) {B.moneycount+=count; A.moneycount-=count; }    }}

18. Broker Mode (mediator pattern)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.