Android design mode (16)-Broker mode

Source: Internet
Author: User

The mediator pattern, also known as mediator mode or mediator mode, is one of the behavioral design patterns.

The role of intermediaries in life is to connect the two sides of a bridge, such as real estate intermediary, buy a house only with intermediary, and then buy a house also follow the intermediary to deal with,

Meow, a little universe.

This is the case when there is no intermediary:

Each buy a house and sell the house to have to deal with a lot of each other, have a new buy a house person, these sell the house must know can go to contact with him.

After having a mediator:

All buy and sell the house only need to deal with intermediaries, buy a house do not need to know who sells the house is who, how many sell the house and so on. It's a lot easier.

Defined

The interaction of a series of objects is wrapped by a mediator, so that these objects do not have to be explicitly referenced to each other, so that they can be loosely coupled.

When the role of some objects changes, it does not immediately affect the role of other objects, ensuring that these effect protocols are independent of each other.

The mediator model transforms many-to-many interactions into a lot of interactions.

Usage Scenarios
    • There are many interactions between objects, and each object's behavior is dependent on batches, forming a mesh of many-to-many structures, and in order to prevent modifying many other objects when modifying an object, you can use the mediator pattern.
Uml

    • Mediator: An abstract mediator role that defines the interface of a colleague object to a mediator.
    • Concretemediator: A specific mediator role that receives messages from a specific colleague object while issuing commands to a specific colleague object.
    • Colleague: Abstract colleague Class role, which defines the interface of the mediator object, only knows the mediation and does not know the other colleague objects.
    • Concretecolleaguea,b: The specific colleague class role, each individual colleague class is aware of itself in a small range of behavior, without knowing his behavior in a wide range.

Template code:

Abstract intermediary:

publicinterface Mediator {    void change();}

Specific intermediaries:

 Public  class concretemediator implements mediator {     PublicConcretecolleaguea Concretecolleaguea; PublicConcretecolleagueb Concretecolleagueb; Public void Setconcretecolleaguea(Concretecolleaguea Concretecolleaguea) { This. Concretecolleaguea = Concretecolleaguea; } Public void Setconcretecolleagueb(Concretecolleagueb Concretecolleagueb) { This. Concretecolleagueb = Concretecolleagueb; }@Override     Public void  Change() {concretecolleaguea.action ();    Concretecolleagueb.action (); }}

Abstract colleague:

publicabstractclass Colleague {    public Mediator mediator;    publicColleague(Mediator mediator) {        this.mediator = mediator;    }    publicabstractvoidaction();}

Specific colleague:

 Public  class Concretecolleaguea extends colleague {     Public Concretecolleaguea(Mediator mediator) {Super(mediator); }@Override     Public void Action() {System.out.println ("to the intermediary to do a thing."); }} Public  class concretecolleagueb extends colleague {     Public Concretecolleagueb(Mediator mediator) {Super(mediator); }@Override     Public void Action() {System.out.println ("to the intermediary to do the b thing."); }}
Simple implementation

Take a computer as an example. CPU, graphics, memory and other parts of the interaction are realized through the motherboard, and each part only need to do their own work, do not need to know what other parts are. So the motherboard can serve as their mediator.

Abstract intermediary:

publicabstractclass Mediator {    publicabstractvoidchange(Colleague colleague);}

Specific intermediaries, motherboard:

 Public  class mainboard extends mediator {    PrivateCddevice Cddevice;PrivateCPU CPU;PrivateGraphicscard Graphicscard;PrivateSoundcard soundcard;@Override     Public void  Change(colleague colleague) {if(Colleague==cddevice)        {HANDLECD (Cddevice) colleague); }if(COLLEAGUE==CPU)        {handlecpu (CPU) colleague); }    }Private void HANDLECD(Cddevice Cddevice)    {Cpu.decodedata (Cddevice.read ()); }Private void handlecpu(CPU CPU)        {Soundcard.playsound (Cpu.getdatasound ());    Graphicscard.vidoeplay (Cpu.getdatavideo ()); } Public void Setcddevice(Cddevice Cddevice) { This. Cddevice = Cddevice; } Public void SetCpu(CPU CPU) { This. CPU = CPU; } Public void Setgraphicscard(Graphicscard Graphicscard) { This. Graphicscard = Graphicscard; } Public void Setsoundcard(Soundcard soundcard) { This. soundcard = soundcard; }}

Abstract Parts:

publicabstractclass Colleague {    public Mediator mediator;    publicColleague(Mediator mediator) {        this.mediator = mediator;    }}

Specific parts:

 Public  class CPU extends colleague {    PrivateString Datavideo,datasound; Public CPU(Mediator mediator) {Super(mediator); } PublicStringGetdatavideo(){returnDatavideo; } PublicStringGetdatasound() {returnDatasound; }//Parse data, split audio and video     Public void Decodedata(String data) {string[] tmp = Data.split (","); datavideo=tmp[0]; datasound=tmp[1]; Mediator.change ( This); }}
public   class  cddevice  extends  colleague     { private  String data; public  cddevice  (Mediator mediator)    {super  (mediator); } public  String read  () {return  data; } public  void  load         () {data= "video data, audio data" ;    Mediator.change (this ); }}
public   class  graphicscard  extends  colleague  { public  graphicscard  (mediator mediator)    {super  (mediator); } public  void      Vidoeplay  (String data) {System.out.println ( "Play Video:"  +data); }}
publicclass SoundCard extends Colleague {    publicSoundCard(Mediator mediator) {        super(mediator);    }    publicvoidplaySound(String data){        System.out.println("播放音频:"+ data);    }}
Summarize

In object-oriented programming, a class is bound to have dependencies on other classes, and when dependencies are complex, consider using a mediator pattern for decoupling.

Advantages
    • Reduce the complexity of the relationship of the class, the many-to-many conversion into one-to-many, realize decoupling.
    • Conforms to the Dimitri principle (minimum knowledge principle)
Disadvantages
    • Intermediaries have to do a lot of things that can become huge and difficult to maintain.
    • If the relationship is not complex, then the use of intermediaries may make the relationship more complex.

Android design mode (16)-Broker mode

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.