dimanche 8 mars 2020

Factory method and broken interface

I wrote simple Factory-method pattern. But in method of concrete classes inside of it I want to use parameters of different type. Depending on which concrete class was created by my factory. Of course I can't do like this.

abstract class PersonListCreator
{

    abstract protected function getPersonListCreator(): PersonList;
}

interface PersonList
{

    public function addPerson(Person $person): void;
}

interface Person
{
   //----
}

class Player implements Person
{
    //----
}

final class PlayerListCreator extends PersonListCreator
{

    protected function getPersonListCreator(): PersonList
    {

        return new PlayerList();
    }
}

final class PlayerList implements PersonList
{
    public function addPerson(Player $person): void 
    {
        //----
    }
}

addPerson function in PlayerList is wrong. Because I broke PersonList interface contract

I'm trying to find proper architectural solution. Maybe I must use another pattern?

Aucun commentaire:

Enregistrer un commentaire