0

我正在为 2 个相机品牌编写界面,相机的界面不同(它们有不同的 sdk)但动作相同(缩放平移倾斜)所以我需要两个类,我不知道如何进一步解释这一点,所以我将展示和示例:

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
    }
}

我该怎么做呢?

4

4 回答 4

4
  1. 创建一个包含所有共享方法和属性的接口。
  2. class BrandACameraController在和中实现该接口class BrandBCameraController
  3. 除了您实际上必须创建具体类的地方之外,在任何地方都使用该接口。

所以对于你的例子:

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
    }
}

您可能还应该CameraControllerFactory封装创建它们:

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

然后,您检查Brand以查看要创建的内容的示例中的代码将如下所示:

ICameraController controller = CameraControllerFactory.Create(Brand);

controller.Pan(); //pan with chosen camera brand
于 2013-06-14T08:35:09.063 回答
2

这正是接口的目的:

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

然后,当您将控制器存储在main上面的方法中时,您可以使用 type ICameraController。如果您想在实现之间共享一些代码,您可能还希望从公共基类派生实现(以及或不使用接口)。

于 2013-06-14T08:35:41.720 回答
1

其他答案使用接口,这很好。您还可以使用另一个选项,即定义一个抽象基类并让不同的相机品牌继承自该基类:

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
    }
}
于 2013-06-14T08:39:07.030 回答
0

公共接口 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();
    }
}
于 2013-06-14T09:02:31.923 回答