Design pattern "Observer"
Real world example
The Observer Design Pattern defines a one-to-many relationship between objects, where a change in one object (the subject) triggers updates to multiple dependent objects (observers).
In a social media context, when a user (subject) posts an update, all their followers (observers) receive notifications about the new post. This pattern helps keep the system modular and scalable.
In plain words
Defines a dependency between objects so that whenever an object changes its state, all its dependents are notified.
Wikipedia definition
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
Programmatic example
Diagram
Loading graph...
Some other examples
Here are 10 real-world use cases for the Observer design pattern:
- Weather Station: A weather station can be observed by various smart devices. When thereβs a change in weather conditions, the weather station notifies all devices about the update.
- Magazine Subscription: A magazine company like ReadDigest generates new content and notifies its subscribers (like a Race Car Driver, a Newspaper Man, and a Movie Director) whenever new content is available.
- Social Media Platforms: When a person updates their status on platforms like Facebook or Twitter, all their followers get the notification.
- Stock Market: Traders subscribe to a stock. When the stock price changes, all traders are notified.
- E-commerce Websites: When a product comes back in stock, all customers who have subscribed to notifications for that product are alerted.
- News Websites: When a news website publishes a new article, all subscribers receive a notification or an email.
- Online Auctions: Bidders subscribe to a product. When thereβs a new highest bid, all bidders are notified.
- Job Posting Websites: Job seekers subscribe to job postings. When a new job is posted that matches their criteria, they receive a notification.
- Email Marketing: When a new blog post or product is launched, all email subscribers receive an email.
- Real-time Data Monitoring: In a real-time data monitoring system, whenever the data changes, the display elements are notified to refresh their display.
These examples illustrate how the Observer Design Pattern allows objects to communicate with each other. The subject doesnβt need to know the concrete classes of its observers, and observers can be added or removed without affecting the subject.
Design pattern "Strategy"
Let's learn what is the "Strategy" design pattern
Design pattern "Visitor"
Let's learn what is the "Visitor" design pattern π€ππΆπ§³
Design pattern "Iterator"
Let's learn what is the "Iterator" design pattern π
Design pattern "Memento"
Let's learn what is the "Memento" design pattern πΈοΈπΌοΈπΌοΈππ°οΈπ
Design pattern "Facade"
Let's learn what is the "Facade" design pattern πΉοΈποΈπ¨
Design pattern "Decorator"
Let's learn what is the "Decorator" design pattern πͺβββ
Design pattern "Composite"
Let's learn what is the "Composite" design pattern π³πΏπΏ
Design pattern "State"
Let's learn what is the "State" design pattern ππ»ββοΈπΆπ»ππ»