Suppose we are designing a game room with two buttons and a treasure chest in it. If we press button 1, both button 2 and treasure chest are unavailable. If we press button 2, button 1 and treasure chest are unavailable. If we open the treasure chest, button 1 cannot be pressed.
In the above example, we can see that there is a dependency between the two buttons and a chest, and the status between them is related. Assume that the Button class is called "Button" and the treasure Chest class is called "Chest". To avoid the connection between buttons and Chest, we should establish an intermediary class. When the status of the Button and Chest changes, the intermediary should be informed that it is up to the intermediary to decide how to change the attributes of these buttons and Chest.
Shows a typical intermediary mode. Mediator is the intermediary's abstraction, and colleque is the object of joint collaboration, such as Button and Chest. We use an intermediary object to encapsulate the interaction of a series of objects. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.
The example at the beginning of this article is as follows:
Interface Director {void switchTriggered (Switch sw); void createSwitches ();} class roomdireimplements Director {private Button button1; private Button button2; private Chest chest; public Button getButton1 () {return button1;} public void setButton1 (Button button1) {this. button1 = button1;} public Button getButton2 () {return button2;} public void setButton2 (Button button2) {this. button2 = Button2;} public Chest getChest () {return chest;} public void setChest (Chest chest) {this. chest = chest;} public void switchTriggered (Switch sw) {if (sw = button1) {System. out. println ("button 1 pressed"); button2.setEnabled (false); chest. setEnabled (false);} else if (sw = button2) {System. out. println ("button 2 pressed"); button1.setEnabled (false); chest. setEnabled (false);} else if (sw = chest) {System. out. p Rintln ("box opened"); button1.setEnabled (false) ;}} public void createSwitches () {button1 = new Button (this); button2 = new Button (this ); chest = new Chest (this) ;}} abstract class Switch {private boolean enabled = true; public boolean isEnabled () {return enabled;} public void setEnabled (boolean enabled) {this. enabled = enabled;} public Switch (Director ctor) {this. director = director;} priva Te Director ctor; void triggered () {director. switchTriggered (this) ;}} class Chest extends Switch {public Chest (Director ctor dire) {super (director);} private boolean opened; public boolean isOpened () {return opened ;} public void open () {if (isEnabled () {opened = true; triggered ();} else {System. out. println ("the box is unavailable! ") ;}} Class Button extends Switch {public Button (Director dire) {super (director);} private boolean push; public boolean isPush () {return push ;} public void push () {if (isEnabled () {this. push = true; triggered ();} else {System. out. println ("button unavailable! ") ;}} Public class Mediator {public static void main (String [] args) {RoomDirector room = new RoomDirector (); room. createSwitches (); room. getChest (). open (); room. getButton1 (). push (); room. getButton2 (). push ();}}
Director is an abstraction of an intermediary class, which derives from roomdire. RoomDirectory contains three objects for collaboration, two buttons and one Chest, which all inherit from the Switch class for collaboration. When a Button is pressed or Chest is opened, the trigger method of the base class is called to notify RoomDirector -- "the status of an object has changed ", call the switchTriggered method in roomdireto operate the three objects. In the main method, we open the box first, and then press button1. In this case, button1 should be invalid, and then press button2, which is effective, the result of the program is:
Box opened
The button is unavailable!
Button 2 is pressed
It can be seen that the interaction between all our objects is written in the switchTriggered method. In the RoomDirector intermediary class, the more objects you need to manage, the more complex it becomes.
As mentioned in GoF's design patterns, the controls in the dialog box usually have dependencies. If the content of a specific text box is empty, a button is dimmed; A table selected from a column in the list box may change the content of a text box ...... Then, we can create an intermediary class for these mutually dependent controls to make the coupling relationship between them loose. To put it bluntly, when the status of mutually dependent controls changes, a message is sent to the same object, and this object is used to reset the status of these dependent controls, this is the central idea of the intermediary model.