📄 pat4c-1.htm
字号:
<SCRIPT>
function setFocus() {
if ((navigator.appName != "Netscape") && (parseFloat(navigator.appVersion) == 2)) {
return;
} else {
self.focus();
}
}
</SCRIPT><HTML>
<HEAD><TITLE>Composite</TITLE>
<SCRIPT>
function setFocus() {
if ((navigator.appName != "Netscape") && (parseFloat(navigator.appVersion) == 2)) {
return;
} else {
self.focus();
}
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR = #FFFFFF
TEXT = #000000
onLoad="setFocus()";
onLoad="setFocus()";>
<A NAME="top"></A>
<A NAME="Composite"></A>
<A NAME="intent"></A>
<H2><A HREF="#motivation"><IMG SRC="down3-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/gifsb/down3.gif" BORDER=0 ALT="next:
Motivation"></A> Intent</H2>
<A NAME="auto1000"></A>
<P>Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and
compositions of objects uniformly.</P>
<A NAME="motivation"></A>
<H2><A HREF="#applicability"><IMG SRC="down3-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/gifsb/down3.gif" BORDER=0
ALT="next: Applicability"></A> Motivation</H2>
<A NAME="auto1001"></A>
<P>Graphics applications like drawing editors and schematic capture
systems let users build complex diagrams out of simple components.
The user can group components to form larger components, which in
turn can be grouped to form still larger components. A simple
implementation could define classes for graphical primitives such
as Text and Lines plus other classes that act as containers for
these primitives.</P>
<A NAME="recursivecomp-graphics"></A>
<P>But there's a problem with this approach: Code that uses these
classes must treat primitive and container objects differently,
even if most of the time the user treats them identically. Having
to distinguish these objects makes the application more complex.
The Composite pattern describes how to use recursive composition
so that clients don't have to make this distinction.</P>
<A NAME="picture-163c"></A>
<P ALIGN=CENTER><IMG SRC="compo075-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/Pictures/compo075.gif"></P>
<A NAME="auto1002"></A>
<P>The key to the Composite pattern is an abstract class that
represents <EM>both</EM> primitives and their containers. For the
graphics system, this class is Graphic. Graphic declares operations
like Draw that are specific to graphical objects. It also declares
operations that all composite objects share, such as operations
for accessing and managing its children.</P>
<A NAME="auto1003"></A>
<P>The subclasses Line, Rectangle, and Text (see preceding class diagram)
define primitive graphical objects. These classes implement Draw to
draw lines, rectangles, and text, respectively. Since primitive
graphics have no child graphics, none of these subclasses implements
child-related operations.</P>
<A NAME="auto1004"></A>
<P>The Picture class defines an aggregate of Graphic objects. Picture
implements Draw to call Draw on its children, and it implements
child-related operations accordingly. Because the Picture interface
conforms to the Graphic interface, Picture objects can compose other
Pictures recursively.</P>
<A NAME="auto1005"></A>
<P>The following diagram shows a typical composite object structure
of recursively composed Graphic objects:</P>
<A NAME="picture-164o"></A>
<P ALIGN=CENTER><IMG SRC="compo074-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/Pictures/compo074.gif"></P>
<A NAME="applicability"></A>
<H2><A HREF="#structure"><IMG SRC="down3-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/gifsb/down3.gif" BORDER=0 ALT="next:
Structure"></A> Applicability</H2>
<A NAME="auto1006"></A>
<P>Use the Composite pattern when</P>
<UL>
<A NAME="auto1007"></A>
<LI>you want to represent part-whole hierarchies of objects.
<A NAME="auto1008"></A>
<P></P>
<A NAME="auto1009"></A>
<LI>you want clients to be able to ignore the difference between
compositions of objects and individual objects. Clients will treat all
objects in the composite structure uniformly.
</UL>
<A NAME="structure"></A>
<H2><A HREF="#participants"><IMG SRC="down3-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/gifsb/down3.gif" BORDER=0 ALT="next:
Participants"></A> Structure</H2>
<P ALIGN=CENTER><IMG SRC="compo072-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/Pictures/compo072.gif"></P>
<A NAME="auto1010"></A>
<P>A typical Composite object structure might look like this:</P>
<A NAME="composite-inst"></A>
<P ALIGN=CENTER><IMG SRC="compo073-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/Pictures/compo073.gif"></P>
<A NAME="participants"></A>
<H2><A HREF="#collaborations"><IMG SRC="down3-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/gifsb/down3.gif" BORDER=0
ALT="next: Collaborations"></A> Participants</H2>
<UL>
<A NAME="auto1011"></A>
<LI><B>Component</B> (Graphic)
<A NAME="auto1012"></A>
<P></P>
<UL>
<A NAME="auto1013"></A>
<LI>declares the interface for objects in the composition.</LI>
<A NAME="auto1014"></A>
<P><!-- extra space --></P>
<A NAME="auto1015"></A>
<LI>implements default behavior for the interface
common to all classes, as appropriate.</LI>
<A NAME="auto1016"></A>
<P><!-- extra space --></P>
<A NAME="auto1017"></A>
<LI>declares an interface for accessing and managing its child
components.</LI>
<A NAME="auto1018"></A>
<P><!-- extra space --></P>
<A NAME="auto1019"></A>
<LI>(optional) defines an interface for accessing a component's
parent in the recursive structure, and implements it if that's
appropriate.</LI>
</UL>
<A NAME="auto1020"></A>
<P></P>
<A NAME="leaf-part-comp"></A>
<LI><B>Leaf</B> (Rectangle, Line, Text, etc.)
<A NAME="auto1021"></A>
<P></P>
<UL>
<A NAME="auto1022"></A>
<LI>represents leaf objects in the composition. A leaf has no
children.</LI>
<A NAME="auto1023"></A>
<P><!-- extra space --></P>
<A NAME="auto1024"></A>
<LI>defines behavior for primitive objects in the composition.</LI>
</UL>
<A NAME="auto1025"></A>
<P></P>
<A NAME="auto1026"></A>
<LI><B>Composite</B> (Picture)
<A NAME="auto1027"></A>
<P></P>
<UL>
<A NAME="auto1028"></A>
<LI>defines behavior for components having children.</LI>
<A NAME="auto1029"></A>
<P><!-- extra space --></P>
<A NAME="auto1030"></A>
<LI>stores child components.</LI>
<A NAME="auto1031"></A>
<P><!-- extra space --></P>
<A NAME="auto1032"></A>
<LI>implements child-related operations in the Component interface.</LI>
</UL>
<A NAME="auto1033"></A>
<P></P>
<A NAME="auto1034"></A>
<LI><B>Client</B>
<A NAME="auto1035"></A>
<P></P>
<UL>
<A NAME="auto1036"></A>
<LI>manipulates objects in the composition through the
Component interface.</LI>
</UL>
</UL>
<A NAME="collaborations"></A>
<H2><A HREF="#consequences"><IMG SRC="down3-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/gifsb/down3.gif" BORDER=0
ALT="next: Consequences"></A> Collaborations</H2>
<UL>
<A NAME="auto1037"></A>
<LI>Clients use the Component class interface to interact with objects in
the composite structure. If the recipient is a Leaf, then the request
is handled directly. If the recipient is a Composite, then it usually
forwards requests to its child components, possibly performing
additional operations before and/or after forwarding.</LI>
</UL>
<A NAME="consequences"></A>
<H2><A HREF="#implementation"><IMG SRC="down3-1.gif" tppabs="http://ultra/development/DesignPatterns/lowres/gifsb/down3.gif" BORDER=0
ALT="next: Implementation"></A> Consequences</H2>
<A NAME="auto1038"></A>
<P>The Composite pattern</P>
<UL>
<A NAME="auto1039"></A>
<LI>defines class hierarchies consisting of primitive objects
and composite objects. Primitive objects can be composed into more
complex objects, which in turn can be composed, and so on recursively.
Wherever client code expects a primitive object, it can also take a
composite object.</LI>
<A NAME="auto1040"></A>
<P></P>
<A NAME="auto1041"></A>
<LI>makes the client simple.
Clients can treat composite structures and individual objects
uniformly. Clients normally don't know (and shouldn't care) whether
they're dealing with a leaf or a composite component. This simplifies
client code, because it avoids having to write
tag-and-case-statement-style functions over the classes that define
the composition.</LI>
<A NAME="auto1042"></A>
<P></P>
<A NAME="auto1043"></A>
<LI>makes it easier to add new kinds of components.
Newly defined Composite or Leaf subclasses work automatically with
existing structures and client code. Clients don't have to be changed
for new Component classes.</LI>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -