In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve a simple data response system in Java". In daily operation, I believe that many people have doubts about how to achieve a simple data response system in Java. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to achieve a simple data response system in Java". Next, please follow the editor to study!
1 、 Dep
In fact, this is a container for dependency collection, depend collects dependencies, notify triggers dependencies
Class Dep {constructor () {this._subs = [];} depend () {this._subs.push (Dep.target)} notify () {this._subs.forEach (item = > {item.fn ();})}} / / is actually the beginning of full love between dep and watcher. Watcher uses / / to store watcherDep.target = null;function pushTarget (watch) {Dep.target = watch through a global attribute. } function popTarget () {Dep.target = null;} 2, learn about obverser
Recursive to convert all properties of the data object to accessor properties
/ / convert to accessor properties function defineReactive (obj, key, val, shallow) {/ / create a dependency collection container let dep = new Dep (); let childOb =! shallow & & observe (val) Object.defineProperty (obj, key, {enumerable: true, configurable: true, get: function reactiveGetter () {if (Dep.target) {/ / Collection dependency dep.depend () } return val; / /...}, set: function reactiveSetter (newVal) {if (newVal = val) return; / / continue recursively traversing observe (newVal); / / trigger dependency dep.notify (); / /...}})} class Observer {constructor (data) {this.walk (data) } walk (data) {const keys = Object.keys (data) for (let I = 0; I
< keys.length; i++) { defineReactive(data, keys[i], data[keys[i]]) } }}// 递归,将 data 对象所有属性转化为访问器属性function observe (data) { if(Object.prototype.toString.call(data) !== '[object Object]') return; new Observer(data);} 此时就可以把任意一个对象的全部属性转为访问器 3、了解 watch 和 observerconst data = { a: 1, b: 2}// 首先监控一个对象observe(data); watcher 的主要功能是检测某个属性,当属性变化时触发一个回调 class Watcher{ /** * @params {Function} exp 一个属性表达式 * @params {Function} fn 回调 */ constructor(exp, fn) { this.exp = exp; this.fn = fn; // 存 watcher // Dep.target = this; pushTarget(this); // 先执行一次表达式函数,会在调用过程中, // 触发到 data.a 的访问器, data.a 的 get 被执行, // 触发 dep.depend() 开始收集依赖 this.exp(); // 释放 Dep.target popTarget(); }}// new Watcher 这样一个依赖就被收集了new Watcher(() =>{return data.a + data.b;}, () = > {console.log ('change')}) 4, trigger dependency data.a = 3; / / changedata.b = 3; / / change so far, the study on "how to implement a simple data response system in Java" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.