3

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

以下是有多少开发人员实施松散耦合的示例。

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();

在上面的示例中,我们使用接口的多态性来实现松散耦合。但我没有看到这段代码是松散耦合的。在上面的示例中,调用代码(客户端)使用“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();

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

我的问题是如果我可以在没有它的情况下实现松散耦合,我为什么要使用接口?在上述情况下,接口会提供什么好处?或者如果我在上面的例子中不使用接口会遇到什么问题?

谢谢你的帮助。

4

1 回答 1

4

正如其他人所指出的,您正在做的更多的是依赖注入,这是一个相关但独立于松散耦合的主题。我将尝试通过接口对松散耦合的简单应用提供一些见解。

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

这个伪代码展示了如何在没有接口的情况下处理 Squares 和 Circles。

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
}

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

interface renderableShape {
  function render()
}

以此类推,您只关心渲染形状的能力,您可以针对该界面进行编程。

你可以有这样的东西:

function renderShapes(Array[renderableShape] shapes){
  foreach(shape in shapes){
    shape.render()
  }
}

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

于 2013-07-29T18:06:20.147 回答