?? chap4-0.htm
字號:
<HTML><HEAD> <TITLE>Structural Patterns</TITLE><SCRIPT>function setFocus() { if ((navigator.appName != "Netscape") && (parseFloat(navigator.appVersion) == 2)) { return; } else { self.focus(); }}</SCRIPT></HEAD><BODY BGCOLOR = #FFFFFF onLoad="setFocus()";><A NAME="top"></A><P>Structural patterns are concerned with how classes and objects arecomposed to form larger structures.Structural <EM>class</EM> patterns use inheritance to compose interfacesor implementations. As a simple example, consider how multipleinheritance mixes two or more classes into one. The result is a classthat combines the properties of its parent classes. This pattern isparticularly useful for making independently developed class librarieswork together. Another example is the class form of the<A HREF="pat4afs.htm" TARGET="_mainDisplayFrame">Adapter (139)</A> pattern. In general, an adapter makes oneinterface (the adaptee's) conform to another, thereby providing auniform abstraction of different interfaces. A class adapteraccomplishes this by inheriting privately from an adaptee class. Theadapter then expresses its interface in terms of the adaptee's.</P><A NAME="structural1"></A><P>Rather than composing interfaces or implementations, structural <EM>object</EM> patterns describe ways to compose objects to realize newfunctionality. The added flexibility of object composition comes fromthe ability to change the composition at run-time, which is impossiblewith static class composition.</P><A NAME="structural2"></A><P><A HREF="pat4cfs.htm" TARGET="_mainDisplayFrame">Composite (163)</A> is an example of a structural objectpattern. It describes how to build a class hierarchy made up ofclasses for two kinds of objects: primitive and composite. Thecomposite objects let you compose primitive and other compositeobjects into arbitrarily complex structures. In the<A HREF="pat4gfs.htm" TARGET="_mainDisplayFrame">Proxy (207)</A> pattern, a proxy acts as a convenientsurrogate or placeholder for another object. A proxy can be used inmany ways. It can act as a local representative for an object in aremote address space. It can represent a large object that should beloaded on demand. It might protect access to a sensitive object.Proxies provide a level of indirection to specific properties ofobjects. Hence they can restrict, enhance, or alter these properties.</P><A NAME="structural3"></A><P>The <A HREF="pat4ffs.htm" TARGET="_mainDisplayFrame">Flyweight (195)</A> pattern defines a structure forsharing objects. Objects are shared for at least two reasons:efficiency and consistency. Flyweight focuses on sharing for spaceefficiency. Applications that use lots of objects must pay carefulattention to the cost of each object. Substantial savings can be hadby sharing objects instead of replicating them. But objects can beshared only if they don't define context-dependent state. Flyweightobjects have no such state. Any additional information they need toperform their task is passed to them when needed. With nocontext-dependent state, Flyweight objects may be shared freely.</P><A NAME="structural4"></A><P>Whereas Flyweight shows how to make lots of little objects,<A HREF="pat4efs.htm" TARGET="_mainDisplayFrame">Facade (185)</A> shows how to make a single object representan entire subsystem. A facade is a representative for a set ofobjects. The facade carries out its responsibilities by forwardingmessages to the objects it represents. The <A HREF="pat4bfs.htm"TARGET="_mainDisplayFrame">Bridge (151)</A>pattern separates an object's abstraction from its implementation sothat you can vary them independently.</P><A NAME="structural5"></A><P><A HREF="pat4dfs.htm" TARGET="_mainDisplayFrame">Decorator (175)</A> describes how to add responsibilitiesto objects dynamically. Decorator is a structural pattern thatcomposes objects recursively to allow an open-ended number ofadditional responsibilities. For example, a Decorator objectcontaining a user interface component can add a decoration like aborder or shadow to the component, or it can add functionality likescrolling and zooming. We can add two decorations simply by nestingone Decorator object within another, and so on for additionaldecorations. To accomplish this, each Decorator object must conformto the interface of its component and must forward messages to it.The Decorator can do its job (such as drawing a border around thecomponent) either before or after forwarding a message.</P><A NAME="structural6"></A><P>Many structural patterns are related to some degree. We'll discussthese relationships at the end of the chapter.</P><A NAME="last"></A><P><A HREF="#top"><IMG SRC="gifsb/up3.gif" BORDER=0></A><BR><A HREF="pat4afs.htm" TARGET="_mainDisplayFrame"><IMG SRC="gifsb/rightar3.gif" ALIGN=TOP BORDER=0></A> <A HREF="pat4afs.htm" TARGET="_mainDisplayFrame">Adapter</A><BR><A HREF="disc3fs.htm" TARGET="_mainDisplayFrame"><IMG SRC="gifsb/leftarr3.gif" ALIGN=TOP BORDER=0></A> <A HREF="disc3fs.htm" TARGET="_mainDisplayFrame">Discussion of Creational Patterns</A></P></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -