CSC/ECE 517 Fall 2012/ch2b 2w47 am: Difference between revisions
(18 intermediate revisions by 2 users not shown) | |||
Line 19: | Line 19: | ||
<br><br><br> | <br><br><br> | ||
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, ( | So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (probably, the main purpose of the wiki page). | ||
<br> | <br> | ||
<br> | <br> | ||
Line 29: | Line 29: | ||
<b>Summary:</b><br>In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).<br> The various Adapter pattern structures are as follows | <b>Summary:</b><br>In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).<br> The various Adapter pattern structures are as follows | ||
* Object Adapter pattern:<br> | * <b>Object Adapter pattern:</b><br> | ||
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.<br> | In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.<br> | ||
* Class Adapter pattern:<br> | * <b>Class Adapter pattern:</b><br> | ||
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.<br> | This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.<br> | ||
* A further form of runtime Adapter pattern:<br> | * <b>A further form of runtime Adapter pattern:</b><br> | ||
This section provides a good example for runtime Adapter pattern.<br> | This section provides a good example for runtime Adapter pattern.<br> | ||
* Implementation:<br> | * <b>Implementation:</b><br> | ||
Provides a basic skeleton of how to implement the adapter pattern. | Provides a basic skeleton of how to implement the adapter pattern. | ||
<br><br> | <br><br> | ||
Line 47: | Line 47: | ||
<b>Summary:</b><br>Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access. | <b>Summary:</b><br>Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access. | ||
* Role:<br> | * <b>Role:</b><br> | ||
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries<br> | The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries<br> | ||
* Illustration:<br> | * <b>Illustration:</b><br> | ||
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.<br> | Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.<br> | ||
* Design:<br> | * <b>Design:</b><br> | ||
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.<br> | The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.<br> | ||
* Implementation:<br> | * <b>Implementation:</b><br> | ||
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other. | This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other. | ||
* Two way adapters and Pluggable adapters:<br> | * <b>Two way adapters and Pluggable adapters:</b><br> | ||
These sections describes the other kinds of adapter with appropriate examples. | These sections describes the other kinds of adapter with appropriate examples. | ||
<br><br> | <br><br> | ||
<b>Pros:</b> This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.<br> | <b>Pros:</b> This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.<br> | ||
<b>Cons:</b> The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do | <b>Cons:</b> The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do..."eyebrows raised" ). | ||
<br> | <br> | ||
<br> | <br> | ||
Line 93: | Line 93: | ||
<b>Summary:</b><br>This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system. | <b>Summary:</b><br>This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system. | ||
* Background:<br> | * <b>Background:</b><br> | ||
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.<br> | The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.<br> | ||
* Design and implementation<br> | * <b>Design and implementation</b><br> | ||
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram: | It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram: | ||
[[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]] | [[File:AdapterClassOnCodeProject.JPG|thumb|500px|center|A real world example for the Adapter Pattern.]] | ||
Line 104: | Line 104: | ||
<b>Cons:</b> A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand. | <b>Cons:</b> A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand. | ||
<br><br> | <br><br> | ||
Alternately, beginners can also refer the webpage on [http://www.codeproject.com/Articles/186003/Adapter-Design-Pattern Adapter Design Pattern] further provided by Codeproject.com <br><br> | |||
=== [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] === | === [http://csc.columbusstate.edu/woolbright/java/adapter.html Adpater Pattern on Columbus State University website] === | ||
Line 113: | Line 115: | ||
The Adapter Pattern converts the interface of a class into another interface the client expects. | The Adapter Pattern converts the interface of a class into another interface the client expects. | ||
Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. | Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. | ||
<br> | |||
* <b>Object-Oriented Adapters:</b><br> | * <b>Object-Oriented Adapters:</b><br> | ||
# Assume your existing system expects interface A. | # Assume your existing system expects interface A. | ||
# You need the system to work with a vendor class that has interface C. | # You need the system to work with a vendor class that has interface C. | ||
# The solution is an adapter class with interface A for the existing system, and interface C for working with the Vendor class. | # The solution is an adapter class with interface A for the existing system, and interface C for working with the Vendor class. | ||
<br> | |||
* <b>The Duck Interface Example:</b><br> The website provides a good example of the Adapter Design Pattern. The example is as follows: <br> The code shown below provides the Duck Interface:<br> | * <b>The Duck Interface Example:</b><br> The website provides a good example of the Adapter Design Pattern. The example is as follows: <br> The code shown below provides the Duck Interface:<br> | ||
Line 126: | Line 128: | ||
} | } | ||
:It is implemented by the MallardDuck class as shown below: | |||
public class MallardDuck implements Duck { | public class MallardDuck implements Duck { | ||
Line 140: | Line 140: | ||
} | } | ||
:There is another interface called the Turkey Interface: | |||
public interface Turkey { //Turkeys don't quack | public interface Turkey { //Turkeys don't quack | ||
Line 149: | Line 147: | ||
} | } | ||
:The turkey interface is implemented by the wildTurkey class as shown below: | |||
public class WildTurkey implements Turkey { | public class WildTurkey implements Turkey { | ||
Line 163: | Line 159: | ||
} | } | ||
:And finally comes our <i>showstopper</i>, the Turkey Adapter code as shown below: | |||
public class TurkeyAdapter implements Duck { | public class TurkeyAdapter implements Duck { | ||
Line 185: | Line 179: | ||
} | } | ||
:The website also provides a good tester to test the Adapter code. Please refer to the link to see the test driving code. <br> | |||
<br> | <br> | ||
* <b>Real world Adapters:</b><br> Following are the examples of real world adapters: | * <b>Real world Adapters:</b><br> Following are the examples of real world adapters: | ||
# The old Java Enumeration class had a couple of methods: | # The old Java Enumeration class had a couple of methods: | ||
Line 199: | Line 190: | ||
## remove() | ## remove() | ||
This section provides an <i>EnumerationIterator</i> adapter class that implements the Iterator. Please refer to the link to see the <i>EnumerationIterator</i> Adapter class. | |||
<br> | |||
<br> | |||
<b>Pros:</b><br> | |||
# A very good example of Adapter Pattern using the Duck Interface, Turkey Interface and the TurkeyAdapter pattern. ( Gosh! Too much of a Turkey used, reminds of Thanksgiving! ). | |||
# Also, the section on real world adapters compliments further understanding of Adapter pattern implementation. Good for Intermediate and Expert users of Design patterns<br> | |||
<b>Cons:</b><br> | |||
# Does not contains enough content to understand Adapter pattern from scratch. | |||
# Also, not suitable for beginners. Though the code provided is lucid and self explanatory, it is not complimented with explanatory texts and this may cause some problems for beginners to understand the code. | |||
# One more thing to observe here is the font size! ( Not good for readers who are adapted to small or medium font size ). | |||
<br> | |||
=== [http://www.blackwasp.co.uk/Adapter.aspx Adapter Pattern on blackwasp.com] === | |||
<br><b>Topics Covered:</b><br>What is adapter pattern, implementing adapter pattern, example of adapter pattern. | |||
<b>Summary:</b><br>This page explained the adapter pattern in brief. It gives a good example for implementing the adapter pattern and testing the adapter. The adapter pattern is a Gang of Four design pattern. Its a structural pattern as it defines a manner for creating relationships between classes. The adapter design pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client. | |||
* <b>What is adapter pattern:</b><br> | |||
:The adapter pattern is introduced with the example of personnel system and intranet system. It helps build up the example in the implementation part<br> | |||
* <b>Implementation:</b><br> | |||
:An implementation of the adapter with client, target, adaptee and adapter is shown. | |||
[[File:AdapterForBlackWasp.png|thumb|350px|center|Adapter pattern implementation.]] | |||
<br> | |||
* <b>Example and testing adapter:</b><br> | |||
:The example of intranet and personnel system discussed in the introduction is implemented in C# code. Also how to test the adapter is shown. | |||
<br> | |||
<b>Pros:</b> This page is useful for beginners. It provides a good example and shows how to implement the adapter.<br> | |||
<b>Cons:</b> It focuses more on the example than on the theory behind adapter pattern. | |||
<br> | |||
<br> | <br> | ||
=== [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] === | === [http://sourcemaking.com/design_patterns/adapter Adpater Pattern on Sourcemaking.com] === | ||
Line 208: | Line 229: | ||
<b>Summary:</b><br>Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages. | <b>Summary:</b><br>Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages. | ||
* Intent:<br> | * <b>Intent:</b><br> | ||
The need and purpose of adapter pattern.<br> | The need and purpose of adapter pattern.<br> | ||
* Problem and Discussion:<br> | * <b>Problem and Discussion:</b><br> | ||
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. This section tells the problems and how adapter pattern helps solve them.<br> | 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. This section tells the problems and how adapter pattern helps solve them.<br> | ||
* Structure:<br> | * <b>Structure:</b><br> | ||
Class diagrams showing the different structures of adapter pattern.<br> | Class diagrams showing the different structures of adapter pattern.<br> | ||
* Example:<br> | * <b>Example:</b><br> | ||
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. <br> | 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. <br> | ||
* Check list and Rule of thumb:<br> | * <b>Check list and Rule of thumb:</b><br> | ||
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.<br> | These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.<br> | ||
* Adapter Code examples:<br> | * <b>Adapter Code examples:</b><br> | ||
Links to examples of Adapter pattern implemented in various languages. | Links to examples of Adapter pattern implemented in various languages. | ||
<br><br> | <br><br> |
Latest revision as of 04:16, 20 November 2012
Adapter Pattern, a directory of sites
This wiki page provides a directory of sites for Adapter Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Adapter Pattern.
Introduction
Sometimes, objects just don't fit together as they should. A class may have changed, or an object turns out to be just too difficult to work with. At such times, adapter design pattern comes to the rescue. Adapter pattern lets you adapt what an object or class has to offer so that another object or class can make use of it. It also helps when classes can't work together because of incompatible interfaces.
Since we are huge fans of visual approach, lets understand the adapter pattern with the help of a diagram. Hold on! Before going in to the UML diagram ( which is kind of less attractive, right? May be .. ) lets look at the figure below which is more appealing.
The above example helps readers quickly get across the intent of the pattern than the UML diagram shown below. Looking at the UML diagram now, can help the readers to learn about the precise relationships between the classes in adapter pattern and it makes a lot more sense!
So, this is all what we had to talk about Adapter Pattern. Lets look at other websites and see what they say about Adapter pattern, (probably, the main purpose of the wiki page).
Directory of sites
Adapter Pattern on wikipedia
Topics Covered:
Lists various structures of Adapter Pattern viz., Object Adapter Pattern, Class Adapter Pattern, Runtime Adapter pattern and implementation of Adapter pattern. It provides a few examples for the above structures.
Summary:
In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).
The various Adapter pattern structures are as follows
- Object Adapter pattern:
In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.
- Class Adapter pattern:
This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing.
- A further form of runtime Adapter pattern:
This section provides a good example for runtime Adapter pattern.
- Implementation:
Provides a basic skeleton of how to implement the adapter pattern.
Pros: This wikipedia page works if readers want a quick review of the adapter pattern. Also it provides good references to read more about the Adapter pattern.
Cons: It doesn't helps to understand the Adapter pattern in depth. The examples provided are not easy to comprehend ( unless you are an experienced developer and not a beginner! ). Its not a good page to refer for the beginners.
Adpater Pattern on MSDN Microsoft Website
Topics Covered:
Role of adapter pattern, Illustrations or examples where adapter pattern is used ,Design and Implementation of adapter pattern, few more kinds of adapters.
Summary:
Adapter pattern is a versatile pattern that joins together types that were not designed to work with each other. It is one of those patterns that comes in useful when dealing with legacy code-i.e., code that was written a while ago and to which one might not have access.
- Role:
The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements. It is especially useful for off-the-shelf code, for toolkits, and for libraries
- Illustration:
Adapter pattern illustration-migration of Mac OS X from a 1998 PowerPC-based iMac to a 2007 Intel-based iMac. This section shows how the adapter pattern has been used to enable Mac OS migration.
- Design:
The Adapter pattern's important contribution is that it promotes programming to interfaces. The Client works to a domain-specific standard, which is specified in the ITarget interface. An Adaptee class provides the required functionality, but with a different interface. The Adapter implements the ITarget interface and routes calls from the Client through to the Adaptee, making whatever changes to parameters and return types are necessary to meet the requirements. A Target class that implements the ITarget interface directly could exist, but this is not a necessary part of the pattern. In any case, the Client is aware only of the ITarget interface, and it relies on that for its correct operation.
- Implementation:
This section provides an example in C# and how the Adapter, Adaptee, Target and Client all communicate with each other.
- Two way adapters and Pluggable adapters:
These sections describes the other kinds of adapter with appropriate examples.
Pros: This page gives an in-depth explanation of the adapter pattern. It shows how the adapter pattern is implemented in C#. The real world examples makes it easy to grasp the topic.
Cons: The page might be too long for a quick review. Though it is really good for a beginner, an Intermediate or an expert person in design pattern may not actually want to wade through the entire concept in such a depth. ( unless the person doesn't have much work to do..."eyebrows raised" ).
Adpater Pattern on OODesign.com
Topics Covered:
Motivation for using Adapter pattern, intent behind using the adapter pattern, a UML diagram to explain the adapter pattern ( like we did above, yeah! ) to start with. It also covers the application and implementation of Adapter patterns. Moreover, it provides some really nice examples and specific problems where adapter pattern becomes a good fit to use.
Summary:
The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.
What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...
- Intent
- Convert the interface of a class into another interface clients expect
- Adapter lets classes work together, that could not otherwise because of incompatible interfaces
- Implementation
Explains the implementation of the Adapter pattern with the help of a UML diagram.
- Applicability and Examples
Adapters are encountered everywhere. From real world adapters to software adapters.
- Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...
- Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
- Specific problem and Implementation
- Objects Adapters - Based on Delegation
It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee). - Class Adapters - Based on (Multiple) Inheritance
Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition. - Adapter Pattern and Strategy Pattern
Cases when Adapter Pattern can be a better option instead of Strategy pattern ( ok, now that sounds something interesting, doesn't it?)
Pros: Helps to learn Adapter pattern in detail. Provides good examples to understand how the pattern works and also give suitable cases where adapter design pattern is most applicable. Good for all readers ( beginners, intermediate and experts )
Cons: Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.
Adpater Pattern on Codeproject.com
Topics Covered:
Background, Introduction to adapter pattern and its structure, class diagram and implementation of adapter pattern for a scenario.
Summary:
This page uses a running example in the background to discuss the design pattern and then further provides a design pattern implementation for that particular example. Moreover, it explains the importance of Adapter Design in system integration when other components have to be adapted by the existing system.
- Background:
The webpage continues using the running example to describe the earlier design patterns and further gives a scenario to introduce the adapter pattern.
- Design and implementation
It gives a class diagram describing the adapter pattern for the example in the background section. Also, this section covers the explanation and implementation of all the classes and interfaces mentioned in the below class diagram:
Pros: This website is useful for intermediate users of design patterns. Some preexisting knowledge of adapter pattern prior to reading this page can help to further understand the topic. The example provided gives a clear view of the design pattern.
Cons: A beginner might find not find this page a good base line to start. The code on the page might be difficult for a novice programmer to understand.
Alternately, beginners can also refer the webpage on Adapter Design Pattern further provided by Codeproject.com
Adpater Pattern on Columbus State University website
Topics Covered:
Introduction to Adapter pattern, Object-Oriented Adapters, an example which implements the Adapter pattern and real world adapters.
Summary:
- Intrduction:
The adapter pattern changes the look of an interface. It takes an interface and adapts it to one the client is expecting.
The Adapter Pattern converts the interface of a class into another interface the client expects. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
- Object-Oriented Adapters:
- Assume your existing system expects interface A.
- You need the system to work with a vendor class that has interface C.
- The solution is an adapter class with interface A for the existing system, and interface C for working with the Vendor class.
- The Duck Interface Example:
The website provides a good example of the Adapter Design Pattern. The example is as follows:
The code shown below provides the Duck Interface:
public interface Duck { public void quack(); public void fly(); }
- It is implemented by the MallardDuck class as shown below:
public class MallardDuck implements Duck { public void quack() { System.out.println("Quack"); } public void fly() { System.out.println("I'm flying"); } }
- There is another interface called the Turkey Interface:
public interface Turkey { //Turkeys don't quack public void gobble(); public void fly(); }
- The turkey interface is implemented by the wildTurkey class as shown below:
public class WildTurkey implements Turkey { public void gobble() { System.out.println("Gobble gobble"); } public void fly() { System.out.println("I'm flying a short distance"); } }
- And finally comes our showstopper, the Turkey Adapter code as shown below:
public class TurkeyAdapter implements Duck { Turkey turkey; public TurkeyAdapter(Turkey turkey) { this.turkey = turkey; } public void quack() { turkey.gobble(); } public void fly() { for(int i=0; i < 5; i++) { turkey.fly(); } } }
- The website also provides a good tester to test the Adapter code. Please refer to the link to see the test driving code.
- Real world Adapters:
Following are the examples of real world adapters:
- The old Java Enumeration class had a couple of methods:
- hasMoreElements()
- nextElement()
- The new Java Iterator class has three methods:
- hasNext()
- next()
- remove()
This section provides an EnumerationIterator adapter class that implements the Iterator. Please refer to the link to see the EnumerationIterator Adapter class.
Pros:
- A very good example of Adapter Pattern using the Duck Interface, Turkey Interface and the TurkeyAdapter pattern. ( Gosh! Too much of a Turkey used, reminds of Thanksgiving! ).
- Also, the section on real world adapters compliments further understanding of Adapter pattern implementation. Good for Intermediate and Expert users of Design patterns
Cons:
- Does not contains enough content to understand Adapter pattern from scratch.
- Also, not suitable for beginners. Though the code provided is lucid and self explanatory, it is not complimented with explanatory texts and this may cause some problems for beginners to understand the code.
- One more thing to observe here is the font size! ( Not good for readers who are adapted to small or medium font size ).
Adapter Pattern on blackwasp.com
Topics Covered:
What is adapter pattern, implementing adapter pattern, example of adapter pattern.
Summary:
This page explained the adapter pattern in brief. It gives a good example for implementing the adapter pattern and testing the adapter. The adapter pattern is a Gang of Four design pattern. Its a structural pattern as it defines a manner for creating relationships between classes. The adapter design pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.
- What is adapter pattern:
- The adapter pattern is introduced with the example of personnel system and intranet system. It helps build up the example in the implementation part
- Implementation:
- An implementation of the adapter with client, target, adaptee and adapter is shown.
- Example and testing adapter:
- The example of intranet and personnel system discussed in the introduction is implemented in C# code. Also how to test the adapter is shown.
Pros: This page is useful for beginners. It provides a good example and shows how to implement the adapter.
Cons: It focuses more on the example than on the theory behind adapter pattern.
Adpater Pattern on Sourcemaking.com
Topics Covered:
Intent of Adapter Pattern, Explanation of the pattern, structure of the pattern, example, rule of thumb for adapter pattern.
Summary:
Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. This webpage gives a basic idea of adapter pattern and gives examples of the pattern in various programming languages.
- Intent:
The need and purpose of adapter pattern.
- Problem and Discussion:
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. This section tells the problems and how adapter pattern helps solve them.
- Structure:
Class diagrams showing the different structures of adapter pattern.
- Example:
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.
- Check list and Rule of thumb:
These sections describe when and how adapter pattern is useful. It also describes the various things to keep in mind when working with adapter pattern.
- Adapter Code examples:
Links to examples of Adapter pattern implemented in various languages.
Pros: A good place to study the adapter pattern from scratch. The examples of code in different languages lets us compare the implementation across all these languages. The links contain easy to understand code making it simple for beginners.
Cons: Hmmm, now this webpage can be qualified as one of the better pages to learn about Adapter patterns. Since we couldn't find any shortcomings of this web page we leave it as an exercise for curious readers!
Further Readings
Understanding and Implementing the Adapter Pattern.
Adapter Pattern in Java.
Flavors of Adapter Pattern.
Implementing Adapter Pattern.