23.1. Adapter (GoF)

The problem explored in the previous chapter to motivate the Polymorphism pattern, and its solution, is more specifically an example of the GoF Adapter pattern.

Adapter

Context/Problem

How to resolve incompatible interfaces, or provide a stable interface to similar components with different interfaces?

Solution

Convert the original interface of a component into another interface, through an intermediate adapter object.


To review: The NextGen POS system needs to support several kinds of external third-party services, including tax calculators, credit authorization services, inventory systems, and accounting systems, among others. Each has a different API, which can't be changed.

A solution is to add a level of indirection with objects that adapt the varying external interfaces to a consistent interface used within the application. The solution is illustrated in Figure 23.1.

Figure 23.1. The Adapter pattern.


As illustrated in Figure 23.2, a particular adapter instance will be instantiated for the chosen external service[3], such as SAP for accounting, and will adapt the postSale request to the external interface, such as a SOAP XML interface over HTTPS for an intranet Web service offered by SAP.

[3] In the J2EE Connector Architecture, these adapters to external services are more specifically called resource adapters.

Figure 23.2. Using an Adapter.


UML notation— Note in Figure 23.2 the use of an interface “lollipop” to indicate that the SAPAccountingAdapter instance implements a noteworthy interface.

Polymorphism, Indirection, and Protected Variations (GRASP)

The previous application of the Adapter pattern is a specialization of the GRASP building blocks. It offers Protected Variations from changing external interfaces or third-party packages through the use of an Indirection object that applies interfaces and Polymorphism.

Note that most more complex or specialized patterns can be analyzed in terms of the basic GRASP family. There are hundreds of published design patterns, and although it is helpful to study these to accelerate learning, understanding their underlying basic themes (Protected Variations, Low Coupling, Polymorphism, Indirection, ...) helps us to cut through the myriad details and see the essential “alphabet” of design techniques being applied.

Naming Convention: Embed Pattern Name in Type Name?

Notice that the type names include the pattern name “Adapter.” This is a relatively common style and has the advantage of easily communicating to others reading the code or diagrams what design patterns are being used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.122.124