[Design Patterns]: Factory

In this pattern, the Factory class is responsible for instantiating the defined models. If in future you would like to exchange the model or change the way the model is instantiated

Example:

class Book 
{
    private $isbn;
    private $title;
    private $author;
    
    public function __construct($isbn, $title, $author)
    {
        $this->isbn = $isbn;
        $this->title = $title;
        $this->author = $author;
    }
}

class BookFactory
{
    public static function create($isbn, $title, $author)
    {
        return new Book($isbn, $title, $author);
    }
}

Majd Arbash

Leave a Reply