vendredi 2 octobre 2015

Observer Patter, or just modify the object in another class?

lets say, in our system when an user registers, he gets a voucher too right away. There are 3 solutions:

1,

class UserModel
{
    public function create ($username)
    {
        $userId = SQL INSERT INTO users
        Voucer::create ($userId);
    }
}

but its bad (hurt SRP and LoD)

2,

class ModelHelper
{
    public static function create ($username)
    {
        $userId = UserModel::create ($userName);
        Voucer::create ($userId);
    }
}

it only hurts LoD now.

3, a solution using Observer Pattern (Im not going to provide a source code right here).

So which is the best?

Aucun commentaire:

Enregistrer un commentaire