AverageJoe's avatar

Which design pattern is this, if any?

Hello there, I was wondering if someone can help me by identifying this pattern (if this is even a design pattern to begin with). I've got the following class:

class myGeneralClass
{
    /**
     * @var Model { Class1, Class2 }
     */
    private Model $model;
    public function __construct(Model $model){
        $this->model = $model;
    }

    public function getData() : mixed {
        return $this->model::first();
    }

    public function setData(array $data) : void{
	    $this->model::create($data)
    }
}

Which I would invoke like this:

    $main = new myGeneralClass(new Class1);
    $main->getData(); // some data from that model
    $main->saveData($data) // save some data

I've got several methods which do exactly the same thing, and I just swap the model in/out whenever and wherever I need to. I was wondering if there is a specific pattern related to this kind of code, and if there is I'd love to learn / read more about it and check all the best practices.

Thanks in Advance

Alex

0 likes
4 replies
AverageJoe's avatar

@christian-qode I think this does not fit the standard Interface pattern, when you've got an interface you only implement the same types of methods, but the methods themselves are not ALWAYS the same (at least not necessarily). In my example above, every single method will be the same and the only think that's necessary is to use a different model for a different result.

tykus's avatar

@averagejoe it is almost like a Factory Pattern; but no quite a true implementation of that pattern IMHO.

Please or to participate in this conversation.