Php 类设计模式-最佳实践

Php 类设计模式-最佳实践,php,class,design-patterns,Php,Class,Design Patterns,男人和女人 我的问题是我真的不知道什么是最好的设计方法,所以我定义了两个类,第一个是: class color { private $id = NULL; private $name = ''; private $rgb = NULL; private $cmy = NULL; private $wavelength = NULL; private $frequency = NULL; public function __construct($name, $rg

男人和女人

我的问题是我真的不知道什么是最好的设计方法,所以我定义了两个类,第一个是:

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>";
}
现在我的问题是,如果只有一个类的颜色,并且在第一个类中包含第二个类的函数,是否更好?还是让他们分开

为什么一个比另一个好,反之亦然?设计模式对我来说很重要,为什么要选择一种呢

如果让我们假设我们有函数create_color,我们将类本身实例化为新颜色,这有问题吗

现在我的问题是,也许只有一种颜色更好 在第一个类中包含第二个类的函数

没有

还是让他们分开

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

例如,如果您决定使用不同类型的积垢或其他使用颜色生成器操作的对象,则需要将“类颜色”设置为单独的颜色。如果希望CRUD不仅处理颜色对象,也要处理颜色对象,则情况也是如此。最好尽可能地实现脱钩

设计模式对我来说很重要,为什么要选择一种而不是另一种呢 其他

有许多模式对您有帮助:构建器、存储库、装饰器、桥接器、工厂。。。这取决于你的需要,什么是更好的实施。您必须熟悉所有这些功能,并且在不了解为什么它是此特定任务的最佳选择的情况下,决不能实施它

如果我们有创建颜色的功能,会有问题吗 巫婆,我们像新颜色一样实例化了类本身

是的,如果您需要添加一些创建步骤,例如以不同的方式生成ID,您必须将此步骤添加到所有类中,如颜色、字体等。如果是单独的生成器类,您将此步骤添加到创建方法中,它将以新的方式为所有抽象对象生成ID

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


顺便说一句,看看这本很棒的免费书:

我投票将这个问题作为离题题题来结束,因为它听起来更适合你;私有$name=;private$rgb=NULL;私有$cmy=NULL;您可以简单地执行以下操作:private$id=NULL、$name=、$rgb=NULL、$cmy=NULL,etcDesign始终是主观的。最好的还是最坏的取决于上下文。非常感谢您的回答!
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']);
    }
}