连接不同的类

时间:2013-06-14 08:31:37

标签: c#

我正在为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 个答案:

答案 0 :(得分:4)

  1. 创建一个包含所有共享方法和属性的接口。
  2. class BrandACameraControllerclass BrandBCameraController
  3. 中实施该界面
  4. 除了实际创建具体类的地方外,在任何地方都使用该接口。
  5. 所以对你的例子来说:

    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
    

答案 1 :(得分:2)

这正是界面的目的:

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

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

答案 2 :(得分: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
    }
}

答案 3 :(得分: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();
    }
}