Introduction to observer pattern of Java design pattern
When Java goes deep into a certain extent, it will inevitably encounter the concept of design pattern. Understanding design patterns will enable you to have a deeper understanding of the application of interfaces or abstract classes in Java. Design patterns are widely used in Java medium-sized systems. Only by following certain programming patterns can your code be easy to understand and communicate. Observer pattern is a commonly used pattern, especially in interface design. This tutorial focuses on the application of Java in e-commerce systems, Therefore, I want to analyze the application of observer from the example of e-commerce.
Although online shops are the first mock exam, each site has its own characteristics, but there are also its general commonalities. It is a common mode of many online shops, which is similar to Observer patern, just as "the change of goods in order to notify subscribers in time".
Specifically, if the name and price of goods in online stores change, if the system can automatically notify members, it will be a major feature of online stores that distinguishes them from traditional stores. This requires the role of observer to be added to the commodity product, so that when the product details change, the observer can automatically observe the change and perform timely update or notify actions.
The Java API also provides us with a ready-made observer interface util. Observer。 We just need to use it directly.
We must extend Java util. Only observer can really use it: 1 Provide the method of add / delete observer; 2. Provide a method to notify all observers.