dsvjmc0907 2015-06-30 08:07
浏览 40
已采纳

类设计模式 - 最佳实践

men and women!

My problem is I don't really know what is the best way to design so I defined 2 classes 1'st one is:

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

and 2'nd class looks like

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']);
    }
}

now my question is if maybe it is better to have only one class color and include the functions from the second class in the 1st one? or let them apart?

why is one better than the other or vice versa? the design pattern is important to me so why choose one over the other..

Is there a problem if lets say we have the function create_color in witch we instantianate the class itself like new color() ????

  • 写回答

1条回答 默认 最新

  • doujiao9574 2015-06-30 17:22
    关注

    now my question is if maybe it is better to have only one class color and include the functions from the second class in the 1st one?

    No.

    or let them apart?

    Yes.

    why is one better than the other or vice versa?

    If you decide to have different types of CRUDs or other objects that manipulate with colors (Builder for example) you need class Color to be a separate one. The same if you would like your CRUD to manipulate not only with Color objects. It is better to make decoupling as much as possible.

    the design pattern is important to me so why choose one over the other..

    There are many patterns that can be helpful for you: Builder, Repository, Decorator, Bridge, Factory... It depends on your needs what is better to implement. You must be familiar with all of them and never implement it without understanding of why it is best choice for this particular task.

    Is there a problem if lets say we have the function create_color in witch we instantianate the class itself like new color() ????

    Yes, if you need to add some creational step (for example generate ID in different way) you have to add this step to all your classes like Color, Font etc. In case of separate builder class - you add this step to create() method and it will generate ID in new way for all abstract objects.

    Hope this will show you a way to learn more about patterns. Good luck!

    BTW, take a look at this great free book: http://gameprogrammingpatterns.com/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c