如何使用接口实现松耦合?

Jay*_*att 3 php architecture oop design-patterns

我似乎不明白接口如何实现松耦合的概念?您可能会发现这个问题与其他问题重复,但我已经阅读了许多与该主题相关的答案,但没有找到令人满意的解释。

下面是许多开发人员实现松散耦合的示例。

interface shape {
   public function sayName();
}

class Circle implements shape {

     public function sayName(){
          echo 'Hello my name is circle';
      }
}

class Square implements shape {

     public function sayName(){
          echo 'Hello my name is square';
      }
}

class Creator {
       public $shape = null;
       public function __construct(Shape $shape){
          $this->shape = $shape;
       }
}

$circle = new Creator(new Circle());
$circle->sayName();

$square = new Creator(new Square());
$square->sayName();
Run Code Online (Sandbox Code Playgroud)

在上面的例子中我们使用接口的多态性来实现松耦合。但我不认为这段代码是松散耦合的。在上面的示例中,调用代码(客户端)使用“new”运算符直接引用“Circle”和“Square”类,因此创建了紧密耦合。

为了解决这个问题,我们可以这样做。

接口形状 { 公共函数 sayName(); }

class Circle implements shape {

     public function sayName(){
          echo 'Hello my name is circle';
      }
}

class Square implements shape {

     public function sayName(){
          echo 'Hello my name is square';
      }
}

class Creator {
       public $shape = null;
       public function __construct($type){
          if(strtolower($type) == 'circle'){
             $this->shape = new Circle();
          } else if(strtolower($type) == 'square'){
             $this->shape = new Square();
          } else {
             throw new Exception('Sorry we don\'t have '.$type.' shape');
          }
       }
}

$circle = new Creator('Circle');
$circle->sayName();

$square = new Creator('Square');
$square->sayName();
Run Code Online (Sandbox Code Playgroud)

这个例子解决了前面例子的问题,但我们根本不使用接口。

我的问题是,如果我可以在没有接口的情况下实现松耦合,为什么还要使用接口?在上述场景中该接口会带来什么好处?或者如果我不使用上面示例中的接口,我会遇到什么问题?

感谢您的帮助。

Lar*_*nal 5

正如其他人所指出的,您所做的更多是依赖注入,这是一个相关但与松散耦合不同的主题。我将尝试通过接口深入了解松散耦合的简单应用。

我倾向于将接口视为定义/执行契约的便捷方式。不要考虑每个形状都可以打招呼的简单示例,而是考虑需要将每个形状渲染到图像的情况。

此伪代码显示了如何在没有界面的情况下处理正方形和圆形。

class Circle {
  function renderCircle() { ... }
}

class Square {
  function renderSquare() { ... }
}

// then when we use them
Circle[] circlesArray = loadCircles
Square[] squaresArray = loadSquares

foreach(circle in circlesArray){
  circle.renderCircle
}

foreach(square in squaresArray){
  square.renderSquare
}
Run Code Online (Sandbox Code Playgroud)

相反,如果我们说我们不太关心它是什么类型的形状,而只关心您可以渲染它,那么我们最终会得到以下界面:

interface renderableShape {
  function render()
}
Run Code Online (Sandbox Code Playgroud)

因此,在您只关心渲染形状的能力的情况下,您可以针对该接口进行编程。

你可以有这样的东西:

function renderShapes(Array[renderableShape] shapes){
  foreach(shape in shapes){
    shape.render()
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,您要确保您的renderShapes函数无法看到 Circle 或 Square 实现的任何具体细节。它只需要知道您可以调用 即可render