我正在为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
}
}
我该怎么做?
答案 0 :(得分:4)
class BrandACameraController
和class BrandBCameraController
所以对你的例子来说:
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();
}
}