C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Adapter Pattern: An introduction


Posted Date: 18 Aug 2008    Resource Type: Articles    Category: General

Posted By: pradipta       Member Level: Gold
Rating:     Points: 10



Adapter pattern (also known as wrapper) helps us to solve the following design problems

  • Classes became incompatible due to incompatible interfaces that means classes are doing similar tasks but are not compatible.

  • Suppose we have implemented a data structure of Last In First Out(LIFO) and First In First Out(FIFO) using Stack and Queue respectively. After few days there is a requirement came to replace the above data structures with new one then we have to change our client code if we use the above data structures directly in client code.


  • If we put an Adapter/wrapper in between our client code need not be changed

    The following class inherited from Stack adding/removing items to it


    internal class LIFO : Stack, IDataStruture
    {
    public void Add(T item)
    {
    this.Push(item);
    }
    public T Remove()
    {
    return this.Pop();
    }
    }

    The following class inherited from Queue adding/removing items to it

    internal class FIFO : Queue, IDataStruture
    {
    public void Add(T item)
    {
    this.Enqueue(item);
    }
    public T Remove()
    {
    return this.Dequeue();
    }
    }


    As they are doing the similar kind of job, so to simplify the implementation lets have an interface

    public interface IDataStruture
    {
    void Add(T item);
    T Remove();
    }


    The adapter looks like as below

    public class Adapter
    {
    private IDataStruture objStack = new LIFO();
    private IDataStruture objQueue = new FIFO();

    public void AddLIFO(T item)
    {
    objStack.Add(item);
    }
    public void AddFIFO(T item)
    {
    objQueue.Add(item);
    }
    public T RemoveLIFO()
    {
    return Remove(objStack);
    }
    public T RemoveFIFO()
    {
    return Remove(objQueue);
    }
    private T Remove(IDataStruture ds)
    {
    T item;
    item = ds.Remove();
    return item;
    }

    }





    Lets look at the client code
    As we have introduced an Adapter in between so our client will create an instance of Adapter and use the data structures indirectly.

    Adapter adapter;
    public Form1()
    {
    InitializeComponent();
    adapter = new Adapter();
    }

    private void btnPush_Click(object sender, EventArgs e)
    {
    if (comboBox1.Text.Equals("LIFO"))
    foreach (string str in lstPush.CheckedItems)
    {
    adapter.AddLIFO(str);
    }
    else
    foreach (string str in lstPush.CheckedItems)
    {
    adapter.AddFIFO(str);
    }
    }

    private void btnPop_Click(object sender, EventArgs e)
    {
    if (comboBox1.Text.Equals("LIFO"))
    lstPop.Items.Add(adapter.RemoveLIFO());
    else
    lstPop.Items.Add(adapter.RemoveFIFO());
    }




    Attachments

  • Adapter Pattern Sample (20291-18654-Adapter.zip)



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Introduction to Adapter Design Pattern  .  Adapter Pattern Introduction  .  Adapter Pattern Example  .  Adapter Patern Sample code  .  Adapter Design Pattern  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C# files : XML Documentation
Previous Resource: Hit count using ASP.net
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use