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
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: using System;using System.Collections.Generic;using System.Text;namespace DesignPattern{ public abstract class ITarget { abstract public int Add(int a, int b); } public abstract class INewTarget { abstract public float Add(float a, float b); } /// <summary> /// Old Implementation of calculator library had ITarget and now its INewTarget /// </summary> class Calculator : INewTarget { public override float Add(float a, float b) { return a + b; } } public class CalcAdapter : ITarget { private Calculator calc; public CalcAdapter() { calc = new Calculator(); } public override int Add(int a, int b) { return (int)calc.Add((float)a, (float)b); } }}
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 .
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
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.