DELAPARRA
Index
Color
NL
Category: Design patterns

Observer

Explained by Updated

What is Observer?

Observer: Define a one-to-many dependency so that when one object changes state, all its dependents are notified and updated automatically. The Observer pattern lets objects subscribe to changes in another object. When the subject changes, it notifies all its observers automatically, without knowing who they are or what they do. Laravel's model observers and event listeners follow this pattern. When an order is created, observers can send a confirmation email, update stock and notify the warehouse, while the order code stays unaware of all of them.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Observer

What should you keep in mind with Observer?

This keeps code loosely coupled and easy to extend. The downside is that behaviour is spread out, so it is not obvious from the order code what happens on save. Keep observers focused, document what listens to what, and move slow work like emails and API calls to queued listeners so they do not slow down the original request. Be careful with observers that trigger other observers.

At a glance

CategoryDesign patterns
Explained by
Updated

More Design patterns terms

All terms starting with O

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.