[英]Class design patterns - best practices
男女!
我的问题是我真的不知道最好的设计方法是什么,所以我定义了两个类,第一个是:
class color
{
private $id = NULL;
private $name = '';
private $rgb = NULL;
private $cmy = NULL;
private $wavelength = NULL;
private $frequency = NULL;
public function __construct($name, $rgb, $cmy, $wavelenght, $frequency)
{
setName($name);
setRGB($rgb);
setCMY($cmy);
setWavelength($wavelength);
setFrequency($frequency);
}
public function __destruct()
{
}
public function setName($name)
{
$this->name=$name;
}
public function setRGB($rgb)
{
$this->rgb=$rgb;
}
public function setCMY($cmy)
{
$this->cmy=$cmy;
}
public function setWavelength($wavelength)
{
$this->wavelength=$wavelength;
}
public function setFrequency($frequency)
{
$this->frequency=$frequency;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getRGB()
{
return $this->rgb;
}
public function getCMY()
{
return $this->cmy;
}
public function getWavelength()
{
return $this->wavelength;
}
public function getFrequency()
{
return $this->frequency;
}
public function toJSON()
{
return "{'id':'".$this->id."', 'name':'".$this->name."', 'rgb':'".$this->rgb."', 'cmy':'".$this->cmy."', 'wavelength':'".$this->wavelength."', 'frequency':'".$this->frequency."'}";
}
public function toCSV()
{
return $this->id . ", " . $this->name . ", " . $this->rgb . ", " . $this->cmy . ", " . $this->wavelength . ", " . $this->frequency;
}
public function toHTML()
{
return "<p>ID: " . $this->id . "</p><p>Name: " . $this->name . "</p><p>RGB: " . $this->rgb . "</p><p>CMY: " . $this->cmy . "</p><p>Wavelength: " . $this->wavelength . "</p><p>Frequency: " . $this->frequency . "</p>";
}
和二等班看起来像
class CRUD_color
{
public function create_color($parameters)
{
$color=new color();
$color->setName($parameter['name']);
$color->setRGB($parameter['rgb']);
$color->setCMY($parameter['cmy']);
$color->setWavelength($parameter['wavelength']);
$color->setFrequency($parameter['frequency']);
$entitymanager->persist($color);
$entitymanager->flush();
}
public function request_color($parameters)
{
$color=$entitymanager->find($parameter['id']);
echo $color->toJSON($parameter['name']);
}
public function update_color($parameters)
{
$color=$entitymanager->find($parameter['id']);
$color->setName($parameter['name']);
$color->setRGB($parameter['rgb']);
$color->setCMY($parameter['cmy']);
$color->setWavelength($parameter['wavelength']);
$color->setFrequency($parameter['frequency']);
$entitymanager->persist($color);
$entitymanager->flush();
}
public function delete_color($parameters)
{
$color=$entitymanager->delete($parameter['id']);
}
}
现在我的问题是,最好只使用一种颜色,并在第一种颜色中包含第二种颜色的功能? 还是让他们分开?
为什么一个比另一个更好? 设计模式对我很重要,所以为什么要选择一个。
如果让我们说在巫婆中有函数create_color,我们像新color()那样实例化类本身,就会有问题吗?
现在我的问题是,最好只使用一种颜色,并在第一种颜色中包含第二种颜色的功能?
没有。
还是让他们分开?
是。
为什么一个比另一个更好?
如果决定使用不同类型的CRUD或其他使用颜色进行操作的对象(例如,Builder),则需要将Color类作为单独的类。 如果您希望CRUD不仅使用Color对象进行操作,也是如此。 最好使去耦尽可能多。
设计模式对我很重要,所以为什么要选择一个。
有许多模式可以为您提供帮助:构建器,存储库,装饰器,桥梁,工厂...取决于您的需求,什么是更好的实施方案。 您必须熟悉所有这些方法,并且永远不要在不理解为什么它是完成此特定任务的最佳选择的情况下实施它。
如果让我们说在巫婆中有函数create_color,我们像新color()那样实例化类本身,就会有问题吗?
是的,如果您需要添加一些创建步骤(例如,以不同的方式生成ID),则必须将此步骤添加到所有类(例如颜色,字体等)中。如果使用单独的构建器类,则可以将此步骤添加到create()方法,它将以新的方式为所有抽象对象生成ID。
希望这将为您展示一种学习模式的方法。 祝好运!
顺便说一句,看看这本很棒的免费书: http : //gameprogrammingpatterns.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.