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()
和getPointY()
只是一个多余的传输"方法。我知道ig Point
有1000个其他方法,将所有对象作为return $this->point
返回是不安全的。但是这次所有的财产都被覆盖了。