CSC/ECE 517 Fall 2012/ch2b 2w47 am: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 76: Line 76:
Explains the implementation of the Adapter pattern with the help of a UML diagram.
Explains the implementation of the Adapter pattern with the help of a UML diagram.
<br>
<br>
* <b>Applicability and Examples<b><br>
* <b>Applicability and Examples</b><br>
Adapters are encountered everywhere. From real world adapters to software adapters.
Adapters are encountered everywhere. From real world adapters to software adapters.
# <u>Non Software Examples of Adapter Patterns:</u> Power Supply Adapters, card readers and adapters, ...
# <u>Non Software Examples of Adapter Patterns:</u> Power Supply Adapters, card readers and adapters, ...
Line 91: Line 91:
<b>Pros:</b> 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 )<br>
<b>Pros:</b> 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 )<br>
<b>Cons:</b> Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.
<b>Cons:</b> Doesn't provides enough figures to understand the concept. For readers who endorse figures, not a good page to look out for.
<br><br>

Revision as of 06:39, 18 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.


A real world example for the Adapter Pattern.




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!


UML diagram for the 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, (which is 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
  1. Convert the interface of a class into another interface clients expect
  2. Adapter lets classes work together, that could not otherwise because of incompatible interfaces

,br>

  • 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.

  1. Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and adapters, ...
  2. 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
  1. Objects Adapters - Based on Delegation

It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).

  1. 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.

  1. 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.