我似乎没有掌握接口如何实现松散耦合的概念?您可能会发现这个问题与其他一些问题重复,但我已经阅读了许多与该主题相关的答案,但我还没有找到令人满意的解释。
以下是有多少开发人员实施松散耦合的示例。
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();
这个例子修复了前面例子的问题,但我们根本不使用接口。
我的问题是如果我可以在没有它的情况下实现松散耦合,我为什么要使用接口?在上述情况下,接口会提供什么好处?或者如果我在上面的例子中不使用接口会遇到什么问题?
谢谢你的帮助。