0
class Point
{
private $x, $y;
public __construction ($x, $y)
{
$this->x = $x;
$this->y = $y;
}
public function getX()
{
return $this->x;
}
public function getY()
{
return $this->y;
}
}
首先我會寫這樣的:爲什麼爲了德米特法來包裝(和多個)代碼更好?
class Item
{
private $point; // Point
public __construction()
{
$this->point = new Point(0,0);
}
public function getPoint()
{
return $this->point;
}
}
然後:
$p = new Item();
$p->getPoint()->getX();
,但他們說這違反了法律。
class Item
{
private $point; // Point
public __construction()
{
$this->point = new Point(0,0);
}
public function getPointX()
{
return $this->point->getX();
}
public function getPointY()
{
return $this->point->getY();
}
}
然後:
$p = new Item();
$p->getPointX();
此時getPointX()
和僅僅是一個冗餘 「傳輸」 的方法在重構之後。據我所知,ig Point
有1000個其他方法,將所有對象返回爲return $this->point
將是不安全的。但是這次所有的屬性都被覆蓋了。
這就是對,但我不能看到好處。 –
@JohnSmith閱讀同一資源的優點/缺點。 – Justinas