Design Patterns in PHP 8: Factory method

Hi!
In this article, I’ll show you how to use the Factory Method design pattern with an example.

Let’s say we have a shopping cart class and that class contains methods for caching the cart and for persisting it for a long time. It is proposed to cach…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Max Zhuk

Hi!
In this article, I'll show you how to use the Factory Method design pattern with an example.

Let's say we have a shopping cart class and that class contains methods for caching the cart and for persisting it for a long time. It is proposed to cache in RedisDB, and save in MySQL.

class CartModel
{
    public array $data;
    // Better take this settings from special file or env variables
    // but this article not about storing settings
    private array $redisSettings = [
        'user' => 'test_user',
        'password' => 'password'
    ];
    private array $mysqlSettings = [
        'hostname' => 'localhost',
        'login' => 'test_user',
        'password' => 'secret',
        'database' => 'test_db'
    ];

    // We need to implement a cache storage method
    // the Redis DB is better suited for this
    public function cache(): void
    {
    }

    public function save(): void
    {
    }
}

Here is such a blank class turned out, now we need to implement these methods. In fact, the meaning of each of them is to connect to the desired database and store data about the basket in it. Thanks to the Factory Method pattern, we will move the common code (saving) for all classes working with databases into an abstract class. And the functionality associated with the connection will be different for each database, so we will take it out separately using the common interface.

abstract class AbstractDataBase
{
    // A directly factory method that allows subclasses to return
    // any concrete connectors of the desired interface, since it is made abstract
    // We will create the interface a little later
    abstract public function getDataBase(): DataBaseConnector;

    // And this method will be the same for all databases
    public function save(array $data): void
    {
        $database = $this->getDataBase();
        $database->connect();
        $database->save($data);
        $database->disconnect();
    }
}

Let's implement a concrete class for RedisDB.

class Redis extends AbstractDataBase
{
    // php8 allows you to add private login and password fields using the constructor
    public function __construct(private readonly string $login, private readonly string $password)
    {}

    // Concrete Factory Method Implementation
    // Returns an instance of the connector class that implements the DataBaseConnector interface
    public function getDataBase(): DataBaseConnector
    {
        return new RedisConnector($this->login, $this->password);
    }
}

About the same way we create a class for the Mysql database.

class Mysql extends AbstractDataBase
{
    // Unlike Redis, we will need an array of data to connect
    public function __construct(private readonly array $settings)
    {}

    // Concrete Factory Method Implementation
    public function getDataBase(): DataBaseConnector
    {
        return new MysqlConnection($this->settings);
    }
}

It is with these database classes that we have just created that our basket will work.

But the interface of connectors as well as they are not written yet. Let's fix this omission. We will need methods for connecting to the database, disconnecting and, of course, saving data. In the future, it will be possible to extend the interface with various methods, but for now this is enough.

interface DataBaseConnector
{
    public function connect();
    public function disconnect();
    public function save(array $data): void;
}

I will not describe the implementations of the RedisDB and Mysql connectors, everything can be implemented there quite standardly.

class RedisConnector implements DataBaseConnector
{
    public function __construct(private $login, private $password)
    {}

    /**
     * @throws Exception
     */
    public function connect(): void
    {
        // connect() method implementation
    }

    public function disconnect()
    {
        // disconnect() method implementation
    }

    public function save(array $data): void
    {
        // save() method implementation
    }
}
class MysqlConnection implements DataBaseConnector
{
    public function __construct(private $settings)
    {}

    public function connect()
    {
        // connect() method implementation
    }

    public function disconnect()
    {
        // disconnect() method implementation
    }

    public function save(array $data): void
    {
        // save() method implementation
    }
}

Everything is ready to be used in the cart methods.

class CartModel
{
    //...

    public function cache(): void
    {
        try {
            $redis = new Redis($this->redisSettings['user'], $this->redisSettings['password']);
            $redis->save($this->data);
        } catch (\Exception $e) {
            //...
        }
    }

    public function save(): void
    {
        try {
            $mysql = new Mysql($this->mysqlSettings);
            $mysql->save($this->data);
        } catch (\Exception $e) {
            //...
        }
    }
}

Thanks to the use of the Factory Method pattern, we can access various databases inside models such as a shopping cart without worrying about the details of their work: connection, saving, data format, disconnect, etc. We avoid code duplication, excessive load on methods and classes, creation of divine classes.

© Photo by Patrick Hendry on Unsplash


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Max Zhuk


Print Share Comment Cite Upload Translate Updates
APA

Max Zhuk | Sciencx (2022-09-18T11:12:59+00:00) Design Patterns in PHP 8: Factory method. Retrieved from https://www.scien.cx/2022/09/18/design-patterns-in-php-8-factory-method/

MLA
" » Design Patterns in PHP 8: Factory method." Max Zhuk | Sciencx - Sunday September 18, 2022, https://www.scien.cx/2022/09/18/design-patterns-in-php-8-factory-method/
HARVARD
Max Zhuk | Sciencx Sunday September 18, 2022 » Design Patterns in PHP 8: Factory method., viewed ,<https://www.scien.cx/2022/09/18/design-patterns-in-php-8-factory-method/>
VANCOUVER
Max Zhuk | Sciencx - » Design Patterns in PHP 8: Factory method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/18/design-patterns-in-php-8-factory-method/
CHICAGO
" » Design Patterns in PHP 8: Factory method." Max Zhuk | Sciencx - Accessed . https://www.scien.cx/2022/09/18/design-patterns-in-php-8-factory-method/
IEEE
" » Design Patterns in PHP 8: Factory method." Max Zhuk | Sciencx [Online]. Available: https://www.scien.cx/2022/09/18/design-patterns-in-php-8-factory-method/. [Accessed: ]
rf:citation
» Design Patterns in PHP 8: Factory method | Max Zhuk | Sciencx | https://www.scien.cx/2022/09/18/design-patterns-in-php-8-factory-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.