C# 3.0

Page 69

Use Bridge is a very simple, but very powerful pattern. Given a single implementation, we can add a second one together with a Bridge and an Abstraction and achieve considerable generality over the original design. A well-quoted use of the Bridge pattern is in graphics, where different displays have different capabilities and drivers. These would be the implementations of the Bridge pattern, and the Bridge would be an interface of their essential capabilities. The Client calls the Abstraction to display something, and the Abstraction can examine the properties of the one or more Bridge instances (drivers) it is holding and select the most appropriate one for the task. Use the Bridge pattern when… You can: • Identify that there are operations that do not always need to be implemented in the same way. You want to: • Completely hide implementations from clients. • Avoid binding an implementation to an abstraction directly. • Change an implementation without even recompiling an abstraction. • Combine different parts of a system at runtime.

Exercise 1. Although some limited operations can be added to OpenBook, the fact that some fundamental ones, such as the page header, are embedded in the private SpaceBook class makes real change difficult. Assuming that you can negotiate an upgrade with the developers of SpaceBook, make a proposal, including a UML diagram, for the long-term design of an extensible system. Implement it.

Pattern Comparison The careful reader might have noticed that the three patterns described in this chapter seem to offer much the same service. At a high level, they are all helping to extend classes in novel ways, and they all provide alternatives to inheritance. A summary of when each pattern might be used was provided at the end of each section. Because this is also a programming book, we’ll now summarize the object mechanisms the patterns use (see Table 2-2) and draw conclusions about their comparative operation. This summary is based on the UML diagrams and theory code for each of the patterns. In each pattern, we can identify four roles, which can fall under the headings original, new, interface, and client. The actual names given to these meta-roles are shown in the first three rows of the table. Note that the Bridge pattern can work in two ways. In our examples, we used the “Bridge-up” option. We assumed that we had 46 |

Chapter 2: Structural Patterns: Decorator, Proxy, and Bridge


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.