lundi 7 septembre 2015

There is a Design Pattern related to this solution?

There is a solution that I use frequently, I would like to know if there is an design pattern that is related to this solution:

I want modify properties of an object, and i would like to separate each responsability in different classes and also be able to add/remove easier those responsabilities.

In the example I have a choice of a test, I want to set the Choice object as selected and right, its just and example.

There is Pattern to name this solution? It is a good solution or there is another pattern this fix it better?

Thank you!

class Choice 
{
    private $isSelected;

    private $isRight; 
}

class ChoiceModifierInterface
{
    public function modify();
}

class Selector
{
    public function modify(Choice $choice)
    {
        //check if the user select the question 
        $choice->isSelected(true);
    }
}

class Corrector
{
public function modify(Choice $choice)
    {
        //check if the question is right
        $choice->isRight(true);
    }
}


class ChoiceModifier
{
    public function add(ModifierInterface $modifier)
    {
        //add classes
    }

    public function modify(Choice $choice)
    {
        foreach ($this->modifiers as $modifier)
        {
            $modifier->modify($choice);
        }
    }
}

class Client
{
    public function main()
    {
        $choiceModifier = new ChoiceModifier();
        $choiceModifier->add(//add all modifiers);

        //run all modifiers
        $choiceModifier->modify($choice);
    }
}

Aucun commentaire:

Enregistrer un commentaire