类设计模式-最佳实践


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>";
}

2'nd类看起来像

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函数,我们像new color()一样实例化类本身,会有问题吗????

现在我的问题是,只有一种班级颜色是否更好并将第二类的函数包含在第一类中?

没有。

还是让他们分开?

是的。

为什么一个比另一个好,反之亦然?

如果您决定使用不同类型的CRUD或其他使用颜色进行操作的对象(例如Builder),则需要将Color类作为一个单独的类。如果您希望CRUD不仅使用Color对象进行操作,也是如此。最好尽可能多地进行脱钩。

设计模式对我来说很重要,为什么要选择一个而不是另外

有许多模式可以对您有所帮助:Builder、Repository、Decorator、Bridge、Factory。。。这取决于您的需求,什么更好地实施。您必须熟悉所有这些,并且在不了解为什么它是此特定任务的最佳选择的情况下永远不要执行它。

如果假设我们在中有函数create_color,会有问题吗我们将类本身实例化为new color()????

是的,如果你需要添加一些创建步骤(例如以不同的方式生成ID),你必须将此步骤添加到你的所有类中,如Color、Font等。如果是单独的构建器类,你将该步骤添加到create()方法中,它将以新的方式为所有抽象对象生成ID。

希望这将向你展示一种学习更多模式的方法。祝你好运

顺便说一句,看看这本很棒的免费书:http://gameprogrammingpatterns.com/