By Judith Bishop
Book Price: $39.99 USD
£24.99 GBP
PDF Price: $27.99
Cover | Table of Contents | Colophon
Program element | Diagram element | Meaning |
|---|---|---|
Class | ![]() | Types and parameters specified when important; access indicated by + (public), (private), and # (protected). |
Interface | ![]() | Name starts with I. Also used for abstract classes. |
Note | ![]() | Any descriptive text. |
Package | ![]() | Grouping of classes and interfaces. |
Inheritance | ![]() | B inherits from A. |
Realization | ![]() | B implements A. |
Association | ![]() | A and B call and access each other's elements. |
Association (one way) | ![]() | A can call and access B's elements, but not vice versa. |
Aggregation | ![]() | A has a B, and B can outlive A. |
Composition | ![]() | A has a B, and B depends on A. |
Decorator object. Given that the number of ways of decorating photos is endless, we can have many such new objects. The beauty of this pattern is that:ComponentOperationIComponent objects that can be replaced (there may be several operations)IComponentComponent is one of these)
ISubjectSubjectProxySubjectRequestSubject that is routed via a proxyProxy, implements the ISubject interface. The Client can use ISubject to abstract away from proxies. However, as we shall see in the upcoming SpaceBook example, sometimes we can dispense with this interface. Each Proxy object maintains a reference to a Subject, which is where the action really takes place. The Proxy performs frontend work. Its value can be enhanced by making the
A and B, implement an interface called the Bridge. The Abstraction includes an attribute of type Bridge but is not otherwise in a relationship with the implementations.
AbstractionOperationclientBridgeAbstraction that might varyImplementationA andMyOpenBook and MySpaceBook need to implement a common interface. We shall call this the Bridge interface. The Abstraction is what is commonly called a Portal. It keeps a copy of the appropriate type of book object and routes each of the methods in the interface to the versions in that book object. The main program then reflects these changes:
class BridgePattern : SpaceBookSystem {
static void Main ( ) {
MySpaceBook me = new MySpaceBook( );
me.Add("Hello world");
me.Add("Today I worked 18 hours");
Portal tom = new Portal(new MyOpenBook("Tom"));
tom.Poke("Judith");
tom.Add("Judith","Poor you");
tom.Add("Hey, I'm also on OpenBook - it was so easy!");
}
}
Portal (the Abstraction) and link up via the Bridge to the new OpenBook access to SpaceBook. The resulting output starts off as before, but then we see that Tom did not have to register or authenticate:Abstraction that was closely connected to the Bridge interface. An equally valid application for the Bridge pattern would be to have an original abstraction in mind and to build it hand-in-hand with the implementations (the "Bridge-down" approach).Decorator | Proxy | Bridge-down | Bridge-up | |
|---|---|---|---|---|
Original | Component | Subject | Abstraction | Implementation |
Interface | IComponent | ISubject | Bridge | Bridge |
New | Decorator | Proxy | Implementation | Abstraction |
Client aggregates | New with interface | New | Original with new | New with original |
Client activates | Original and new | New | Original | New |
Original changed by | Implementing the interface | No change | Aggregating the interface | Implementing the interface |
New classes | Aggregate the interface
Implement the interface | Aggregate the original
Implement the interface | Implement the interface | Aggregate the interface |
Operation routed | From new to original | From new to original | From original to new | From new to original |
Components and Composites of those components. Both types agree to conform to an interface of common operations. Composite objects consist of Components, and in most cases, operations on a Composite are implemented by calling the equivalent operations for its Component objects. See for the UML diagram for this pattern.
Components and Composites of those components. Both types agree to conform to an interface of common operations. Composite objects consist of Components, and in most cases, operations on a Composite are implemented by calling the equivalent operations for its Component objects. See for the UML diagram for this pattern.
IComponentOperationIComponent-conforming objectsComponent
System.Drawing namespace to convert an image to a thumbnail of about 8 KB. This will be the object's intrinsic state, which is small enough to allow all the unique images to remain in memory at any one time. Through the group information, the application can display the images in various combinations. Using disk fetches, it can also show the complete original-sized images.AddSet Home AddPhoto Dinner.jpg AddSet Pets Going down another level AddPhoto Dog.jpg AddPhoto Cat.jpg Find Album Ensures Garden is at same level as Home AddSet Garden AddPhoto Spring.jpg AddPhoto Summer.jpg AddPhoto Flowers.jpg AddPhoto Trees.jpg
Display.)CompositePattern namespace was programmed generically, giving any client the built-in flexibility of instantiating the pattern to suit its needs. The constraints that I mentioned at the time were that a type used for instantiating Component needed to have Equals and ToString methods defined. The Equals method is essential for the correct working of the Find method, but the ToString requirement can be dispensed with if different output is arranged. The FlyweightPattern namespace, on the other hand, contains the IFlyweight and Flyweight types, which are very specific to the photo application used for illustration. (It also contains the FlyweightFactory class, which is not dependent on any class from the client.)
Namespace 1SubsystemFaçadeNamespace 2ClientFaçade in Namespace 1Mechanism | Adapter | Façade |
|---|---|---|
Original | Adaptee | SubsystemA, B, and C |
Interface | ITarget | Façade |
New | Adapter | Operation1 and 2 |
Client | Aggregates ITarget | Accesses Façade |
Client activates | New | New |
Original changed by | No change | No change |
New classes/subsystems | Adapter provides adaptations to their methods | Façade supplies high-level operations |
Operation routed | From new to original | From new to original |