1"use strict";(self.webpackChunkjanzhou_123_github_io=self.webpackChunkjanzhou_123_github_io||[]).push([[3776],{3905:(e,t,r)=>{r.d(t,{Zo:()=>u,kt:()=>m});var n=r(7294);function a(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function i(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function o(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?i(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):i(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function l(e,t){if(null==e)return{};var r,n,a=function(e,t){if(null==e)return{};var r,n,a={},i=Object.keys(e);for(n=0;n<i.length;n++)r=i[n],t.indexOf(r)>=0||(a[r]=e[r]);return a}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(n=0;n<i.length;n++)r=i[n],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(a[r]=e[r])}return a}var c=n.createContext({}),s=function(e){var t=n.useContext(c),r=t;return e&&(r="function"==typeof e?e(t):o(o({},t),e)),r},u=function(e){var t=s(e.components);return n.createElement(c.Provider,{value:t},e.children)},p="mdxType",d={inlineCode:"code",wrapper:function(e){var t=e.children;return n.createElement(n.Fragment,{},t)}},f=n.forwardRef((function(e,t){var r=e.components,a=e.mdxType,i=e.originalType,c=e.parentName,u=l(e,["components","mdxType","originalType","parentName"]),p=s(r),f=a,m=p["".concat(c,".").concat(f)]||p[f]||d[f]||i;return r?n.createElement(m,o(o({ref:t},u),{},{components:r})):n.createElement(m,o({ref:t},u))}));function m(e,t){var r=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var i=r.length,o=new Array(i);o[0]=f;var l={};for(var c in t)hasOwnProperty.call(t,c)&&(l[c]=t[c]);l.originalType=e,l[p]="string"==typeof e?e:a,o[1]=l;for(var s=2;s<i;s++)o[s]=r[s];return n.createElement.apply(null,o)}return n.createElement.apply(null,r)}f.displayName="MDXCreateElement"},9831:(e,t,r)=>{r.r(t),r.d(t,{assets:()=>c,contentTitle:()=>o,default:()=>d,frontMatter:()=>i,metadata:()=>l,toc:()=>s});var n=r(7462),a=(r(7294),r(3905));const i={title:"\u8fed\u4ee3\u5668\u6a21\u5f0f (Iterator)",slug:"iterator",sidebar_position:10},o=void 0,l={unversionedId:"pattern/iterator",id:"pattern/iterator",title:"\u8fed\u4ee3\u5668\u6a21\u5f0f (Iterator)",description:"\u6982\u5ff5",source:"@site/docs/pattern/iterator.md",sourceDirName:"pattern",slug:"/pattern/iterator",permalink:"/docs/pattern/iterator",draft:!1,editUrl:"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/docs/pattern/iterator.md",tags:[],version:"current",sidebarPosition:10,frontMatter:{title:"\u8fed\u4ee3\u5668\u6a21\u5f0f (Iterator)",slug:"iterator",sidebar_position:10},sidebar:"tutorialSidebar",previous:{title:"\u5916\u89c2\u6a21\u5f0f (Facade)",permalink:"/docs/pattern/facade"},next:{title:"\u547d\u4ee4\u6a21\u5f0f (Command)",permalink:"/docs/pattern/command"}},c={},s=[{value:"\u6982\u5ff5",id:"\u6982\u5ff5",level:2},{value:"\u793a\u4f8b",id:"\u793a\u4f8b",level:2}],u={toc:s},p="wrapper";function d(e){let{components:t,...r}=e;return(0,a.kt)(p,(0,n.Z)({},u,r,{components:t,mdxType:"MDXLayout"}),(0,a.kt)("h2",{id:"\u6982\u5ff5"},"\u6982\u5ff5"),(0,a.kt)("p",null,"\u8fed\u4ee3\u5668\u6a21\u5f0f\u662f\u4e00\u79cd\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e0d\u66b4\u9732\u96c6\u5408\u5e95\u5c42\u8868\u73b0\u5f62\u5f0f\uff08\u5217\u8868\u3001\u6808\u548c\u6811\u7b49\uff09\u7684\u60c5\u51b5\u4e0b\u904d\u5386\u96c6\u5408\u4e2d\u6240\u6709\u7684\u5143\u7d20\u3002"),(0,a.kt)("ul",null,(0,a.kt)("li",{parentName:"ul"},"\u5355\u4e00\u804c\u8d23\u539f\u5219\u3002\u901a\u8fc7\u5c06\u4f53\u79ef\u5e9e\u5927\u7684\u904d\u5386\u7b97\u6cd5\u4ee3\u7801\u62bd\u53d6\u4e3a\u72ec\u7acb\u7684\u7c7b\uff0c\u53ef\u5bf9\u5ba2\u6237\u7aef\u4ee3\u7801\u548c\u96c6\u5408\u8fdb\u884c\u6574\u7406\u3002"),(0,a.kt)("li",{parentName:"ul"},"\u5f00\u95ed\u539f\u5219\u3002\u53ef\u5b9e\u73b0\u65b0\u578b\u7684\u96c6\u5408\u548c\u8fed\u4ee3\u5668\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u73b0\u6709\u4ee3\u7801\uff0c\u65e0\u9700\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u3002"),(0,a.kt)("li",{parentName:"ul"},"\u53ef\u4ee5\u5e76\u884c\u904d\u5386\u540c\u4e00\u96c6\u5408\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u90fd\u5305\u542b\u5176\u81ea\u8eab\u7684\u904d\u5386\u72b6\u6001\u3002"),(0,a.kt)("li",{parentName:"ul"},"\u53ef\u4ee5\u6682\u505c\u904d\u5386\u5e76\u5728\u9700\u8981\u65f6\u7ee7\u7eed\u3002"),(0,a.kt)("li",{parentName:"ul"},"\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u53ea\u4e0e\u7b80\u5355\u7684\u96c6\u5408\u8fdb\u884c\u4ea4\u4e92\uff0c\u5e94\u7528\u8be5\u6a21\u5f0f\u53ef\u80fd\u4f1a\u77eb\u6789\u8fc7\u6b63\u3002"),(0,a.kt)("li",{parentName:"ul"},"\u5bf9\u4e8e\u67d0\u4e9b\u96c6\u5408\uff0c\u4f7f\u7528\u8fed\u4ee3\u5668\u53ef\u80fd\u6bd4\u76f4\u63a5\u904d\u5386\u7684\u6548\u7387\u4f4e\u3002")),(0,a.kt)("h2",{id:"\u793a\u4f8b"},"\u793a\u4f8b"),(0,a.kt)("p",null,"\u4e3e\u4f8b\u8bf4\u660e\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5bf9\u5fae\u4fe1\u597d\u53cb\u8fdb\u884c\u904d\u5386\u3002\u5b9a\u4e49\u4e86\u4e24\u4e2a\u63a5\u53e3 SocialNetwork\u3001ProfileIterator
1<T",">"," \u548c\u4e24\u4e2a\u7c7b Wechat\u3001WechatIterator\u3002"),(0,a.kt)("p",null,"Wechat \u7c7b\u8981\u5b9e\u73b0 SocialNetwork \u63a5\u53e3\uff0c\u6765\u5b9e\u73b0\u521b\u5efa\u5fae\u4fe1\u8fed\u4ee3\u5668\u7684\u5de5\u5382\u65b9\u6cd5\uff0c\u5e76\u50a8\u5b58\u597d\u53cb\u5217\u8868\u3001\u63d0\u4f9b\u589e\u52a0\u597d\u53cb\u548c\u83b7\u53d6\u597d\u53cb\u7684\u65b9\u6cd5\u3002"),(0,a.kt)("p",null,"WeChatIterator \u7c7b\u8981\u5b9e\u73b0 ProfileIterator<T",">"," \u63a5\u53e3\uff0c\u6765\u5b9e\u73b0\u4e00\u4e9b\u5177\u4f53\u7684\u5fae\u4fe1\u8fed\u4ee3\u5668\u65b9\u6cd5\uff0c\u7528\u6765\u8fed\u4ee3\u597d\u53cb\u5217\u8868\u3002"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-ts"},"// \u58f0\u660e\u7528\u4e8e\u751f\u6210\u8fed\u4ee3\u5668\u7684\u5de5\u5382\u65b9\u6cd5\ninterface SocialNetwork {\n createFriendsIterator(): ProfileIterator<string | null>;\n}\n\n// \u58f0\u660e\u8fed\u4ee3\u5668\u63a5\u53e3\ninterface ProfileIterator<T> {\n // \u8fd4\u56de\u5f53\u524d\u9879\uff0c\u5e76\u5411\u540e\u79fb\u4e00\u4f4d\n next(): T;\n // \u8fd4\u56de\u5f53\u524d\u9879\n current(): T;\n}\n\n// \u5b9e\u73b0\u751f\u6210\u8fed\u4ee3\u5668\u7684\u5de5\u5382\u65b9\u6cd5\uff0c\u548c\u4e00\u4e9b Wechat \u7c7b\u81ea\u6709\u7684\u65b9\u6cd5\nclass Wechat implements SocialNetwork {\n private friendList: string[] = [];\n\n createFriendsIterator(): ProfileIterator<string | null> {\n // \u8fed\u4ee3\u5668\u6307\u5411\u672c\u5b9e\u4f8b\n return new WeChatIterator(this);\n }\n\n public addFriend(friend: string) {\n this.friendList.push(friend);\n }\n\n public getFriendList() {\n return this.friendList;\n }\n}\n\n// \u5b9e\u73b0\u5177\u4f53\u7684\u5fae\u4fe1\u597d\u53cb\u8fed\u4ee3\u5668\u7c7b\nclass WeChatIterator implements ProfileIterator<string | null> {\n private wechat: Wechat; // \u5fae\u4fe1\u5b9e\u4f8b\n private position = 0; // \u8bb0\u5f55\u5f53\u524d\u8fed\u4ee3\u5230\u7684\u4f4d\u7f6e\n\n constructor(weChat: Wechat) {\n this.wechat = weChat;\n }\n\n // \u8fd4\u56de\u5f53\u524d\u9879\uff0c\u5e76\u540e\u79fb\u4e00\u4f4d\uff1b\u82e5\u5df2\u7ecf\u904d\u5386\u5b8c\uff0c\u6ca1\u6709\u6570\u636e\u4e86\uff0c\u8fd4\u56de null\n next(): string | null {\n return this.wechat.getFriendList()[this.position++] || null;\n }\n\n current(): string {\n return this.wechat.getFriendList()[this.position];\n }\n}\n\nconst wechat = new Wechat();\nwechat.addFriend('Lee');\nwechat.addFriend('Tom');\nwechat.addFriend('Bella');\n\nconst iterator = wechat.createFriendsIterator();\n\nlet curr;\nwhile ((curr = iterator.next())) {\n console.log(curr);\n}\n")),(0,a.kt)("p",null,"\u8f93\u51fa\uff1a"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre"},"Lee\nTom\nBella\n")))}d.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.