Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

In addition, the coding requirements for the Bridge give you an overall savings in the number of classes written as you increase the number of variations. Table 3-2 shows the number of terminal classes required using strict inheritance compared with the Bridge pattern.

Table 3-2. Class coding requirements

External representations

Implementations

Classes required with inheritance

Classes required with Bridge

2

2

4

4

3

2

6

5

4

4

16

8

5

4

20

9

Comparison of Inheritance Pattern and Bridge Pattern

The Bridge design allows you to multiplex the external representation and internal implementation choices for the component. Multiplexing simply means associating any combination of external and internal elements, to get a greater range of options.

Dividing the component according to its two differentiating concepts also tends to produce a component that is easier to understand and maintain. This is because each inheritance chain revolves around a single concept, abstraction or implementation.

The Bridge is a useful pattern for any system that should display localized flexibility during runtime. An example is GUI systems that must be portable among platforms, requiring that an underlying implementation be applied when the application is started in a different operating system. Systems that change their representation of data depending on locale (for example, altering date, language, or monetary representation) are often good candidates for the Bridge, as well. Similarly, the Bridge is often effective for business entities that can potentially map back to a number of different database sources.

A conceptual example for the Bridge is a technical support switchboard. A number of pre-established phone lines connect a user with a variety of technical support personnel. Naturally, the response will be markedly different depending on the experience of the technical support representative who is on the line. The response probably varies according to the question as well; there will be a somewhat different response to users complaining about broken cup-holders on their PCs.

Implementation

The Bridge class diagram is shown in Figure 3.3.

Figure 3.3. Bridge class diagram

Implementing the Bridge pattern requires the following classes:

Abstraction – The Abstraction class defines the functional abstraction for the Bridge, providing standard behavior and structure. It contains a reference to an Implementation instance. This Implementation instance is usually set with either a setter method (to allow modification at run-time) or through the constructor.

RefineAbstraction – The RefineAbstraction class extends the Abstraction class and provides additional or modified behavior.

104