我似乎不明白接口如何实现松耦合的概念?您可能会发现这个问题与其他问题重复,但我已经阅读了许多与该主题相关的答案,但没有找到令人满意的解释。
下面是许多开发人员实现松散耦合的示例。
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();
这个例子解决了前面例子的问题,但我们根本不使用接口。
我的问题是,如果我可以在不使用接口的情况下实现松耦合,为什么还要使用接口?在上述场景中该接口会带来什么好处?或者如果我不使用上面示例中的接口,我会遇到什么问题?
感谢您的帮助。
正如其他人所指出的,您所做的更多是依赖注入,这是一个相关但与松散耦合不同的主题。我将尝试通过接口深入了解松散耦合的简单应用。
我倾向于将接口视为定义/执行契约的便捷方式。不要考虑每个形状都可以打招呼的简单示例,而是考虑需要将每个形状渲染到图像的情况。
此伪代码显示了如何在没有界面的情况下处理正方形和圆形。
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
}
相反,如果我们说我们不太关心什么type它的形状,但只有你可以渲染它,我们最终得到以下界面:
interface renderableShape {
function render()
}
等等,在您只关心渲染形状的能力的情况下,您可以编程against那个界面。
你可以有这样的东西:
function renderShapes(Array[renderableShape] shapes){
foreach(shape in shapes){
shape.render()
}
}
您现在要确保您的renderShapes
函数无法了解 Circle 或 Square 实现的任何具体细节。它需要知道的是你可以打电话render
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)