Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C++ publishes subscriptions and watcher mode

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how C++ publishes subscriptions and Observer Mode". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how C++ publishes subscriptions and observers mode".

1. Realization ideas

Arr acts as a cache center for subscribed events

Push arr cache the things that need to be done through on in the array

Emit executes the event in turn when waiting for the event to be triggered

2. Code implementation

Interface eventHub {arr: Array; on (fn: Function): void; emit (): void;} interface Person {age: number; name: string;} let eventHub: eventHub = {arr: [] as Array, / / subscribe to on (fn: Function) {this.arr.push (fn);}, / / publish emit () {this.arr.forEach ((fn) = > fn ());},}; let person: Person = {} as Person EventHub.on (() = > {/ / subscription event determines that person is printed when person length is 2, if (Object.keys (person). Length = = 2) {console.log (person);}}); setTimeout (function () {person.age = 27; / / publish time to traverse this.arr and execute the first eventHub.emit ();}, 10); setTimeout (function () {person.name = "Zoe") / / traverse the this.arr and execute the second eventHub.emit ();}, 20)

3. Results

Although it was released twice, in the end, the console in on was only executed once because of external conditions.

Observer mode

1. Realization ideas

Similar to the observer model, but requires a distinction between the observer and the observed

There is an association between the observer and the observed (internally based on the publish-subscribe model)

2. Code implementation

/ / define a name attribute state on the class Subject {name:string; / / instance: string; observers: any []; constructor (name:string) {this.name = name; this.observers = []; this.state = "";} attach (o) {/ / pass in the observer this.observers.push (o);} setState (newState) {this.state = newState This.observers.forEach ((o) = > o.update (this));}} / / Observer class Observer {name: string; constructor (name) {this.name = name;} update (interviewee) {console.log (`${interviewee.name} say to: ${interviewee.state}` of ${this.name} ZOE);} let hr = new Subject ("HR"); let observer1 = new Observer ("intruder") Let observer2 = new Observer ("interviewer"); hr.attach (observer1); hr.attach (observer2); hr.setState ("passed the interview"); / / baby.setState ("failed the interview")

3. Realize the result

The difference between the two eventHub publish subscriptions

There is no direct connection between on (subscription) and publication (emit). It relies on the middle arr to make a connection and subscribe from push to arr, and execute arr in turn when emit.

Observer mode

There is an association between the observer and the observed (internally based on the publish-subscribe model)

Pass the instance of the observer as a parameter to the observed's attach method and cache it in the observers array

When the observer setState, call the update method of the observer in the cache array observers in turn

At this point, I believe you have a better understanding of "how C++ publishes subscriptions and Observer Mode". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report