Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Engineering Concepts (http://www.go4expert.com/forums/engineering-concepts/)
-   -   ask about adpater pattern (http://www.go4expert.com/forums/adpater-pattern-t15267/)

honeyboy_20 27Nov2008 04:07

ask about adpater pattern
 
i want a simple example for adapter pattern , i read this article in the site and its good but i have gap in some understanding some point so i want the implementation of this pattern and example in real world.
thanks in advance

shabbir 27Nov2008 09:45

Re: ask about adpater pattern
 
The example is also attached in the article.

honeyboy_20 27Nov2008 19:35

Re: ask about adpater pattern
 
Thanks , u say that u want to adapt to return float instead of int so u will make adapter to Itarget but it still return integer so we not make any changes.

PHP Code:

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

namespace 
DesignPattern
{
    public abstract class 
ITarget
    
{
        abstract public 
int Add(int aint b);
    }

    public abstract class 
INewTarget
    
{
        abstract public 
float Add(float afloat b);
    }

    
/// <summary>
    /// Old Implementation of calculator library had ITarget and now its INewTarget
    /// </summary>
    
class Calculator INewTarget
    
{
        public 
override float Add(float afloat b)
        {
            return 
b;
        }
    }

    public class 
CalcAdapter ITarget
    
{
        private 
Calculator calc;

        public 
CalcAdapter()
        {
            
calc = new Calculator();
        }

        public 
override int Add(int aint b)
        {
            return (int)
calc.Add((float)a, (float)b);
        }
    }




shabbir 27Nov2008 19:49

Re: ask about adpater pattern
 
The type casted ints are actually not changed but type cast and works that way.

I would try to explain in a bit better real time example

Lets say you have purchased a Laptop from USA and a charger for that Laptop is which only works in a 110 Volt situation.

Now you move to a place like say India where we have 220 Volt as normal and hardly find 110 Volt thing.

Now what you can do.

1. Change the Laptop
2. Change the Power Supply System
3. Use an Adapter which works in both environment and converts one form to the other.

This is what is done in here.

It actually takes int as input converts them float and performs the operation and returns int back.

It does not solve the issue if there is any about trancate but then the issue is not with truncate which is addressed here but the issue is with the operator knows only float and we know only int and so how we can use the Math Library to do our operation. We Adapt .

honeyboy_20 28Nov2008 00:31

Re: ask about adpater pattern
 
thanks for ur reply and really i learn from ur notes, as i understand the class of calculator change so it need to be adapted via adapter class but if u look depth about the example u will find the all examples we say is not nearly to the real world , the example of laptop is clear and is good to understand the adapter pattern but the implementation of calculator is not clear because what make me to make adapter i will do the method from start, from my imagine its good if we say if we class that do some operation and work in platform X and i want to take the benefit of this class to make it work in platform Y , so i will need adapter to make class feel that it work in platform x however it work in platform Y , i need example like this , if u can help to find something like this , i would pleased to u.

Thanks Again for ur effort

shabbir 28Nov2008 12:06

Re: ask about adpater pattern
 
The simple real world example is always comes when we have 2 stable things running but need someone to interact with them.

May not be Good Example but here is what I can think of as of now when I am doing some printing work.

Printer Hardware performs the way they are suppose and so is the case with PCs and driver can be considered as Adapter which makes it possible for both of them to operate together.


All times are GMT +5.5. The time now is 15:36.