DCSIMG
Adapter Pattern - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Adapter Pattern

Adapter Pattern

Continuing the tour in the structural patterns I'm going to introduce the adapter pattern.
You can read my previous posts on structural patterns here:
Decorator pattern 
Proxy pattern
Facade pattern

The Adapter Pattern
The adapter pattern helps us to convert an interface of a class into another
interface that we need. It helps us to adapt old systems to new systems
without the need to rewrite the old systems. Also, adapter pattern enables
classes to work together even though they have an incompatible interfaces.
For a UML diagram of the pattern go to dofactory site.

How does it Work?
One way to implement the adapter pattern includes 3 participants -
an interface, an adaptee and an adapter. The interface defines the
operations we need in the adapter. The adaptee has it's original operations.
The adapter class inherits the adaptee class and implements the interface
by using the adaptee operations.

Another way to implement the adapter also includes 3 participants -
the class with operations to inherit (which replace the interface), an adaptee and an adapter.
In this case The class defines virtual methods, the adapter class inherit the
class and holds the adaptee as member. The adapter class overrides the class' virtual
methods and use the adaptee operations inside them.

Example in C#
Lets look at a simple example of the first way:

    #region Interface

 

    public interface IOperation

    {

        #region Methods

 

        void Fly();

 

        #endregion

    }

 

    #endregion

 

    #region Adaptee

 

    public class Car

    {

        #region Methods

 

        public void Drive()

        {

           // do the operation of drive

        }

 

        #endregion

    }

 

    #endregion

 

    #region Adapter

 

    public class FlyingCar : Car, IOperation

    {

        #region IOperation Members

 

        public void Fly()

        {

            // calling the drive method of the Car

            this.Drive();

        }

 

        #endregion

    }

 

    #endregion

Summary
The adapter pattern is useful for places where you have a class with an operation
that you want to use but the interface of the class isn't compatible with the
interface that you need.
In the next post in this series I'll write about the composite pattern.

Comments

Gil Fink Blog said:

The first pattern in the creational pattern series is the singleton pattern. You can read my previous

# April 23, 2008 9:20 AM

Gil Fink's Blog said:

In this post I give an introduction to the creational patterns.

# May 18, 2008 8:33 PM

Gil Fink's Blog said:

In the post I explain the composite design pattern and show an example of the pattern in C#.

# May 18, 2008 8:52 PM

Gil Fink's Blog said:

In the post I introduce the flyweight design pattern and show a C# example.

# May 19, 2008 8:57 PM

Gil Fink's Blog said:

In the post I describe the bridge design pattern and show an example in C#.

# May 19, 2008 8:58 PM

Gil Fink's Blog said:

In this post I give an introduction to the third design patterns type - the behavioral patterns.

# May 21, 2008 8:34 PM

Gil Fink's Blog said:

In this post I explain the abstract factory design pattern and show an example in C#.

# June 13, 2008 6:28 PM

Gil Fink's Blog said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 1:03 PM

Alpesh said:

Thanks

# October 3, 2008 8:58 AM