Introduction
This particular pattern seems to very interesting and goes with the core concept of java. I really liked the concept.
In today’s lesson we have discussed about how a pattern is preserved in a catalogue generally. I was curious to find out if all this preserving elements were all necessary to describe the nature of a pattern. So I picked one of the GOF pattern which adapter. I am trying to determine the importance and applicability of these elements in adapter.
Adapter Pattern
Adapters are used to enable objects with different interfaces to communicate with each other.
Adapters come in two flavors, object adapters and class adapters. These two methods embody the difference between the use of inheritance and composition to solve problems.
This pattern is used when;
·User wants to use existing class but the interface doesn't match user needs
·Creating a class that communicates with unrelated classes
There are two types of this pattern; Object Adapter pattern and Class Adapter pattern
Object Adapter Pattern
In this pattern the adapter holds the instance of the class it wraps and makes call to the instance of the wrapped object. [1]
Class Adapter Pattern
The class adapter pattern uses the concept of multiple inheritance. The adapter is created inheriting interfaces from both the interface that is expected and the interface that is pre-existing. [1]
Adapter Pattern Template [2]
Intent
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Also Known As
Wrapper
Motivation
Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application
Adapter Pattern Template [2]
Intent
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Also Known As
Wrapper
Motivation
Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application
We can not change the library interface, since we may not have its source code. Even if we did have the source code, we probably should not change the library for each domain-specific application
Structure
A class adapter uses multiple inheritances to adapt one interface to another:
An object adapter relies on object composition:
Applicability
Use the Adapter pattern when You want to use an existing class, and its interface does not match the one you need. You want to create a reusable class that cooperates with unrelated classes with incompatible interfaces
Implementation Issues
How much adapting should be done?
Simple interface conversion that just changes operation names and order of arguments
Totally different set of operations
Does the adapter provide two-way transparency?
Structure
A class adapter uses multiple inheritances to adapt one interface to another:
An object adapter relies on object composition:
Applicability
Use the Adapter pattern when You want to use an existing class, and its interface does not match the one you need. You want to create a reusable class that cooperates with unrelated classes with incompatible interfaces
Implementation Issues
How much adapting should be done?
Simple interface conversion that just changes operation names and order of arguments
Totally different set of operations
Does the adapter provide two-way transparency?
A two-way adapter supports both the Target and the Adaptee interface. It allows an adapted object (Adapter) to appear as an Adaptee object or a Target
Adapter Pattern Example
public class ButtonDemo {
public ButtonDemo() {
Button button = new Button("Press me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doOperation();
}
});
}
public void doOperation() { whatever }
}
Button objects expect to be able to invoke the actionPerformed() method on their associated ActionListener objects. But the ButtonDemo class does not have this method! It really wants the button to invoke its doOperation() method. The anonymous inner class we instantiated acts as an adapter object, adapting ButtonDemo to ActionListener!
Conclusion
Adapter Pattern Example
public class ButtonDemo {
public ButtonDemo() {
Button button = new Button("Press me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doOperation();
}
});
}
public void doOperation() { whatever }
}
Button objects expect to be able to invoke the actionPerformed() method on their associated ActionListener objects. But the ButtonDemo class does not have this method! It really wants the button to invoke its doOperation() method. The anonymous inner class we instantiated acts as an adapter object, adapting ButtonDemo to ActionListener!
Conclusion
Class adapters are a lot flexible than object adapters in that they involve fewer classes and are useful if total decoupling of the client and adaptee is not needed.
References:
2.Design Patterns: Elements of Reusable Object-Oriented Software By Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
4 comments:
Good day
Thanks for the effort in explaining what an adapter pattern is all about. i think you should have given in your introduction some simple real life scenario of how basic adapters are used to make it much understandable for a less technical person.
Cheers
Marville
Cheers
Thanks Marville, for your valueable comment. I will take it in to considerations for next blogs.
cheers
imtiaz
Adapter involves implementing one or more interfaces. Given that Java does not support multiple inheritances what is the behavior when two interfaces define a method with the same signature?
you have really gone into depths about explaining what the adapter patterns does although if you applied a real life example such as two differnt types of incomaptable gadgets with the way you have used to described the pattern would of certainly given the reader the full picture.
Your sample code on button is good and i am tried to implemnt it...
Post a Comment