dimanche 4 décembre 2022

What oop pattern is this and how it is used?

I've seen this code in multiple projects(usually laravel) that i support now and i can not determine which pattern is this. It looks somewhat alike Factory Method, but it is obviously not and has a stable implementation that pops up here and there. Personally i like it(nice instatiating method) but i want to know more about it(name, purposes and other information). Can you help me?

So basically it is like this

class Foo implements Fooable
{
    public readonly $bar;
    
    
    private function make($bar)
    {
        $instance = new self();
        $instance->bar = $bar;
        
        return $instance;
    }
    
    public function process($bar) //or execute, that is a cqrs app but i saw that in non-cqrs too
    {
     $instance = make($bar);
     //continue code
    }
}

Sometimes there is an protected/private constructor and make is static method that sets up the instance. But there is always a make() method that returns set up self/static instance of class.

Aucun commentaire:

Enregistrer un commentaire