mardi 30 juin 2015

Class design patterns - best practices

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() ????

Aucun commentaire:

Enregistrer un commentaire