0% found this document useful (0 votes)
73 views3 pages

Daily Patterns #07

The document discusses the Adapter design pattern, which allows classes with incompatible interfaces to work together by converting the interface of one class into an interface expected by clients of the other. The Adapter pattern provides a wrapper that translates requests from one interface into calls made to the wrapped class. This allows classes to work together that could not otherwise due to mismatched interfaces.

Uploaded by

Vladislav Bauer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views3 pages

Daily Patterns #07

The document discusses the Adapter design pattern, which allows classes with incompatible interfaces to work together by converting the interface of one class into an interface expected by clients of the other. The Adapter pattern provides a wrapper that translates requests from one interface into calls made to the wrapped class. This allows classes to work together that could not otherwise due to mismatched interfaces.

Uploaded by

Vladislav Bauer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

INTENT Convert the interface of a class into another interface clients expect.

Adapter lets classes work together that couldnt otherwise because of incompatible interfaces. Wrap an existing class with a new interface. Impedance match an old component to a new system Adapter design pattern... PROBLEM Reuse has always been painful and elusive. One reason has been the tribulation of designing something new, while reusing something old. There is always something not quite right between the old and the new. It may be physical dimensions or misalignment. It may be timing or synchronization. It may be unfortunate assumptions or competing standards. An off the shelf component offers compelling functionality that you would like to reuse, but its view of the world is not compatible with the philosophy and architecture of the system currently being developed.

It is like the problem of inserting a new three-prong electrical plug in an old two-prong wall outlet some kind of adapter or intermediary is necessary.

Adapter functions as a wrapper or modifier of an existing class. It provides a different or translated view of that class.

Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.

STRUCTURE Below, a legacy Rectangle components display() method expects to receive x, y, w, h parameters. But the client wants to pass upper left x and y and lower right x and y. This incongruity can be reconciled by adding an additional level of indirection i.e. an Adapter object. Check list 1. Identify the players: the component(s) that want to be accommodated (i.e. the client), and the component that needs to adapt (i.e. the adaptee). 2. Identify the interface that the client requires. 3. Design a wrapper class that can impedance match the adaptee to the client 4. The adapter/wrapper class has a instance of the adaptee class. 5. The adapter/wrapper class maps the client interface to the adaptee interface. 6. The client uses (is coupled to) the new interface The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. A socket attaches to a ratchet, provided that the size of the drive is the same. Typical drive sizes in the United States are 1/2 and 1/4. Obviously, a 1/2 drive ratchet will not fit into a 1/4 drive socket unless an adapter is used. A 1/2 to 1/4 adapter has a 1/2 female connection to fit on the 1/2 drive ratchet, and a 1/4 male connection to fit in the 1/4 drive socket.

The Adapter could also be thought of as a wrapper

Adapter makes things work after theyre designed; Bridge makes them work before they are. Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together. Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface. Adapter is meant to change the interface of an existing object. Decorator enhances another object without changing its interface. Decorator is thus more transparent to the application than an adapter is. As a consequence, Decorator supports recursive composition, which isnt possible with pure Adapters. Facade defines a new interface, whereas Adapter reuses an old interface. Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.

This video may be interesting for you

You might also like