• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

'Switch' Interface example

Hi,

I'm just getting to grips with interfaces and I wanted to implement an example I found that uses a switch, an interface and another object.

E.g.
Switch -> ISwitchable (on or off) -> Lightbulb, Alarm. Television.

I did the following but I'm not sure if its covered the whole concept of Interfaces.

Any pointers?

Thanks
Paul

...
        private void button1_Click(object sender, EventArgs e)
        {
            Alarm myalarm = new Alarm();
            MessageBox.Show(myalarm.turnOn().ToString());
            LightBulb mylightbulb = new LightBulb();
            MessageBox.Show(mylightbulb.turnOn().ToString());
        }


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

namespace InterfaceExample
{
    interface ISwitchable
    {
        string turnOn();
        string turnOff();
    }
}




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

namespace InterfaceExample
{
    class LightBulb : ISwitchable
    {

        public string turnOn()
        {
            return "LightBulb On!";
        }

        public string turnOff()
        {
            return "LightBulb Off!";
        }

    }
}




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

namespace InterfaceExample
{
    class Alarm : ISwitchable
    {

        public string turnOn()
        {
            return "Alarm On!";
        }

        public string turnOff()
        {
            return "Alarm Off!";
        }

    }
}
0
paulwhelan
Asked:
paulwhelan
  • 3
  • 3
  • 2
  • +1
1 Solution
 
AlexFMCommented:
This code shows advantage of using interfaces:

       private void button1_Click(object sender, EventArgs e)
        {
            Alarm myalarm = new Alarm();
            Show(myalarm);
            LightBulb mylightbulb = new LightBulb();
            Show(mylightbulb);
        }


       private void Show(ISwitchable switch)
       {
             MessageBox.Show(switch.turnOn());
       }

Show function doesn't care what is actual instance type passed to it. Parameter must implement ISwitchable interface.
0
 
paulwhelanAuthor Commented:
Just with this

I create Lightbulb objects and Alarm objects but where does the Switch come into it?
Is that in the Interface?

Thanks
Paul
0
 
PoeticAudioCommented:
Your alarm and your lightbulb both implement the ISwitchable interface, therefor the MUST implement the mehtods in that interface (TurnOn, TurnOff). There is no implementation code in the interface.

The benefit of the interface is that other objects know how to communicate with your alarm and lightbulb objects because they implement ISwitchable. Knowing this, other objects are able to call TurnOn and TurnOff without knowing anything else about the alarm and lightbulb. As long as an object implements ISwitchable it can be passed through the Show method that Alex pointed out. It could be a computer, an ignition, a prostitute...etc as long as you implement ISwitchable you can turn those objects on and off without knowing anything else about them. Using interfaces you can build sort of a plug-n-play type design which allows dynamically created objects to easily communicate with eachother at some level.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dstanley9Commented:
Here's an example using interfaces:

        private void button1_Click(object sender, EventArgs e)
        {
            ISwitchable switch;
            switch = new Alarm();
            MessageBox.Show(switch.turnOn().ToString());
            switch = new LightBulb();
            MessageBox.Show(switch.turnOn().ToString());
        }

0
 
paulwhelanAuthor Commented:
Thanks guys.

Poetic aren't all the advantages you mention easily recreated with Inheritance?

Thanks
0
 
dstanley9Commented:
Interfaces, while sharing some characteristics, are different that inheritance.  An interface _defines_ properties and methods, while inheritance _duplicates_ implementation from one class to another (allowing for overrides, etc.).  

One other difference is that in .NET, classes can implement multiple interfaces (see ArrayList) while they can only inherit from ONE parent.
0
 
PoeticAudioCommented:
paul,

Yes they are, but oftentimes an object doesn't necissarily inherit from another object. For example, for humor lates take the prostitute =) She probably inherits from Person (or something) but she needs the advantage you get with inheritance of objects communicating with her without knowing anything about her, except that she has a TurnOn and TurnOff method. With interfaces we can still pass her through that Show method and the show method will still call TurnOn despite her inheriting from a completely different class. It's because she has the ISwitchable interface.

Lets look at another example

interface IPlugInAble
{
    void PlugIn();
}

Now lets say we have a TV object that derrives from ElectronicDevice. Well not all ElectronicDevices plug in to something (ie, some use batteries), but we still want to be able to plug the TV in, so we implement IPlugInAble. Well, now we have a car cigarette lighter that doesn't have anything to do with electronics, but it still plugs into the car lighter socket. We can still do this dynamically because we can have the cigarette lighter implement IPlugInAble. Despite the TV and the lighter having nothing in common except that both of them plug into something. Inheritance is not a good option here because what can we inherit from that both a TV and CigaretteLighter would have in common?
0
 
dstanley9Commented:
Also, referring to PoeticAudio's example, there are many diverse classes that could implement ISwitchable, but they would have completely different implementations, so inheritance would be of no use - you'd end up overriding all of the ISwitchable members anyway.
0
 
paulwhelanAuthor Commented:
Thanks guys.
I'm in a course today but Ill read over and split up points.
It all looks very helpful and thanks for the examples (my brain only works on examples!)
Cheers
Paul
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now