class Point
{
private $x, $y, $color;
public function draw(Graphic $graphicEngine)
{
$graphicEngine->setPixel ($this->x, $this->y, $this->color);
}
}
class Graphic
{
private $points; // array of Point
public function draw()
{
foreach ($this->points as $point)
{
$point->draw($this);
}
}
}
我的同事说不好。它有一个循环引用(Point
知道Graphic
,反之亦然)。他说"不是主题(Point
)自己做某事,但是(Point
)主题发生了一些事情,而Point
应该是一个独立的生活事情。他的解决方案:
class Point
{
public $x, $y, $color;
}
class Graphic
{
private $points; // array of Point
public function draw()
{
foreach ($this->points as $point)
{
$this->setPixel ($point->x, $point->y, $point->color);
}
}
}
它分开了,好的。但在我看来,有一个Point
。它怎么能吸引自己呢?