?? pat5e.htm
字號:
<HTML><HEAD> <TITLE>Mediator</TITLE><SCRIPT>function setFocus() { if ((navigator.appName != "Netscape") && (parseFloat(navigator.appVersion) == 2)) { return; } else { self.focus(); }}</SCRIPT></HEAD><BODY BGCOLOR = #FFFFFFonLoad="setFocus()";><A NAME="top"></A><A NAME="Mediator"></A><A NAME="intent"></A><H2><A HREF="#motivation"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Motivation"></A> Intent</H2> <A NAME="auto1000"></A><P>Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring toeach other explicitly, and it lets you vary their interactionindependently.</P><A NAME="motivation"></A><H2><A HREF="#applicability"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Applicability"></A> Motivation</H2> <A NAME="auto1001"></A><P>Object-oriented design encourages the distribution of behavioramong objects. Such distribution can result in an object structurewith many connections between objects; in the worst case, every objectends up knowing about every other.</P><A NAME="auto1002"></A><P>Though partitioning a system into many objects generally enhancesreusability, proliferating interconnections tend to reduce it again.Lots of interconnections make it less likely that an object can workwithout the support of others—the system acts as though it weremonolithic. Moreover, it can be difficult to change the system'sbehavior in any significant way, since behavior is distributed amongmany objects. As a result, you may be forced to define many subclassesto customize the system's behavior.</P><A NAME="auto1003"></A><P>As an example, consider the implementation of dialog boxes in agraphical user interface. A dialog box uses a window to present acollection of widgets such as buttons, menus, and entry fields, asshown here:</P><P ALIGN=CENTER><IMG SRC="Pictures/fontc047.gif"></P><A NAME="listbox"></A><P>Often there are dependencies between the widgets in the dialog. Forexample, a button gets disabled when a certain entry field is empty.Selecting an entry in a list of choices called a <STRONG>list box</STRONG>might change the contents of an entry field. Conversely, typing textinto the entry field might automatically select one or morecorresponding entries in the list box. Once text appears in the entryfield, other buttons may become enabled that let the user do somethingwith the text, such as changing or deleting the thing to which it refers.</P><A NAME="auto1004"></A><P>Different dialog boxes will have different dependencies betweenwidgets. So even though dialogs display the same kinds of widgets,they can't simply reuse stock widget classes; they have to becustomized to reflect dialog-specific dependencies. Customizing themindividually by subclassing will be tedious, since many classes areinvolved.</P><A NAME="def-media"></A><P>You can avoid these problems by encapsulating collective behavior in aseparate <STRONG>mediator</STRONG> object. A mediator is responsible forcontrolling and coordinating the interactions of a group of objects.The mediator serves as an intermediary that keeps objects in the groupfrom referring to each other explicitly. The objects only know themediator, thereby reducing the number of interconnections.</P><A NAME="fontdlogdirector"></A><P>For example, <STRONG>FontDialogDirector</STRONG> can be the mediatorbetween the widgets in a dialog box. A FontDialogDirector object knowsthe widgets in a dialog and coordinates their interaction. It acts asa hub of communication for widgets:</P><A NAME="mediator-eg-obj"></A><P ALIGN=CENTER><IMG SRC="Pictures/media033.gif"></P><A NAME="auto1005"></A><P>The following interaction diagram illustrates how the objects cooperate tohandle a change in a list box's selection:</P><A NAME="mediator-id"></A><P ALIGN=CENTER><IMG SRC="Pictures/media031.gif"></P><A NAME="auto1006"></A><P>Here's the succession of events by which a list box's selection passesto an entry field:</P><OL><A NAME="auto1007"></A><LI>The list box tells its director that it's changed.</LI><A NAME="auto1008"></A><P></P><A NAME="auto1009"></A><LI>The director gets the selection from the list box.</LI><A NAME="auto1010"></A><P></P><A NAME="auto1011"></A><LI>The director passes the selection to the entry field.</LI><A NAME="auto1012"></A><P></P><A NAME="auto1013"></A><LI>Now that the entry field contains some text, the directorenables button(s) for initiating an action (e.g., "demibold," "oblique").</LI></OL><A NAME="auto1014"></A><P>Note how the director mediates between the list box and the entry field.Widgets communicate with each other only indirectly, through thedirector. They don't have to know about each other; all they know is thedirector. Furthermore, because the behavior is localized in one class,it can be changed or replaced by extending or replacing that class.</P><A NAME="auto1015"></A><P>Here's how the FontDialogDirector abstraction can be integrated into aclass library:</P><A NAME="275c"></A><P ALIGN=CENTER><IMG SRC="Pictures/media034.gif"></P><A NAME="auto1016"></A><P>DialogDirector is an abstract class that defines the overall behavior ofa dialog. Clients call the ShowDialog operation to display the dialog onthe screen. CreateWidgets is an abstract operation for creating thewidgets of a dialog. WidgetChanged is another abstract operation;widgets call it to inform their director that they have changed.DialogDirector subclasses override CreateWidgets to create the properwidgets, and they override WidgetChanged to handle the changes.</P><A NAME="applicability"></A><H2><A HREF="#structure"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Structure"></A> Applicability</H2> <A NAME="auto1017"></A><P>Use the Mediator pattern when</P><UL><A NAME="auto1018"></A><LI>a set of objects communicate in well-defined but complex ways. Theresulting interdependencies are unstructured and difficult tounderstand.</LI><A NAME="auto1019"></A><P></P><A NAME="auto1020"></A><LI>reusing an object is difficult because it refers to and communicateswith many other objects.</LI><A NAME="auto1021"></A><P></P><A NAME="auto1022"></A><LI>a behavior that's distributed between several classes should becustomizable without a lot of subclassing.</LI></UL><A NAME="structure"></A><H2><A HREF="#participants"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Participants"></A> Structure</H2> <P ALIGN=CENTER><IMG SRC="Pictures/mediator.gif"></P><A NAME="auto1023"></A><P>A typical object structure might look like this:</P><P ALIGN=CENTER><IMG SRC="Pictures/media030.gif"></P><A NAME="participants"></A><H2><A HREF="#collaborations"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Collaborations"></A> Participants</H2><UL><A NAME="auto1024"></A><LI><B>Mediator</B> (DialogDirector)</LI><A NAME="auto1025"></A><P></P> <UL> <A NAME="auto1026"></A><LI>defines an interface for communicating with Colleague objects.</LI> </UL><A NAME="auto1027"></A><P></P><A NAME="auto1028"></A><LI><B>ConcreteMediator</B> (FontDialogDirector)<A NAME="auto1029"></A><P></P> <UL> <A NAME="auto1030"></A><LI>implements cooperative behavior by coordinating Colleague objects.</LI> <A NAME="auto1031"></A><P><!-- extra space --></P> <A NAME="auto1032"></A><LI>knows and maintains its colleagues.</LI> </UL><A NAME="auto1033"></A><P></P><A NAME="auto1034"></A><LI><B>Colleague classes</B> (ListBox, EntryField)<A NAME="auto1035"></A><P></P> <UL> <A NAME="auto1036"></A><LI>each Colleague class knows its Mediator object.</LI> <A NAME="auto1037"></A><P><!-- extra space --></P> <A NAME="auto1038"></A><LI>each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.</LI> </UL></UL><A NAME="collaborations"></A><H2><A HREF="#consequences"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Consequences"></A> Collaborations</H2><UL><A NAME="auto1039"></A><LI>Colleagues send and receive requests from a Mediator object. Themediator implements the cooperative behavior by routing requestsbetween the appropriate colleague(s).</LI></UL><A NAME="consequences"></A><H2><A HREF="#implementation"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Implementation"></A> Consequences</H2> <A NAME="auto1040"></A><P>The Mediator pattern has the following benefits and drawbacks:</P><OL><A NAME="auto1041"></A><LI><EM>It limits subclassing.</EM>A mediator localizes behavior that otherwise would be distributed amongseveral objects. Changing this behavior requires subclassing Mediatoronly; Colleague classes can be reused as is.</LI><A NAME="auto1042"></A><P></P><A NAME="auto1043"></A><LI><EM>It decouples colleagues.</EM>A mediator promotes loose coupling between colleagues. You can varyand reuse Colleague and Mediator classes independently.</LI><A NAME="auto1044"></A><P></P><A NAME="auto1045"></A><LI><EM>It simplifies object protocols.</EM>A mediator replaces many-to-many interactions with one-to-manyinteractions between the mediator and its colleagues. One-to-manyrelationships are easier to understand, maintain, and extend.</LI><A NAME="auto1046"></A><P></P><A NAME="auto1047"></A><LI><EM>It abstracts how objects cooperate.</EM>Making mediation an independent concept and encapsulating it in anobject lets you focus on how objects interact apart from theirindividual behavior. That can help clarify how objects interact in asystem.</LI><A NAME="auto1048"></A><P></P><A NAME="auto1049"></A><LI><EM>It centralizes control.</EM>The Mediator pattern trades complexity of interaction for complexity inthe mediator. Because a mediator encapsulates protocols, it can becomemore complex than any individual colleague. This can make the mediatoritself a monolith that's hard to maintain.</LI></OL><A NAME="implementation"></A><H2><A HREF="#samplecode"><IMG SRC="gifsb/down3.gif" BORDER=0 ALT="next: Sample Code"></A> Implementation</H2> <A NAME="auto1050"></A><P>The following implementation issues are relevant to the Mediatorpattern:</P><OL><A NAME="media-omit-abs"></A><LI><EM>Omitting the abstract Mediator class.</EM>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -