In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about the parsing of JavaScript design model Iterator examples. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Iterator Pattern is a very important and simple Pattern: iterator! We can provide an iterator with a unified entry. Client only needs to know what methods or Concrete Iterator are available, not how they are implemented at the bottom! Let's get started now!
Start-up type
The main things of Iterator are two: hasNext and next. Let Client know if there is another one, and switch to the next one!
Define Interface
Interface IteratorInterface {index: number dataStorage: any hasNext (): boolean next (): any addItem (item: any): void}
Implementation interface
In the following example, I will use two common interfaces, Map and Array, to implement.
Class iterator1 implements IteratorInterface {index: number dataStorage: any [] constructor () {this.index = 0 this.dataStorage = []} hasNext (): boolean {return this.dataStorage.length > this.index} next (): any {return this.dataStorage [this.index + +]} addItem (item: any): void {this.dataStorage.push (item)}}
/ / mapclass iterator2 implements IteratorInterface {index: number dataStorage: Map constructor () {this.index = 0 this.dataStorage = new Map ()} hasNext (): boolean {return this.dataStorage.get (this.index)! = undefined} next (): any {return this.dataStorage.get (this.index + +)} addItem (item: any): void {this.dataStorage.set (this.dataStorage.size, item)}}
Client
I did not implement a Client, so I am directly new a category out of direct use!
Const I = new iterator1 () i.addItem, i.addItem (456) i.addItem ('dolphin') while (i.hasNext ()) {console.log (i.next ())} console.log (`=`) const i2 = new iterator2 () i2.addItem (123) i2.addItem (456) i2.addItem (' dolphin') while (i2.hasNext ()) {console.log (i2.next ())}
Conclusion
You will find that the results of Iterator No.1 and No.2 are all the same! They all just need to let Client know that there is hasNext and next, and the underlying implementation does not need to let them know!
This is how the parsing of the Iterator instance of the JavaScript design model shared by the editor is like. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.