Observer mode
The observer pattern defines one to many dependencies between objects, so that when an object changes state, all its dependencies are notified and automatically updated
The observer model has two roles, one is the observer, the other is the observed (subject), which can also be considered as the subscriber and publisher.
It's the observer who receives the notice.
If the words "Observer" and "observed" have been confused, we can use newspaper publishing subscription to cover them. The customer is the person who subscribes to the newspaper and receives the notice, so the customer is the observer, so the newspaper office is the observed.
Use
/** * abstract class * @author aodeng-Low profile Panda * @since 19-7-15 */ public abstract class Customer { /** * <p> * Define an abstract method * </p> * @author aodeng * @since 19-7-15 */ public abstract void update(); }
/** * Notified observer customer A * @author aodeng-Low profile Panda * @since 19-7-15 */ public class CustomerA extends Customer { @Override public void update() { System.out.println("I am a client. A,I got the paper!"); } }
/** * Notified observer customer B * @author aodeng-Low profile Panda * @since 19-7-15 */ public class CustomerB extends Customer { @Override public void update() { System.out.println("I am a client. B,I got the paper"); } }
/** * Observed newspaper * @author aodeng-Low profile Panda * @since 19-7-15 */ public class NewsOffice { private List<Customer> customerList=new ArrayList<>(); public void add(Customer customer){ this.customerList.add(customer); } /** * <p> * Here comes the simulation new newspaper * </p> * @author aodeng * @since 19-7-15 */ public void newspaperCome(){ this.notifyAllObservers(); } public void notifyAllObservers(){ for (Customer customer : customerList) { customer.update(); } } }
test
public static void main(String[] args) { System.out.println("Hello World!"); NewsOffice newsOffice=new NewsOffice(); newsOffice.add(new CustomerA()); newsOffice.add(new CustomerB()); newsOffice.newspaperCome(); } Output: Hello World! I'm customer A, I got the newspaper! I'm client B. I got the paper
Strengthen
/** * Define the observed interface * @author aodeng-Low profile Panda * @since 19-7-15 */ public interface ISubject { public void registerObserver(Customer customer); public void removeObserver(Customer customer); public void notifyObservers(); }
/**Enhanced edition of the observed newspaper * @author aodeng-Low profile Panda * @since 19-7-15 */ public class NewsOfficeNiu implements ISubject { private List<Customer> customerList = new ArrayList<>(); @Override public void registerObserver(Customer customer) { this.customerList.add(customer); } @Override public void removeObserver(Customer customer) { customerList.remove(customer); } @Override public void notifyObservers() { for (Customer customer : customerList) { customer.update(); } } //Here comes the mock newspaper public void newspaperCome(){ this.notifyObservers(); } }
test
System.out.println("enhanced version ========"); ISubject iSubject=new NewsOfficeNiu(); Customer customera=new CustomerA(); iSubject.registerObserver(customera); iSubject.removeObserver(customera); iSubject.registerObserver(new CustomerB()); ((NewsOfficeNiu) iSubject).newspaperCome(); Output: Enhanced version======== I'm client B. I got the paper
Observer pattern provided by Java
Java has provided us with the classes required by observer pattern: Observer class and Subject class. In Java, the Subject is not called Subject, but is called Observable.
copy a demo
//demo public class NewsOffice2 extends Observable { /** * Here comes the mock newspaper */ public void newspaperCome(){ this.setChanged(); this.notifyObservers(); } } public class CustomerC implements Observer { @Override public void update(Observable o, Object arg) { System.out.println("I am a client. C,I got the message"); } } //test Observableoffice = new NewsOffice2(); Observer observer = new CustomerC(); office.addObserver(observer); ((NewsOffice2) office).newspaperCome();
summary
java takes thread safety into consideration, but it is convenient to write by itself, which has advantages and disadvantages
1. advantages
There is an abstract coupling between the observer and the observed, and there is a set of trigger mechanism. The observed does not need to know who the notification object is, as long as it conforms to the observer interface.
2. disadvantages
The observer only knows the changes of the observed, but can't know how, such as modifying the name field or other fields, the observer doesn't know.
If there are many observers, one by one notification is more time-consuming.
Source code
Source address: https://github.com/java-aoden...
Links:
- CI/CD process and principle description
- Github
- Personal blog: ilovey.live (new domain name, love life)
- Giant panda base
- Welcome to the public number.
This article is released by the low-key panda one article multiple sending operation! Welcome to public address: low key panda.