development:software-architecture:design-patterns:observer
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
development:software-architecture:design-patterns:observer [2024/08/15 07:33] – [Observer] tungnt | development:software-architecture:design-patterns:observer [2024/08/19 09:51] (current) – [Ví dụ 3:] tungnt | ||
---|---|---|---|
Line 178: | Line 178: | ||
===== Ví dụ 3: ===== | ===== Ví dụ 3: ===== | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | <file php> | ||
+ | <?php | ||
+ | |||
+ | namespace RefactoringGuru\Observer\Conceptual; | ||
+ | |||
+ | /** | ||
+ | * PHP has a couple of built-in interfaces related to the Observer pattern. | ||
+ | * | ||
+ | * Here's what the Subject interface looks like: | ||
+ | * | ||
+ | * @link http:// | ||
+ | * | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | * | ||
+ | | ||
+ | | ||
+ | * | ||
+ | | ||
+ | | ||
+ | | ||
+ | * | ||
+ | * There' | ||
+ | * | ||
+ | * @link http:// | ||
+ | * | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | /** | ||
+ | * The Subject owns some important state and notifies observers when the state | ||
+ | * changes. | ||
+ | */ | ||
+ | class Subject implements \SplSubject | ||
+ | { | ||
+ | /** | ||
+ | * @var int For the sake of simplicity, the Subject' | ||
+ | * all subscribers, | ||
+ | */ | ||
+ | public $state; | ||
+ | |||
+ | /** | ||
+ | * @var \SplObjectStorage List of subscribers. In real life, the list of | ||
+ | * subscribers can be stored more comprehensively (categorized by event | ||
+ | * type, etc.). | ||
+ | */ | ||
+ | private $observers; | ||
+ | |||
+ | public function __construct() | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The subscription management methods. | ||
+ | */ | ||
+ | public function attach(\SplObserver $observer): void | ||
+ | { | ||
+ | echo " | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function detach(\SplObserver $observer): void | ||
+ | { | ||
+ | $this-> | ||
+ | echo " | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Trigger an update in each subscriber. | ||
+ | */ | ||
+ | public function notify(): void | ||
+ | { | ||
+ | echo " | ||
+ | foreach ($this-> | ||
+ | $observer-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Usually, the subscription logic is only a fraction of what a Subject can | ||
+ | * really do. Subjects commonly hold some important business logic, that | ||
+ | * triggers a notification method whenever something important is about to | ||
+ | * happen (or after it). | ||
+ | */ | ||
+ | public function someBusinessLogic(): | ||
+ | { | ||
+ | echo " | ||
+ | $this-> | ||
+ | |||
+ | echo " | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Concrete Observers react to the updates issued by the Subject they had been | ||
+ | * attached to. | ||
+ | */ | ||
+ | class ConcreteObserverA implements \SplObserver | ||
+ | { | ||
+ | public function update(\SplSubject $subject): void | ||
+ | { | ||
+ | if ($subject-> | ||
+ | echo " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class ConcreteObserverB implements \SplObserver | ||
+ | { | ||
+ | public function update(\SplSubject $subject): void | ||
+ | { | ||
+ | if ($subject-> | ||
+ | echo " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The client code. | ||
+ | */ | ||
+ | |||
+ | $subject = new Subject(); | ||
+ | |||
+ | $o1 = new ConcreteObserverA(); | ||
+ | $subject-> | ||
+ | |||
+ | $o2 = new ConcreteObserverB(); | ||
+ | $subject-> | ||
+ | |||
+ | $subject-> | ||
+ | $subject-> | ||
+ | |||
+ | $subject-> | ||
+ | |||
+ | $subject-> | ||
+ | </ | ||
+ | |||
+ | ===== Ví dụ 4: ===== | ||
<file php> | <file php> |
development/software-architecture/design-patterns/observer.1723707192.txt.gz · Last modified: 2024/08/15 07:33 by tungnt