Preface
๐ง Manager Wang: Class is over, class is over. Today we will talk about what is the observer mode. In our usual code practice, many of us have the idea of observer mode. Today, we are full of goods, listening well, we don't understand our classmates. You are still in a daze.
๐ Don't understand: Manager Wang, don't mention it. Xiao Mei hairdressed a circle of friends and said that she wanted to see a movie. I just ordered a compliment and was ready to go to find her to go with me this evening. As a result, there were more than 50 instant messages on her mobile phone. At first glance, all of them were for Xiao Mei. I certainly have no hope.
๐ Don't know: the manager or your honest light, let Xiaomei stay and work overtime with me, don't go to the movies
๐ง Manager Wang: You listened well to today's lesson and did well. I'll let Xiaomei go to the movies with you
๐คช Didn't understand revealed a happy smile: Really, quick, the manager talks quickly
text
๐ง Manager Wang: The observer mode defines a one-to-many dependency between objects, which allows multiple observer objects to listen on a subject at the same time. When the subject object changes, all its dependents (observers) will be notified and updated.
๐ช Don't understand: Zzzzz
๐ Manager Wang: I don't know. You're still sleeping and you don't want to see a movie with Xiaomei.
๐คฅ I don't know how to suck my nose: Of course I want to go. What the manager said is too abstract to understand.
๐ Manager Wang: Definitions are so abstract that you can understand them with coding in a moment. In fact, this definition is very similar. You circled Xiao Mei's friends and you listened to the topic of Xiao Mei's circle of friends. Once someone else circled Xiao Mei's friends, you as an observer can also receive corresponding information.
๐ Don't understand: Although I understand, I am very conscious
๐ Manager Wang: Okay, next, let's take the sign to pick up the license plate as an example, let's start coding
Let's first determine the dto LotteryResult of a rocker result. This dto records some information about the rocker, such as who the rocker is, when the rocker is performed, and what the rocker result is.
Let's let this dto inherit Observable, and after inheritance, this class is an observer object
public class LotteryResult extends Observable { /** * User Name */ private String userName; /** * Rocker result */ private String msg; /** * Shake time */ private Date dateTime; }
Then write a produceLotteryResult method in this dto. The main purpose is to tell the listener that the dto has changed and you can do it yourself
public void produceLotteryResult(LotteryResult result){ // Marker changed setChanged(); // Notify listeners notifyObservers(); }
The observee is ready, and then the listener Send Lottery ResultListener comes on
The listener needs to implement an Observer method to accept information about the observer's changes
The code is as follows
public class SendLotteryResultListener implements Observer { /** * This method is called whenever the observed object is changed. An * application calls an <tt>Observable</tt> object's * <code>notifyObservers</code> method to have all the object's * observers notified of the change. * * @param o the observable object. * @param arg an argument passed to the <code>notifyObservers</code> */ @Override public void update(Observable o, Object arg) { LotteryResult result = (LotteryResult) o; System.out.println(result.getUserName() + "The result of the rocker is" + result.getMsg()); } }
Everything is OK, Start testing
public class Test { public static void main(String[] args) { SendLotteryResultListener listener = new SendLotteryResultListener(); LotteryResult lotteryResult = new LotteryResult(); // Add listeners lotteryResult.addObserver(listener); lotteryResult.setUserName("Zhang San"); lotteryResult.setMsg("Trumpet Success"); lotteryResult.setDateTime(new Date()); lotteryResult.produceLotteryResult(lotteryResult); } } ---------------- Output Results Zhang San's rocker turned out to be a success
Test Successful ๐๐ป๏ผ Our listeners receive information about changes in the observer
๐คจ I don't know: Wonderful, Manager Wang, is this the same as the Spring Event that Yang Xiaoshuai and I talked about before?
๐ค Manager Wang: Don't understand. You're getting started today. You're right. Can you write a demo?
๐คจ I don't know: Yes.
---------------------------------------------------- @RestController public class EventRequest implements ApplicationContextAware { private ApplicationContext appContext; @RequestMapping("/testEvent") public String testEventSave(String name) { appContext.publishEvent(new LotteryResult(this, name)); return "ok"; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { appContext = applicationContext; } } -------------------------------------------------- // monitor @Component public class WebEventListener { /** * Only if the listening field value is Lao Wang, the class is User.class time */ @EventListener(classes = LotteryResult.class, condition = "#result.name =='King') public void listen(LotteryResult result){ // TODO } /** * Listen for LotteryResult.class case */ @EventListener(classes = LotteryResult.class) public void listen1(LotteryResult result){ // TODO } }
๐ค Manager Wang: Good. That's the end of today's class.
๐ฅบ Don't understand: Don't you mean, let me watch movies with Beauty?
๐ค Manager Wang cleared up his computer and went away: you don't see what time it is either. You went to the movies with others early in the morning.
Leave me unable to understand crying silently
summary
As a mode of behavior, the observer mode is structurally full open and close. When you need to add other monitoring events or modify the listening logic, it is not
The event handling class needs to be changed. However, you may not be able to control the sequence and the return of some event results to continue, so you need to consider the rationality of the scene when making the process.
End of Article ๐คฃ
If this article has helped you, be a little complimentary.
Share more on the WeChat Public Number (codeLiveHouse)
Public Number Reply "Materials" to get factory interview questions/technical documents/e-books, etc.