Introduction to the Adaptor design pattern

No.of Views967
Bookmarked0 times
Downloads 
Votes0
By  Chinna Srihari   On  23 Oct 2010 03:10:18
Tag : Design Patterns , General
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 
Definition
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
 
Note:
1.       Wrap an existing class with a new interface.
2.       Impedance match an old component to a new system
3.       Adapter, the modifier, is about creating an intermediary abstraction that translates, or maps to the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.
4.       Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an improved interface.
 
Design
 
Please refer to the code
 
Some of best example: Adaptor for Two pin plug, We can have only one adaptor
 
 
UML Diagram
From GoF
 
 
 Please refer to attached doc for images
 
 
Code
 
    ///<summary>
    /// Target Wraper base
    ///</summary>
 
    public class AdaptorTarget
    {
        public virtual void Request()
        {
            Console.WriteLine("Called Target Request()");
        }
    }
 
    ///<summary>
    /// Adapter -- Wrapper
    ///</summary>
 
    public class Adaptor : AdaptorTarget
    {
        internal OldSystem.Adaptee Adaptee
        {
            get { return new OldSystem.Adaptee();}
            set { ;}
        }
 
        public override void Request()
        {
            Adaptee.OldMethod();
        }
    }
 
    namespace OldSystem
    {
        ///<summary>
        /// Adaptee -- Old system
        ///</summary>
        internal class Adaptee
        {
            internal void OldMethod()
            {
                Console.WriteLine("Called incompatable system method....");
            }
        }
    }
 
Client
 
internal static AdaptorTarget AdaptorTargetLocal
        {
            get
            {
                return new Adaptor();
            }
            set
            {
            }
        }
   
        static void Main(string[] args)
        {
            AdaptorTargetLocal.Request();
}
 
 
Note: Adator provides wrapper for each system. Where are Façade does differently. And Proxy is also a wrapper but it exposes the same interfaces as the subsystem.
 
 
Hope this helps
 

Download Sample

Download source files -47 kb

 
Sign Up to vote for this article
 
About Author
 
Chinna Srihari
Occupation-Software Engineer
Company-
Member Type-Junior
Location-India
Joined date-17 Jun 2010
Home Page-
Blog Page-
Having 12 + years of IT experience, 2.5 years as Technical Management experience and 1.5 year of onsite coordinator experience and rest 8+ years of technical activities including Project Leader, Tech Lead, Designer, PQL, and Developer experience in Quality Management System (CMM-level 5, SEI-CMM-level 5).
 
 
Other popularSectionarticles
    This is a quite simple design pattern, let me tell you one thing there are many design patterns that they are implicitly singletons. I rather recommend to understanding this one first before going through other patterns.
    Published Date : 21/Oct/2010
    As a developer we might have noticed that creation of some class is very difficult and number of time that we need to create. Such object creation every time is very expensive in terms of system resources. If we can cache that kind of object show how will surely boost the application performance and resource usage can be saved. This where object pool design pattern will help development community to cache the objects. Some time it is also called as Object cache or Resource cache design pattern.
    Published Date : 21/Oct/2010
    The Command pattern creates distance between the client that requests an operation and the object that can perform it.
    Published Date : 20/Sep/2010
    Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.
    Published Date : 22/Oct/2010
    A fully initialized instance to be copied or cloned. The Prototype pattern specifies the kind of objects to create using a prototypical instance.
    Published Date : 22/Oct/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top