In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Java Observer pattern case Analysis". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Java Observer pattern case Analysis".
Working with scen
What the observer pattern does is decouple, so that both sides of the coupling rely on abstraction rather than concrete, so that their own changes do not affect changes on the other side.
When a change in one object needs to change other objects, and it doesn't know exactly how many objects need to be changed, you should consider using the observer pattern.
An abstract model has two aspects, one of which depends on the other, and the observer pattern can be used to encapsulate the two in separate objects so that they can be changed and reused independently.
For example, public interface ISubject {void Notify (); string SubjectState {get; set;}} public class Boss: ISubject {private readonly IList _ observers = new List (); public void Attach (Observer observer) {_ observers.Add (observer);} public void Detach (Observer observer) {_ observers.Remove (observer);} public void Notify () {foreach (var observer in _ observers) {observer.Update () }} public string SubjectState {get; set;}} public abstract class Observer {protected string Name; protected ISubject Subject; protected Observer (string name, ISubject subject) {Name = name; Subject = subject;} public abstract void Update () } public class StockObserver: Observer {public StockObserver (string name, ISubject subject): base (name, subject) {} public override void Update () {Console.WriteLine ($"{Name} {Subject.SubjectState} close the stock market and continue to work") } public class NBAObserver: Observer {public NBAObserver (string name, ISubject subject): base (name, subject) {} public override void Update () {Console.WriteLine ($"{Name} {Subject.SubjectState} close NBA LVB and continue working");} var boss = new Boss (); var stockObserver = new StockObserver ("Wei Guanfu", boss); var nbaObserver = new NBAObserver ("easy to check", boss); boss.Attach (stockObserver); boss.Attach (nbaObserver) Boss.Detach (stockObserver); boss.SubjectState = "Boss I Hu Hanshan is back"; boss.Notify ()
With event (delegate), we can implement a flexible observer pattern, and we have defined a way for a new boss to demonstrate events. Take a look at the following example:
Public class NewBoss: ISubject {public event Action Update; public void Notify () {Update?.Invoke ();} public string SubjectState {get; set;}} public class GamePlayerObserver {private readonly string _ name; private readonly ISubject _ subject; public GamePlayerObserver (string name, ISubject subject) {_ name = name; _ subject = subject } public void CloseGame () {Console.WriteLine ($"{_ name} {_ subject.SubjectState} close the LOL game and continue to work");}} var newBoss = new NewBoss (); var stockObserver = new StockObserver ("Wei Guanju", boss); var nbaObserver = new NBAObserver ("easy to check", boss); var gameObserver = new GamePlayerObserver ("West Gate", newBoss); / / Registration Notification event newBoss.Update + = stockObserver.Update;newBoss.Update + = nbaObserver.Update NewBoss.Update + = gameObserver.CloseGame;newBoss.Update-= stockObserver.Update;newBoss.SubjectState = "Boss I Hu Hanshan is back"; newBoss.Notify ()
As you can see from the above example, through the way of events, we do not need to display the abstract class inherited from Observer, and we can be more flexible and extensible, which is an important reason why events are used to extend many class libraries.
More
What the design pattern does is decouple. The creative pattern is to decouple the code to be created and used, the structural pattern is to decouple the code of different functions, and the behavioral pattern is to decouple the different behavior code, which is specific to the observer pattern, which decouples the code of the observer and the observed.
According to the different application scenarios, the observer pattern will correspond to different code implementations: synchronous blocking, asynchronous non-blocking, intra-process and cross-process.
Define an one-to-many dependency between objects, and all dependent objects are automatically notified when the state of an object changes. In general, the dependent object is called the Observable, and the dependent object is called the Observer. However, in the actual project development, the names of these two objects are more flexible, there are a variety of different names, such as: Subject-Observer, Publisher-Subscriber, Producer-Consumer, EventEmitter-EventListener, Dispatcher-Listener. Whatever you call it, as long as the application scenario meets the definition just given, it can be regarded as the observer pattern.
EventBus (event bus) is a practical application of the observer pattern.
Thank you for your reading. the above is the content of "case Analysis of Java Observer Mode". After the study of this article, I believe you have a deeper understanding of the problem of case analysis of Java Observer Mode, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.