Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>Create an interface that contains all the shared methods and properties.</li> <li>Implement that interface in <code>class BrandACameraController</code> and <code>class BrandBCameraController</code></li> <li>Use that interface everywhere except where you actually have to create the concrete classes.</li> </ol> <p>So for your example:</p> <pre><code>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 } } </code></pre> <p>You should probably also make a <code>CameraControllerFactory</code> to encapsulate creating them:</p> <pre><code>static class CameraControllerFactory { public static ICameraController Create(string brand) { if (brand == "A") return new BrandACameraController(); else return new BrandBCameraController(); } } </code></pre> <p>Then the code from your example where you check <code>Brand</code> to see what to create will become like this:</p> <pre><code>ICameraController controller = CameraControllerFactory.Create(Brand); controller.Pan(); //pan with chosen camera brand </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload