Difference between revisions of "Adapter"

From CDOT Wiki
Jump to: navigation, search
(Adapter UML Diagram)
(Adapter UML Diagram)
Line 23: Line 23:
 
[[Image:Adapter_diag.gif]]
 
[[Image:Adapter_diag.gif]]
  
Image source: http://www.dofactory.com/Patterns/Diagrams/adapter.gif
+
Image source: http://www.dofactory.com/Patterns/Diagrams/adapter.gif (''Copyright 2001 - 2007 Data & Object Factory.'')
 
 
''Copyright 2001 - 2007 Data & Object Factory.''
 
  
 
== Code Examples ==
 
== Code Examples ==

Revision as of 22:33, 25 February 2007

Adapter Design Pattern

Adapter, a Structural Pattern and also referred to as a wrapper, is most commonly used when you want to reuse a class to work with a target class but are constrained by incompatible interfaces. The idea is to convert the interface of a reusable class into an interface that your classes expect.

A good analogy for the Adapter pattern would be the adapter's commonly used to convert the voltage of Canadian/U.S made appliances to be used in different countries. In this case, the appliance (Target) is using the adapter (Adapter) to reuse the socket (Adaptee) already in place.

There are two specific types of adapter patterns - object adapter pattern and class adapter pattern. The latter uses multiple inheritance whereas the former uses an instance of the reuseable class it wants to adapt. Between the two, object adapter pattern is the favored one due to the fact that popular languages such as Java do not support multiple inheritance.

Object Adapter Pattern

This adapter pattern uses an instance of the class it wraps. By using an instance, methods belonging to the wrapped object can be invoked.

Objectadapter diag rueen2.GIF

Class Adapter Pattern

This adapter pattern uses multiple inheritance as a means to wrap the reusable class and use its functionality.

Classadapter diag rueen.GIF

Adapter UML Diagram

UML diagram example of the Adapter pattern. Specifically, the class adapter pattern

Adapter diag.gif

Image source: http://www.dofactory.com/Patterns/Diagrams/adapter.gif (Copyright 2001 - 2007 Data & Object Factory.)

Code Examples

References

Other

Rueen Fiez
BTP600




--Rueen 19:41, 17 January 2007 (EST)