


Detailed explanation of the application of observer pattern in PHP design pattern_PHP tutorial
Observer pattern: Define a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.
Observer class:
1. Abstract theme role: The theme role saves all references to observer objects in a collection, and each theme can have as many an observer. The abstract theme provides an interface for adding and removing observer objects.
2. Abstract observer role: Define an interface for all specific observers and update themselves when the subject of observation changes
3. Specific topic role: Store relevant states to specific observer objects. When the specific topic When the internal state of the object changes, a notification is sent to all registered observers. Concrete theme roles are usually implemented using a concrete subclass.
4. Specific observer role: Store a specific topic object, store related states, and implement the update interface required by the abstract observer role to make its own state consistent with the state of the topic
Function:
1. The coupling of the observer mode is small
2. Supports broadcast communication
//抽象主题
interface Subject{
public function attach($observer);
public function detach($observer);
public function notifyObservers();
}
//具体主题
class ConcreateSubject implements Subject{
private $_observers;
public function __construct(){
$this->_observers = array();
}
public function attach($observer){
return array_push($this->_observers,$observer);
}
public function detach($observer){
$index = array_search($observer,$this->_observers);
if($index === false || !array_key_exists($index,$this->_observers)){
return false;
}
unset($this->_observer[$index]);
return true;
}
public function notifyObservers(){
if(!is_array($this->_observers)){
return false;
}
foreach($this->_observers as $observer){
$observer->update();
}
return true;
}
}
//抽象观察者
interface Observer{
public function update();
}
//具体观察者
class ConcreteObserver implement Observer{
private $_name;
public function __construct($name){
$this->_name = $name;
}
public function update(){
echo 'Observer',$this->_name.'has notified
';
}
}
//客户端
class Client{
public static function main(){
$subject = new ConcreteSubject();
//新增第一个观察者
$observer1 = new ConcreteObserver('Martin');
$subject->attach($observer1);
//通知
$subject->notifyObservers();
//新增第二个观察者
$observer2 = new ConcreteObserver('jaky');
$subject->attach($observer2);
//通知
$subject->notifyObservers();
//删除观察者1
$subject->deatch($observer1);
//通知
$subject->notifyObservers();
}
}
Client::main();
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
