Home > Article > Backend Development > php design pattern, design pattern_PHP tutorial
A brief description of several patterns:
1. Factory mode
Factory pattern is a class that has certain methods that create objects for you.
You can use factory classes to create objects instead of using new directly. This way, if you want to change the type of object created, you only need to change the factory. All code using this factory is automatically changed.
Functions and classes in one part of the system are heavily dependent on the behavior and structure of functions and classes in other parts of the system.
You want a set of patterns that allow these classes to communicate with each other, but you don't want to tie them tightly together to avoid interlocking.
In large systems, a lot of code depends on a few key classes. Difficulties may arise when these classes need to be changed.
2. Single element mode
Some application resources are exclusive because there is only one resource of this type.
For example, connections to a database via a database handle are exclusive.
You want to share the database handle across your application because it is an overhead when keeping the connection open or closed, even more so during the process of fetching a single page.
3. Observer mode
The Observer pattern gives you another way to avoid tight coupling between components.
The pattern is very simple: an object makes itself observable by adding a method that allows another object, an observer, to register itself.
When an observable changes, it sends messages to registered observers. These observers use this information to perform operations independent of the observable object. The result is that objects can talk to each other without having to understand why.
4. Command chain mode
The Command Chain pattern is based on loosely coupled topics, sending messages, commands and requests, or sending arbitrary content through a set of handlers.
Each handler determines for itself whether it can handle the request. If it can, the request is processed and the process stops. You can add or remove handlers from the system without affecting other handlers.
5. Strategy mode
The last design pattern we talk about is the strategy pattern. In this pattern, algorithms are extracted from complex classes and can therefore be easily replaced.
For example, if you want to change the way pages are ranked in search engines, Strategy mode is a good choice.
Think about the parts of a search engine - one that traverses pages, one that ranks each page, and another that sorts the results based on the ranking.
In complex examples, these parts are all in the same class. By using the Strategy pattern, you can put the arrangement part into another class to change the way the page is arranged without affecting the rest of the search engine's code.
Detailed explanation of common modes:
1. Singleton mode (three private and one public)
①. Private constructor (access control: prevent external code from using the new operator to create objects. Singleton classes cannot be instantiated in other classes and can only be instantiated by themselves)
②. Private static properties (having a static member variable that holds an instance of the class)
③. Private cloning method (has a public static method to access this instance. The getInstance() method is commonly used to instantiate a singleton class. The instanceof operator can be used to detect whether the class has been instantiated)
④, public static methods (prevent objects from being copied)
The so-called singleton mode means that at any time, only one instance of this class will exist in the application.
Commonly, we use the singleton mode to only allow one object to access the database, thereby preventing multiple database connections from being opened.
To implement a singleton class should include the following points:
Unlike ordinary classes, singleton classes cannot be instantiated directly, but can only be instantiated by themselves. Therefore, to obtain such restrictive effects, the constructor must be marked private.
For a singleton class to function without being instantiated directly, such an instance must be provided for it.
Therefore, it is necessary for the singleton class to have a private static member variable that can save the instance of the class and a corresponding public static method that can access the instance.
In PHP, in order to prevent the cloning of the singleton class object from breaking the above implementation form of the singleton class, an empty private __clone() method is usually provided for the base.
The following is a basic singleton pattern:
class SingetonBasic {
private static $instance;
private function __construct() {
// do construct..
}
private function __clone() {}
public static function getInstance() {
if (!(self::$instance instanceof self)) {
self::$instance = new self();
}
return self::$instance;
}
}
$a = SingetonBasic::getInstance();
$b = SingetonBasic::getInstance();
var_dump($a === $b);
2. Factory mode
The factory pattern allows you to create a class specifically designed to implement and return instances of other classes based on input parameters or application configuration. The following is the most basic factory pattern:
class FactoryBasic {
public static function create($config) {
}
}
For example, here is a factory that describes shape objects. It hopes to create different shapes based on the number of parameters passed in.
// Define the public function of the shape: get the perimeter and area.
interface IShape {
function getCircum();
function getArea();
}
// Define rectangle class
class Rectangle implements IShape {
private $width, $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function getCircum() {
return 2 * ($this->width $this->height);
}
public function getArea() {
return $this->width * $this->height;
}
}
//Define circle class
class Circle implementations IShape {
private $radii;
public function __construct($radii) {
$this->radii = $radii;
}
public function getCircum() {
return 2 * M_PI * $this->radii;
}
public function getArea() {
return M_PI * pow($this->radii, 2);
}
}
// Create different shapes based on the number of parameters passed in.
class FactoryShape {
public static function create() {
switch (func_num_args()) {
case 1:
return new Circle(func_get_arg(0));
break;
case 2:
return new Rectangle(func_get_arg(0), func_get_arg(1));
break;
}
}
}
// Rectangular object
$c = FactoryShape::create(4, 2);
var_dump($c->getArea());
// Circle object
$o = FactoryShape::create(2);
var_dump($o->getArea());
Using the factory pattern makes it easier to call methods, because it only has one class and one method. If the factory pattern is not used, you have to decide which class and which method should be called at the time of the call;
Using the factory pattern also makes it easier to make changes to the application in the future. For example, if you want to add support for a shape, you only need to modify the create() method in the factory class, instead of using the factory pattern. To modify the code block that calls the shape.
3. Observer mode
The Observer pattern gives you another way to avoid tight coupling between components.
The pattern is very simple: an object makes itself observable by adding a method that allows another object, the observer, to register itself.
When an observable changes, it sends messages to registered observers. These observers use this information to perform operations independent of the observable object. The result is that objects can talk to each other without having to understand why.
A simple example: when a listener is listening to the radio (i.e. the radio joins a new listener), it will send out a prompt message, which can be observed by the log observer who sent the message.
// Observer interface
interface IObserver {
function onListen($sender, $args);
function getName();
}
// Observable interface
interface IObservable {
function addObserver($observer);
function removeObserver($observer_name);
}
// Observer class
abstract class Observer implements IObserver {
protected $name;
public function getName() {
return $this->name;
}
}
// Observable class
class Observable implements IObservable {
protected $observers = array();
public function addObserver($observer) {
if (!($observer instanceof IObserver)) {
return;
}
$this->observers[] = $observer;
}
public function removeObserver($observer_name) {
foreach ($this->observers as $index => $observer) {
if ($observer->getName() === $observer_name) {
array_splice($this->observers, $index, 1);
return;
}
}
}
}
// Simulate a class that can be observed: RadioStation
class RadioStation extends Observable {
public function addListener($listener) {
foreach ($this->observers as $observer) {
$observer->onListen($this, $listener);
}
}
}
// Simulate an observer class
class RadioStationLogger extends Observer {
protected $name = 'logger';
public function onListen($sender, $args) {
echo $args, ' join the radiostation.
';
}
}
// Simulate another observer class
class OtherObserver extends Observer {
protected $name = 'other';
public function onListen($sender, $args) {
echo 'other observer..
';
}
}
$rs = new RadioStation();
//Inject observer
$rs->addObserver(new RadioStationLogger());
$rs->addObserver(new OtherObserver());
//Remove observers
$rs->removeObserver('other');
//You can see the observed information
$rs->addListener('cctv');