Common design patterns for object-oriented programming using PHP

PHPz
Release: 2023-06-22 11:36:02
Original
738 people have browsed it

As a widely used programming language, PHP can be easily used to implement Object-Oriented Programming (OOP). When developing using OOP, a programmer needs to have a deep understanding of the concept of design patterns. Design patterns refer to general solutions to certain types of problems under specific circumstances. So this article will introduce several common design patterns implemented using PHP.

  1. Singleton Pattern

In the singleton pattern, a class has only one instance. When the repeated creation of instances in some cases will cause program performance consumption, and each instance can apply different states under concurrent conditions, then the singleton mode needs to be used.

The following is a sample code for singleton mode:

class Singleton {
    private static $instance;

    private function __construct() {}

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new singleton();
        }
        return self::$instance;
    }
}
Copy after login

In the above code, the getInstance function will create a singleton and ensure that there can only be A class instance.

  1. Factory Pattern

The factory pattern encapsulates the object creation process in a factory class, which separates the factory class from the specific class and reduces the degree of coupling. . In the factory pattern, a factory class can create instances of multiple categories.

The following is a sample code of the factory pattern:

interface Shape {
    public function draw();
}

class Circle implements Shape {
    public function draw() {
        echo "Circle
";
    }
}

class Rectangle implements Shape {
    public function draw() {
        echo "Rectangle
";
    }
}

class ShapeFactory {
    public function createShape($type) {
        if ($type == 'circle') {
            return new Circle();
        } else if ($type == 'rectangle') {
            return new Rectangle();
        }
    }
}

$shapeFactory = new ShapeFactory();
$circle = $shapeFactory->createShape('circle');
$rectangle = $shapeFactory->createShape('rectangle');

$circle->draw(); //output: Circle
$rectangle->draw(); //output: Rectangle
Copy after login
  1. Observer Pattern (Observer Pattern)

The Observer pattern is when an object is modified A pattern that automatically notifies other objects. In the observer pattern, an observed object can have multiple observers. When the state changes, these observers will receive notifications and automatically update.

The following is a sample code of the observer pattern:

interface Subject {
    public function attach(Observer $observer);
    public function detach(Observer $observer);
    public function notify();
}

class ConcreteSubject implements Subject {
    private $observers = [];

    public function attach(Observer $observer) {
        $this->observers[] = $observer;
    }

    public function detach(Observer $observer) {
        $key = array_search($observer, $this->observers);
        if ($key !== false) {
            unset($this->observers[$key]);
        }
    }

    public function notify() {
        foreach ($this->observers as $observer) {
            $observer->update();
        }
    }
}

interface Observer {
    public function update();
}

class ConcreteObserver implements Observer {
    public function update() {
        echo "Updated!
";
    }
}

$subject = new ConcreteSubject();
$observer1 = new ConcreteObserver();
$observer2 = new ConcreteObserver();

$subject->attach($observer1);
$subject->attach($observer2);

$subject->notify(); //output: Updated! Updated!

$subject->detach($observer2);

$subject->notify(); //output: Updated!
Copy after login

In the above code, Subject and Observer are both interfaces, mainly used for fixation The structure of the observer pattern, ConcreteSubject is a concrete class used to be observed, ConcreteObserver is a concrete observer class.

In PHP, design patterns can be used very flexibly. The above three design patterns are just a few examples of common design patterns implemented using PHP. Programmers need to deeply understand the concept of design patterns in actual development, so as to select appropriate patterns and apply them to programs to solve actual problems.

The above is the detailed content of Common design patterns for object-oriented programming using PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!