Observer Pattern
Observer Pattern References https://refactoring.guru/design-patterns/observer https://ko.wikipedia.org/wiki/%EC%98%B5%EC%84%9C%EB%B2%84_%ED%8C%A8%ED%84%B4 Intent Classification Behavioral design pattern A pattern about how objects communicate within a group. Definition (guru) A pattern that defines a subscription mechanism to notify multiple objects about events that happen to the object they are observing. (wiki) A design pattern in which observers, who are interested in changes to an object’s state, register themselves with the object, and the object notifies each observer directly via methods whenever its state changes. Problem Situation - Notifying Events There are two types of objects: Customer and Store. Customers are interested in the latest iPhone model that will soon be sold at the Store. Problem How can customers buy the product as soon as the latest iPhone is available in the store? Bad Approaches [Method 1] Customer visits every day Customers visit the store every day to check if the latest iPhone has been released. Drawbacks Customers may visit the store even if the product hasn’t been released, resulting in unnecessary effort. [Method 2] Store notifies all customers The store sends emails to all customers whenever a new product is released. This avoids unnecessary visits by customers. Drawbacks Customers who are not interested in the new product still receive notifications, which can be annoying. The store wastes resources. From a programming perspective, the problems can be summarized as: ...