Since bloggers have recently reviewed the basics and learned new knowledge points over and over again, blog articles published will not be published in sequence, but are well categorized.
Learn together and make progress together. Continue to precipitate, slowly strong. I hope this article will be helpful to you. If there is something wrong with it, please comment on it.
Shortly after I started blogging, I was Yang Zhanhao. This is my eleventh blog. Come on!!!
Share the basic knowledge of Java today - > Observer Design Patterns!!! Learn to use an interface Observer and a class Observable provided by Java documents for observer design patterns.
First, the basic principle of the observer design pattern is briefly introduced: the design pattern is divided into two roles, one is the observer, the other is the observer.
For example, two dogs (teasing, fur balls) and their shit shoveling officers. When the shit shovel officer holds the dog food and orders the teaser and the wool ball to sit well, the observer is the teaser and the wool ball (staring at the dish of dog food held by the shit shovel officer), so the object of observation is the dog food.
That's the way it works. Here's a wave of code experiments:
Write a class for the observer (here dog food is defined as the observer):
package com.java8090.Watcher; import java.util.Observable; /** * @author zhanhao * @create 2019-09-10 14:11 */ public class DogFood extends Observable { private Integer amount; public DogFood (Integer amount){ System.out.println("Initial Dog Food Quantity:" + amount); this.amount = amount; } public void setAmount(Integer amount){ if(amount > this.amount){ System.out.println("The number of dog food changed:" + amount); // It means that the amount of dog food has changed. super.setChanged(); // Synchronized notification to observers concerned about dog food super.notifyObservers(amount); } this.amount = amount; } }
Write an observer class (here dogs are defined as observers):
package com.java8090.Watcher; import java.util.Observable; import java.util.Observer; /** * @author zhanhao * @create 2019-09-10 14:14 */ public class DogFoodObserver implements Observer { private String name; public DogFoodObserver(String name){ this.name = name; } @Override public void update(Observable o, Object arg) { if(o instanceof DogFood){ DogFood dogFood = (DogFood) o; Integer dogFoodAmount = (Integer) arg; System.out.println("Concerned dog food:" + dogFood + " The amount has changed:" + dogFoodAmount); } } }
Testing class starts:
package com.java8090.Watcher; /** * @author zhanhao * @create 2019-09-10 14:09 */ public class WatcherTest { public static void main(String[] args){ // Define the Observed Object - > Dog Food DogFood dogFood = new DogFood(0); // Define the Observer Object - > Observers Concerned about Dog Food DogFoodObserver dfo1 = new DogFoodObserver("Dog No. 1 Concerned about Dog Food"); DogFoodObserver dfo2 = new DogFoodObserver("Dog No. 2 Concerned about Dog Food"); DogFoodObserver dfo3 = new DogFoodObserver("Dog No. 3 Focusing on Dog Food"); // Connect the observer with the observer dogFood.addObserver(dfo1); dogFood.addObserver(dfo2); dogFood.addObserver(dfo3); // When the amount of dog food changes, all observers are notified. dogFood.setAmount(100); } }
Implementation results:
Summarize: It's enough to know about an interface Observer and a class Observable provided by Java.
Observer design pattern development: The relationship between the observer and the observer needs to be correlated, and the observer will be notified synchronously when the observer changes.