The Design Pattern Observer: a powerful notification system

DDD
Release: 2024-09-13 06:33:11
Original
861 people have browsed it

In the development of your applications, you will often need your application to be able to react to events flexibly. For example, let's say you want to notify multiple systems when certain actions occur (like user registration). This is where the Pattern Observer comes in. This pattern allows you to establish a relationship between objects, so that when the state of one changes, all the others are automatically informed and updated.

Symfony already effectively implements this pattern thanks to the Event Dispatcher, which makes it very practical and powerful to use in your projects.

What is the Pattern Observer?

The Pattern Observer allows you to define a relationship between the observed subject and one or more observers. When a change of state occurs on the observed object, all observers are notified so that they can react accordingly.

Here’s how it works:

  • ? Subject (Observable): The main object that changes state.
  • ? Observers: Objects that react to changes in the observable.

Example

Let's imagine you have a site where a user can register. With each registration, you want to send a welcome email, add the user to a newsletter list and inform an analytics system.

Rather than rigidly coding everything in one place, you can delegate these tasks to different observers who will be informed as soon as the "registered user" event is triggered.

Implementing the Pattern Observer in Symfony

In Symfony, you will use the Event Dispatcher to set up the Pattern Observer. This will allow you to organize the code neatly and make it extensible without modifying the basic logic.

Step 1️⃣: Declare the Event

We will start by creating an event which will be dispatched when a user registers. The latter will contain the user's information.

Le Design Pattern Observer : un système de notification puissant

Step 2️⃣: Dispatch the Event

Now, in your controller or service, you will dispatch this event when a user registers.

Le Design Pattern Observer : un système de notification puissant

Step 3️⃣: Create Observers (Listeners)

Next, you will have to create the observers, which will be called each time the registration event is dispatched. Here, you have the example where we send an email and we add the user to a newsletter list.

Observer 1: Send a welcome email

Le Design Pattern Observer : un système de notification puissant

Watcher 2: Add user to a newsletter list

Le Design Pattern Observer : un système de notification puissant

Step 4️⃣: Configuring Observers

You must now register the listeners in the configuration so that they listen to the user.registered event.

In config/services.yaml, add observers as services:

Le Design Pattern Observer : un système de notification puissant

ℹ️ Since version 5.3 of Symfony, you can use PHP attributes to configure services and event listeners. This is a more modern approach that allows you to declare events directly in classes, instead of using the services.yaml file.
You can therefore use the #[AsEventListener] attribute directly on your listener method.

I'll show you how to adapt the two observers with attributes (so no need for special config in config/services.yaml? :

Observer 1

Le Design Pattern Observer : un système de notification puissant

Observer 2

Le Design Pattern Observer : un système de notification puissant

一點解釋

  • #[AsEventListener] 屬性指示方法是特定事件的監聽器。
  • 屬性的第一個參數是觀察者正在監聽的事件的名稱(UserRegisteredEvent::NAME)。
  • 最後,method 參數指定事件觸發時將執行的類別的方法(在我們的例子中為 onUserRegistered)。 ?

步驟5️⃣步:測試實施

當您使用 RegistrationController 註冊使用者時,將調度該事件,並且 Symfony 會自動呼叫對應的觀察者。電子郵件已發送,用戶將添加到新聞通訊清單中,而這些操作/邏輯不會與您的業務代碼混合。

為什麼要使用模式觀察器?

因為它是必需品!它將允許您的應用程式:

  1. 更靈活:您可以新增或修改觀察者,而無需觸及主程式碼。只需添加一個監聽器即可!
  2. 耦合度較低:應用程式的不同部分沒有緊密連結。例如,發送電子郵件和新增新聞通訊不是直接在註冊控制器中編碼的。
  3. 更具可擴展性:這種模式可以輕鬆地對可能涉及多個系統或服務(通知、分析、發送等)的事件做出反應。

獎勵:與 DDD(領域驅動設計)的鏈接

模式觀察器通常在基於 DDD(領域驅動設計)的架構中找到自己的位置,其中事件是關鍵元素(領域事件)。這些事件允許系統的不同部分做出反應,通常在主域之外。但這是未來關於 DDD 的完整文章的討論!

The above is the detailed content of The Design Pattern Observer: a powerful notification system. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
Latest Articles by Author
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!