Adapter design pattern (II)
8 March 2008This post is in continuation to Adapter design pattern (I). Do read that before this one.
Choosing between Facade and Adapter
There is a definite difference between the Facade and Adapter. A facade is a better choice when one wants an easier or simpler interface to work with. Where as the Adapter Design Pattern is used when wrapper must support a polymorphic behavior and wrapper must respect a particular interface.
The example below demonstrates the use of Adapter Design Pattern. The adaptee class is DoubleLinkedList. The client class DoubleLinkedListPushPull instanciates adapter objects.
interface PushPull<T> { void push (T o); T pop (); T top (); } //adaptee class class DoubleLinkedList<T> { public void insert (DNode pos, T o) { // code to insert} public void remove (DNode pos) { // code to remove} public void insertHead (T o) { // code to insert head} public void insertTail (T o) { // code to insert tail} public T removeHead () { // code to remove head} public T removeTail () { // code to remove tail} public T getHead () { // code to get head} public T getTail () { // code to tail} } class DoubleLinkedListPushPull<T> extends DoubleLinkedList<T> implements PushPull<T> { public void push (T o) { insertTail (o); } public T pop () { return removeTail (); } public T top () { return getTail (); } }
Related Posts:
- Adapter design pattern
- The Facade Design Pattern (I)
- The Strategy Design Pattern in Java(I)
- Singleton - Creational Design Pattern (I)
- The Observer Design Pattern(I)
- Design Patterns - Basics (I)
- The Strategy Design Pattern in Java(II)
- The Facade Design Pattern (II)
- Introduction to Model View Controller Architecture (II)
- Singleton - Creational Design Pattern (II)
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Adapter design pattern (II)