php factory pattern

Creating Classes With PHP Factory, Abstract Factory, Simple and Static Factory Patterns

Factories act as a way to create a new instance of specific class, in another way instead of handling the creation of the class by the client code some classes will be in charge of that and handles the creation process, all you have to do is to call that factory.

 

 

There are four types of factories:

  • Factory Method
  • Abstract Factory Method
  • Simple Factory
  • Static Factory

 

First Pattern: The Factory Method

The Factory pattern is one of the commonly used design patterns.

In it’s normal form is a class that can be instantiated and have a method that takes some parameters and creates a new instance based on these parameters, to understand how this is work let’s show an example:

 

Imagine that we have an interface for Geometrical shapes like Rectangle, Triangle, Circle, etc and have only one method draw that draws a shape according to position like this:

 

Now let’s create a specific shape from this interface for example Rectangle as shown below:

As shown in the code above we implemented the Shape interface and typehint the constructor with the Position parameter through using dependency injection and finally implemented the concrete draw() method.

 

Now to create an instance of the class we can do something like this:

 

But we can create a new instance in another way using the Factory method pattern as shown below:

As shown above we created a new shape factory class and has a method that takes the class name as a string and returns a new instance of that class. This method is called the creator.

 

Now you can create Circle and Triangle classes the same way as Rectangle as shown in the following code:

As you see the advantage of this approach is that you don’t have to remember each class and how it’s instantiated, just give the class name to the creator and the creator gives you the instance.

 

Second Pattern: The Abstract Factory Method

The abstract factory method is the as the factory method except that in this case instead of just having one class, you have a series of related or dependent objects and they share a common interface and you need an easy way to create objects for each concrete class.

 

Let’s imagine that we need to format text in two ways, json and html so we need to create a concrete abstract class that will represent our concrete base implementation as follows:

 

Next we have to add the different implementations for that class as shown below:

As shown above each class extends from the abstract Text class and provides it’s own implementation.

Now To create the factory using this approach you have to define the base abstract factory and it’s child implementations like this:

So as you see in the code above for each concrete implementation [classX] we define concrete factory (factoryX) and each factory responsible for only creating instance of his class.

This approach in some cases not considered a real design pattern because it violates the principle of dependency injection, instead of using the classes through dependency injection, it calls the classes directly in the code and this considers a bad practice in OOP.

 

Third Pattern: The Static Factory Method

This pattern like the abstract factory method is used to create series of related or dependent objects. The difference between this and the abstract factory pattern is that the static factory pattern uses just one static method to create all types of objects it can create.

 

A good practice when creating the static factory class it to declare it as final to disable other classes from overriding it, also the factory method must be static and return the class instance based on some sort of condition as shown in the following example.

From the code above we created just one factory class and it contains just one factory static method that takes a argument $type, then we do a simple if statement to return the valid class instance based on that argument.

Fourth Pattern: The Simple Factory Method

The simple factory method differs from the static factory because it is not static. In this pattern instead of having one static factory method, you have a factory method for each class you want to initialize. For this reason the simple pattern is usually preferred over the static pattern.

 

As you see we declared two methods for each class and each method responsible for creating instances.

 

 

Conclusion

In this tutorial we covered the four types of factory pattern and describe them with simple examples. Factories is a good alternative for class instance creation as it provides a standard way of creating objects without knowing the internals of classes.

Share this: