Current Location: Home> Latest Articles> Implementing Object Decoupling and Code Optimization with PHP OOP Simple Factory Pattern

Implementing Object Decoupling and Code Optimization with PHP OOP Simple Factory Pattern

M66 2025-10-10

What is the Simple Factory Pattern

The Simple Factory Pattern is a creational design pattern that delegates object creation to a factory class instead of directly instantiating objects in the business logic. This separates the object creation process from business logic, enhancing system flexibility and scalability. In PHP development, the Simple Factory Pattern can be used to create different types of objects, achieving object decoupling and code refactoring.

Example: Implementing Simple Factory Pattern with PHP OOP

The following example demonstrates how to implement object decoupling using the Simple Factory Pattern.

Creating the Animal Interface

First, define an Animal interface to declare common behaviors for animals.

<?php
interface Animal {
    public function eat();
    public function speak();
}
?>

Implementing Concrete Classes Cat and Dog

Create two classes that implement the Animal interface, representing a cat and a dog.

<?php
class Cat implements Animal {
    public function eat() {
        echo "Cat is eating";
    }
    
    public function speak() {
        echo "Cat is meowing";
    }
}

class Dog implements Animal {
    public function eat() {
        echo "Dog is eating";
    }
    
    public function speak() {
        echo "Dog is barking";
    }
}
?>

Creating the Simple Factory Class AnimalFactory

The factory class creates the corresponding animal object based on the input parameter.

<?php
class AnimalFactory {
    public static function create($animalType) {
        switch ($animalType) {
            case "cat":
                return new Cat();
            case "dog":
                return new Dog();
            default:
                throw new Exception("Unsupported animal type: " . $animalType);
        }
    }
}
?>

Client Usage Example

In the client code, objects are created through the factory class and methods are called:

<?php
$cat = AnimalFactory::create("cat");
$cat->eat();
$cat->speak();

$dog = AnimalFactory::create("dog");
$dog->eat();
$dog->speak();
?>

Advantages of Using the Simple Factory Pattern

With the Simple Factory Pattern, the client code does not need to worry about the details of object creation and only calls the factory method. This achieves:

  • Reduced coupling between objects
  • Improved code maintainability and scalability
  • Enhanced development efficiency and code quality

Conclusion

The PHP OOP Simple Factory Pattern is an efficient design approach. By encapsulating the object creation process in a factory class, it enables object decoupling and code refactoring. In practical development, mastering and applying the Simple Factory Pattern can significantly improve system flexibility and maintainability, making it an essential skill for PHP developers.