Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Toy toy = new Toy();
            ZincCarbonBattery zincCarbonBattery = new ZincCarbonBattery();

            toy.UseBattery(zincCarbonBattery);

            AlcalineBattery alcalineBattery = new AlcalineBattery();
            //toy.UseBattery(alcalineBattery);

            /*  Want to use alcaline battery?
             *  We get compiler error. Toy class is tightly coupled to ZincCarbonBattery class
             *  We have to make some arrangement in Toy class.
             *  Maybe we might want to add second UseBattery overload method.
             *  But we have to touch Toy class. This is undesirable.
             */
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Toy toy = new Toy();
            ZincCarbonBattery zincCarbonBattery = new ZincCarbonBattery();

            toy.UseBattery(zincCarbonBattery);

            AlcalineBattery alcalineBattery = new AlcalineBattery();

            toy.UseBattery(alcalineBattery);
            // Now we can use alcaline battery
            // Toy class, is Loosely coupled to ZincCarbonBattery and AlcalineBattery classes

            /* But if we want to use a kind of third battery?
             *  We can create a new class that implements IBattery interface.
             *  Then we can use new battery with toy just by calling UseBattery method.
             *  We dont have to make any arrangement in Toy class.
             *  This is what we want.
             */
        }