none
Pattern Adapter RRS feed

  • Question

  • The Adapter used to work with two classes with incompatible interfaces, ok?
    Doubt:

    Since the Class1

    Class1
    (
    void move(int direction)
    (do something ...)
    )


    class2
    (
    void run ()
    (do something ...)
    )

    only the method of Class1, move, receive param direction by the cliente, but run does not receive.
    How to adapt this and make the client work with the two classes the same way?
    Wednesday, March 4, 2009 12:23 PM

All replies

  • Hi Lucas A,

    Pattern adapter c# code is given below

    using System;
    using System.Collections.Generic;
    using System.Text;

    //This structural code demonstrates the Adapter pattern
    //which maps the interface of one class onto another
    //so that they can work together.


    namespace AdaptePattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                Target target = new Adapter();
                target.Request();
            }
        }

        class Adapter : Target
        {
            private ClassA _objClassA = new ClassA();
            private ClassB _objClassB = new ClassB();



            public override void Request()
            {
                _objClassA.move(11);
                _objClassB.run();
            }
        }

       
        class Target
        {
            public virtual void Request()
            {
               Console.WriteLine("Called Target Request()");
            }
        }

       
          public class ClassA
          {
                   public void move(int direction)
                   {
                    Console.WriteLine("do something " + direction);
                   }
          }

        public class ClassB
        {
             public void run ()
             {
                 Console.WriteLine("inside the run method");
             }
        }

    }

    Hope that this will help you.

    Regards,
    Phijo Mathew Philip.


    PHIJO MP
    Wednesday, March 4, 2009 5:11 PM
  •  
    Thanks for your reply. How to implement the Factory Method pattern when the methods have the same functionality but require different parameters?
    I want to instantiate one of the classes and use the methods in a uniform manner by the client. The client must pass the parameters.
    Thursday, March 5, 2009 1:38 AM
  • Lucas, if you need same methods with different parameters, that is can be done easily by method overloading concept supported by .NET.  Here is the link for C# language specification on overloading resolution.

    { Gaja; }
    Pl mark if answer solves your problem | Visit http://gajakannan.com/netarch.aspx for .net ref Arch
    Thursday, March 5, 2009 3:13 PM
  • For some useful information and C# code examples using the Adapter pattern, check out http://www.rmfusion.com/design_patterns/gof/adapter_pattern.htm
    Friday, August 14, 2009 12:25 PM
  • Hi,
    Ok I will consider that the client target is class1 so the client used to work like this

    public void Method()
            {
                Class1 target = new Class1();
                target.Move(5);
    
            }

    so now we need to adapt the class2 so the client can use it as it use calss1, so we will create an Adapter class that inherit from class1 and has an instance of class2, after that we will override or hide the move method and call the run inside it.


    public class Adapter:Class1
        {
            private Class2 c2;
            public new void Move(int direction)
            {
                c2.Run();
                Console.WriteLine("Move in Adapter");
            }
        }
    Now the client can work with calss2 as it works with calss1 using the adapter
    public void Method()
            {
                Class1 target = new Adapter();
                target.Move(5);
    
            }
    That's it.

    Thanks



    We are volunteers, if the reply help you mark it as your answer. thanks!!
    My Blog
    Saturday, August 15, 2009 12:03 PM