Jeffnl
Jeffnl

Reputation: 261

Interfacing different classes

i am writing an interface for 2 camera brands, the interface to the cameras is different(they have a different sdk) but the actions are the same (zooming panning tilting) so i need two classes for this, i don't know how to explain this further so I will show and example:

class program
{
    public void main(string Brand)
    {
        CameraController controller;

        if( Brand == "A" )
            controller = new BrandACameraController();
        else
            controller = new BrandBCameraController();

        controller.Pan(); //pan with chosen camera brand
    }
}

class BrandACameraController
{
    public BrandACameraController()
    { 

    }

    public void Pan()
    {
        //interface with brand a camera
    }

    public void Tilt()
    {
        //interface with brand a camera
    }

    public void Zoom()
    {
        //interface with brand a camera
    }
}

class BrandBCameraController
{
    public BrandBCameraController()
    { 

    }

    public void Pan()
    {
        //interface with brand b camera
    }

    public void Tilt()
    {
        //interface with brand b camera
    }

    public void Zoom()
    {
        //interface with brand b camera
    }
}

how do I do this?

Upvotes: 0

Views: 71

Answers (4)

Rohit Singh
Rohit Singh

Reputation: 106

public interface A { void Test();

    int TestApp(int input);
}

public interface B
{
    void Test();

    int TestApp(int input);
}

public class Testing : A, B
{
    void A.Test()
    {
        throw new NotImplementedException();
    }

    int B.TestApp(int input)
    {
        throw new NotImplementedException();
    }

    void B.Test()
    {
        throw new NotImplementedException();
    }

    int A.TestApp(int input)
    {
        throw new NotImplementedException();
    }
}

public class TestingApp
{
    public void DoOperation()
    {
        A testing = new Testing();
        // This will call interface A method
        testing.Test();
        B testing1 = new Testing();
        // This will call interface B method
        testing1.Test();
        //testing.Test();
    }
}

Upvotes: 0

Matthew Watson
Matthew Watson

Reputation: 109567

  1. Create an interface that contains all the shared methods and properties.
  2. Implement that interface in class BrandACameraController and class BrandBCameraController
  3. Use that interface everywhere except where you actually have to create the concrete classes.

So for your example:

interface ICameraController
{
    void Pan();
    void Tilt();
    void Zoom();
}

class BrandACameraController: ICameraController
{
    public BrandACameraController()
    {

    }

    public void Pan()
    {
        //interface with brand a camera
    }

    public void Tilt()
    {
        //interface with brand a camera
    }

    public void Zoom()
    {
        //interface with brand a camera
    }
}

class BrandBCameraController: ICameraController
{
    public BrandBCameraController()
    {

    }

    public void Pan()
    {
        //interface with brand b camera
    }

    public void Tilt()
    {
        //interface with brand b camera
    }

    public void Zoom()
    {
        //interface with brand b camera
    }
}

You should probably also make a CameraControllerFactory to encapsulate creating them:

static class CameraControllerFactory
{
    public static ICameraController Create(string brand)
    {
        if (brand == "A")
            return new BrandACameraController();
        else
            return new BrandBCameraController();
    }
}

Then the code from your example where you check Brand to see what to create will become like this:

ICameraController controller = CameraControllerFactory.Create(Brand);

controller.Pan(); //pan with chosen camera brand

Upvotes: 4

Erik Schierboom
Erik Schierboom

Reputation: 16636

The other answers use an interface, which is fine. You can also use another option, which is to define an abstract base class and have the different camera brands inherit from that:

abstract class CameraController
{
    public abstract void Pan();
    public abstract void Tilt();
    public abstract void Zoom();
}

class BrandACameraController : CameraController
{      
    public override void Pan()
    {
        //interface with brand a camera
    }

    public override void Tilt()
    {
        //interface with brand a camera
    }

    public override void Zoom()
    {
        //interface with brand a camera
    }
}

class BrandBCameraController : CameraController
{      
    public override void Pan()
    {
        //interface with brand b camera
    }

    public override void Tilt()
    {
        //interface with brand b camera
    }

    public override void Zoom()
    {
        //interface with brand b camera
    }
}

Upvotes: 1

Nicholas W
Nicholas W

Reputation: 2241

That is exactly the purpose of an interface:

interface ICameraController
{
    void Pan();
    void Tilt();
    void Zoom();
}
class BrandACameraController : ICameraController { ... }
class BrandBCameraController : ICameraController { ... }

Then when you store the controller in your main method above, you do so with the type ICameraController. If you want to share some code between implementations, you may also wish to derive the implementations from a common base class (as well as or instead of using an interface).

Upvotes: 2

Related Questions