1ã€FACTORY—追MMž®‘ä¸äº†è¯·åƒé¥äº†ï¼Œéº¦å½“劳的鸡翅和肯德基的鸡¾˜…都是MM爱åƒçš„东西,虽然å£å‘³æœ‰æ‰€ä¸åŒåQŒä½†ä¸ç®¡ä½ 带MM去麦当劳或肯德基åQŒåª½Ž¡å‘æœåŠ¡å‘˜è¯´â€œæ¥å››ä¸ªé¸¡ç¿…â€å°±è¡Œäº†ã€‚麦当劳和肯德基ž®±æ˜¯ç”Ÿäñ”鸡翅的Factory
工厂模å¼åQšå®¢æˆïL±»å’Œå·¥åŽ‚ç±»åˆ†å¼€ã€‚æ¶ˆè´¹è€…ä“Qä½•æ—¶å€™éœ€è¦æŸ¿Uäñ”å“,åªéœ€å‘工厂请求å³å¯ã€‚æ¶ˆè´¹è€…æ— ™åÖM¿®æ”¹å°±å¯ä»¥æŽ¥çº³æ–îCñ”å“。缺ç‚ÒŽ˜¯å½“äñ”å“ä¿®æ”ÒŽ—¶åQŒå·¥åŽ‚ç±»ä¹Ÿè¦åšç›¸åº”的修改。如åQšå¦‚何创建åŠå¦‚何å‘客æˆïL«¯æä¾›ã€?
2ã€BUILDER—MM最爱å¬çš„就是“我çˆ×ƒ½ â€è¿™å¥è¯äº†ï¼Œè§åˆ°ä¸åŒåœ°æ–¹çš„MM,è¦èƒ½å¤Ÿç”¨å¥¹ä»¬çš„æ–¹a€è·Ÿå¥¹è¯´è¿™å¥è¯å“¦ï¼Œæˆ‘有一个多¿Uè¯a€¾˜»è¯‘æœºï¼Œä¸Šé¢æ¯ç§è¯è¨€éƒ½æœ‰ä¸€ä¸ªæŒ‰é”®ï¼Œè§åˆ°MM我åªè¦æŒ‰å¯¹åº”的键åQŒå®ƒž®Þpƒ½å¤Ÿç”¨ç›¸åº”çš„è¯a€è¯´å‡ºâ€œæˆ‘çˆ×ƒ½ â€è¿™å¥è¯äº†ï¼Œå›½å¤–çš„MM也å¯ä»¥è½»æ¾æžæŽ‚,˜q™å°±æ˜¯æˆ‘的“我çˆ×ƒ½ â€builder。(˜q™ä¸€å®šæ¯”¾ŸŽå†›åœ¨ä¼Šæ‹‰å…‹ç”¨çš„¾˜»è¯‘机好å–)
å»ºé€ æ¨¡å¼ï¼šž®†äñ”å“的内部表象和äñ”å“的生战q‡ç¨‹åˆ†å‰²å¼€æ¥ï¼Œä»Žè€Œä‹É一个å¾é€ 过½E‹ç”Ÿæˆå…·æœ‰ä¸åŒçš„内部表象的äñ”å“对象。å¾é€ 模å¼ä‹Éå¾—äñ”å“内部表象å¯ä»¥ç‹¬ç«‹çš„å˜åŒ–åQŒå®¢æˆ·ä¸å¿…知é“äñ”å“内部组æˆçš„¾l†èŠ‚ã€‚å¾é€ 模å¼å¯ä»¥å¼ºåˆ¶å®žè¡Œä¸€¿U分æ¥éª¤˜q›è¡Œçš„å¾é€ 过½E‹ã€?
3ã€FACTORY METHOD—请MMåŽ»éº¦å½“åŠ³åƒæ±‰å ¡ï¼Œä¸åŒçš„MM有ä¸åŒçš„å£å‘³åQŒè¦æ¯ä¸ªéƒ½è®°ä½æ˜¯ä¸€ä»¶çƒ¦äººçš„事情åQŒæˆ‘一般采用Factory Method模å¼åQŒå¸¦ç€MM到æœåŠ¡å‘˜é‚£å„¿åQŒè¯´â€œè¦ä¸€ä¸ªæ±‰å ¡â€ï¼Œå…·ä½“è¦ä»€ä¹ˆæ ·çš„æ±‰å ¡å‘¢åQŒè®©MM直接跟æœåŠ¡å‘˜è¯´å°±è¡Œäº†ã€?
工厂æ–ÒŽ³•模å¼åQšæ ¸å¿ƒå·¥åŽ‚ç±»ä¸å†è´Ÿè´£æ‰€æœ‰äñ”å“的创å¾åQŒè€Œæ˜¯ž®†å…·ä½“创建的工作交给åç±»åŽÕdšåQŒæˆä¸ÞZ¸€ä¸ªæŠ½è±¡å·¥åŽ‚è§’è‰ÔŒ¼Œä»…负责给出具体工厂类必须实现的接å£ï¼Œè€Œä¸æŽ¥è§¦å“ªä¸€ä¸ªäñ”å“类应当被实例化˜q™ç§¾l†èŠ‚ã€?
4ã€PROTOTYPE—跟MM用QQèŠå¤©åQŒä¸€å®šè¦è¯´äº›æ·±æƒ…çš„è¯è¯äº†åQŒæˆ‘æœé›†äº†å¥½å¤šè‚‰éºÈš„æƒ…è¯åQŒéœ€è¦æ—¶åªè¦copyå‡ºæ¥æ”‘Öˆ°QQ里颞®Þp¡Œäº†ï¼Œ˜q™å°±æ˜¯æˆ‘的情è¯prototype了。(100å—é’±ä¸€ä»½ï¼Œä½ è¦ä¸è¦åQ?
原始模型模å¼åQšé€šè¿‡¾l™å‡ºä¸€ä¸ªåŽŸåž‹å¯¹è±¡æ¥æŒ‡æ˜Žæ‰€è¦åˆ›å»ºçš„对象的类型,然åŽç”¨å¤åˆ¶è¿™ä¸ªåŽŸåž‹å¯¹è±¡çš„æ–ÒŽ³•创å¾å‡ºæ›´å¤šåŒ¾cÕdž‹çš„对象。原始模型模å¼å…许动æ€çš„å¢žåŠ æˆ–å‡ž®‘äñ”å“ç±»åQŒäñ”å“ç±»ä¸éœ€è¦éžå¾—有ä»ÖM½•事先¼‹®å®šçš„牾U§ç»“构,原始模型模å¼é€‚用于ä“Q何的½{‰çñ”¾l“构。缺ç‚ÒŽ˜¯æ¯ä¸€ä¸ªç±»éƒ½å¿…™å»é…备一个克隆方法ã€?
5ã€SINGLETON—俺æœ?个漂亮的è€å©†åQŒå¥¹ä»¬çš„è€å…¬éƒ½æ˜¯æˆ‘,我就是我们家里的è€å…¬SigletonåQŒå¥¹ä»¬åªè¦è¯´é““è€å…¬â€ï¼Œéƒ½æ˜¯æŒ‡çš„åŒä¸€ä¸ªähåQŒé‚£ž®±æ˜¯æˆ?刚æ‰åšäº†ä¸ªæ¢¦å•¦ï¼Œå“ªæœ‰˜q™ä¹ˆå¥½çš„äº?
å•例模å¼åQšå•例模å¼ç¡®ä¿æŸä¸€ä¸ªç±»åªæœ‰ä¸€ä¸ªå®žä¾‹ï¼Œè€Œä¸”è‡ªè¡Œå®žä¾‹åŒ–åÆˆå‘æ•´ä¸ªç³»¾lŸæä¾›è¿™ä¸ªå®žä¾‹å•例模å¼ã€‚å•例模å¼åªåº”在有真æ£çš„“å•一实例â€çš„需求时æ‰å¯ä½¿ç”¨ã€?
¾l“构型模å¼?
6ã€ADAPTER—在朋å‹èšä¼šä¸Šç¢°åˆîCº†ä¸€ä¸ªç¾Žå¥³SarahåQŒä»Žé¦™æ¸¯æ¥çš„åQŒå¯æˆ‘ä¸ä¼šè¯´¾_¤è¯åQŒå¥¹ä¸ä¼šè¯´æ™®é€šè¯åQŒåªå¥½æ±‚助于我的朋å‹kent了,他作为我和Sarah之间的AdapteråQŒè®©æˆ‘å’ŒSarahå¯ä»¥ç›æ€º’交谈äº?也ä¸çŸ¥é“他会ä¸ä¼šè€æˆ‘)
适é…器(å˜åŽ‹å™¨ï¼‰æ¨¡å¼åQšæŠŠä¸€ä¸ªç±»çš„æŽ¥å£å˜æ¢æˆå®¢æˆ·ç«¯æ‰€æœŸå¾…çš„å¦ä¸€¿U接å£ï¼Œä»Žè€Œä‹ÉåŽŸæœ¬å› æŽ¥å£åŽŸå› ä¸åŒšw…è€Œæ— æ³•ä¸€èµ·å·¥ä½œçš„ä¸¤ä¸ª¾c»èƒ½å¤Ÿä¸€èµ·å·¥ä½œã€‚适酾cÕd¯ä»¥æ ¹æ®å‚æ•°è¿”˜q˜ä¸€ä¸ªåˆé€‚的实例¾l™å®¢æˆïL«¯ã€?
7ã€BRIDGE—早上碰到MMåQŒè¦è¯´æ—©ä¸Šå¥½åQŒæ™šä¸Šç¢°åˆ°MMåQŒè¦è¯´æ™šä¸Šå¥½åQ›ç¢°åˆ°MM½I¿äº†ä»¶æ–°è¡£æœåQŒè¦è¯´ä½ 的衣æœå¥½æ¼‚亮哦,¼„°åˆ°MMæ–°åšçš„å‘型,è¦è¯´ä½ 的头å‘好漂亮哦。ä¸è¦é—®æˆ‘“早上碰到MMæ–°åšäº†ä¸ªå‘型怎么说â€è¿™¿U问题,自己用BRIDGE¾l„åˆä¸€ä¸‹ä¸ž®Þp¡Œäº?
æ¡¥æ¢æ¨¡å¼åQšå°†æŠ½è±¡åŒ–与实现化脱耦,使得二者å¯ä»¥ç‹¬ç«‹çš„å˜åŒ–åQŒä¹Ÿž®±æ˜¯è¯´å°†ä»–们之间的强兌™”å˜æˆå¼±å…³è”,也就是指在一个èÊYä»¶ç³»¾lŸçš„æŠ½è±¡åŒ–和实现化之间ä‹É用组å?èšåˆå…³ç³»è€Œä¸æ˜¯ç‘ô承关¾p»ï¼Œä»Žè€Œä‹É两者å¯ä»¥ç‹¬ç«‹çš„å˜åŒ–ã€?
8ã€COMPOSITE—Mary今天˜q‡ç”Ÿæ—¥ã€‚“我˜q‡ç”Ÿæ—¥ï¼Œä½ è¦é€æˆ‘一件礼物。â€â€œå—¯åQŒå¥½å§ï¼ŒåŽÕd•†åº—ï¼Œä½ è‡ªå·±æŒ‘ã€‚â€â€œè¿™ä»¶Tæ¤æŒºæ¼‚亮åQŒä¹°åQŒè¿™æ¡è£™å好看,乎ͼŒ˜q™ä¸ªåŒ…也ä¸é”™åQŒä¹°ã€‚â€â€œå–‚åQŒä¹°äº†ä¸‰ä»¶äº†å‘€åQŒæˆ‘åªç”应é€ä¸€ä»¶ç¤¼ç‰©çš„哦。â€â€œä»€ä¹ˆå‘€åQŒTæ¤åŠ è£™ååŠ åŒ…åŒ…ï¼Œæ£å¥½é…æˆä¸€å¥—å‘€åQŒå°å§ï¼ŒéºÈƒ¦ä½ 包èµäh¥ã€‚â€â€œâ€¦â€¦â€ï¼ŒMM都会用Composite模å¼äº†ï¼Œä½ 会了没有?
åˆæˆæ¨¡å¼åQšåˆæˆæ¨¡å¼å°†å¯¹è±¡¾l„ç»‡åˆ°æ ‘¾l“æž„ä¸ï¼Œå¯ä»¥ç”¨æ¥æè¿°æ•´ä½“ä¸Žéƒ¨åˆ†çš„å…³ç³»ã€‚åˆæˆæ¨¡å¼å°±æ˜¯ä¸€ä¸ªå¤„ç†å¯¹è±¡çš„æ ‘结构的模å¼ã€‚åˆæˆæ¨¡å¼æŠŠéƒ¨åˆ†ä¸Žæ•´ä½“çš„å…³ç³»ç”¨æ ‘¾l“构表示出æ¥ã€‚åˆæˆæ¨¡å¼ä‹É得客æˆïL«¯æŠŠä¸€ä¸ªä¸ªå•独的æˆåˆ†å¯¹è±¡å’Œç”׃»–们å¤åˆè€Œæˆçš„åˆæˆå¯¹è±¡åŒ½{‰çœ‹å¾…ã€?
9ã€DECORATOR—Mary˜q‡å®Œè½®åˆ°Sarly˜q‡ç”Ÿæ—¥ï¼Œ˜q˜æ˜¯ä¸è¦å«å¥¹è‡ªå·±æŒ‘了åQŒä¸ç„¶è¿™ä¸ªæœˆä¼™é£Ÿè´¹è‚¯å®šçŽ©å®Œï¼Œæ‹¿å‡ºæˆ‘åŽ»òq´åœ¨åŽå±±™å¶ä¸Šç…§çš„照片åQŒåœ¨èƒŒé¢å†™ä¸Šâ€œæœ€å¥½çš„的礼物,ž®±æ˜¯çˆ×ƒ½ çš„Fitaâ€ï¼Œå†åˆ°è¡—上½C¼å“åº—ä¹°äº†ä¸ªåƒæ¡†åQˆå–½C¼å“çš„MM也很漂亮哦)åQŒå†æ‰ùNš”壿ž¾ŸŽæœ¯è®¾è®¡çš„Mike设计了一个漂亮的盒å装è“væ¥â€¦â€¦ï¼Œæˆ‘们都是DecoratoråQŒæœ€¾lˆéƒ½åœ¨ä¿®é¥°æˆ‘˜q™ä¸ªäººå‘€åQŒæ€Žä¹ˆæ øP¼Œçœ‹æ‡‚了å—åQ?
装饰模å¼åQšè£…饰模å¼ä»¥å¯¹å®¢æˆïL«¯é€æ˜Žçš„æ–¹å¼æ‰©å±•对象的功能åQŒæ˜¯¾l§æ‰¿å…³ç³»çš„一个替代方案,æä¾›æ¯”ç‘ô承更多的ç‰|´»æ€§ã€‚动æ€ç»™ä¸€ä¸ªå¯¹è±¡å¢žåŠ åŠŸèƒ½ï¼Œ˜q™äº›åŠŸèƒ½å¯ä»¥å†åЍæ€çš„æ’¤æ¶ˆã€‚å¢žåŠ ç”±ä¸€äº›åŸºæœ¬åŠŸèƒ½çš„æŽ’åˆ—¾l„åˆè€Œäñ”生的éžå¸¸å¤§é‡çš„功能ã€?/p>
10ã€FACADE—我有一个专业的Nikon相机åQŒæˆ‘ž®±å–œ‹Æ¢è‡ªå·±æ‰‹åŠ¨è°ƒå…‰åœˆã€å¿«é—¨ï¼Œ˜q™æ ·ç…§å‡ºæ¥çš„照片æ‰ä¸“业,但MMå¯ä¸æ‡‚这些,教了åŠå¤©ä¹Ÿä¸ä¼šã€‚幸好相机有Facade设计模å¼åQŒæŠŠç›¸æœºè°ƒæ•´åˆ°è‡ªåŠ¨æ¡£åQŒåªè¦å¯¹å‡†ç›®æ ‡æŒ‰å¿«é—¨ž®Þp¡Œäº†ï¼Œä¸€åˆ‡ç”±ç›¸æœºè‡ªåŠ¨è°ƒæ•´åQŒè¿™æ ·MM也å¯ä»¥ç”¨˜q™ä¸ªç›¸æœº¾l™æˆ‘æ‹å¼ 照片了ã€?
闍颿¨¡å¼åQšå¤–部与一个å¾pÈ»Ÿçš„通信必须通过一个统一的门é¢å¯¹è±¡è¿›è¡Œã€‚闍颿¨¡å¼æä¾›ä¸€ä¸ªé«˜å±‚次的接å£ï¼Œä½¿å¾—åç³»¾lŸæ›´æ˜“于使用。æ¯ä¸€ä¸ªå¾pÈ»Ÿåªæœ‰ä¸€ä¸ªé—¨é¢ç±»åQŒè€Œä¸”æ¤é—¨é¢ç±»åªæœ‰ä¸€ä¸ªå®žä¾‹ï¼Œä¹Ÿå°±æ˜¯è¯´å®ƒæ˜¯ä¸€ä¸ªå•例模å¼ã€‚但整个¾pÈ»Ÿå¯ä»¥æœ‰å¤šä¸ªé—¨é¢ç±»ã€?
11ã€FLYWEIGHT—æ¯å¤©è·ŸMMå‘çŸä¿¡ï¼Œæ‰‹æŒ‡éƒ½ç¯æÖMº†åQŒæœ€˜q‘买了个新手机,å¯ä»¥æŠŠä¸€äº›å¸¸ç”¨çš„å¥åå˜åœ¨æ‰‹æœºé‡Œï¼Œè¦ç”¨çš„æ—¶å€™ï¼Œç›´æŽ¥æ‹¿å‡ºæ¥ï¼Œåœ¨å‰é¢åŠ ä¸ŠMMçš„åå—å°±å¯ä»¥å‘é€äº†åQŒå†ä¸ç”¨ä¸€ä¸ªå—ä¸€ä¸ªå—æ•²äº†ã€‚共享的å¥åž®±æ˜¯FlyweightåQŒMMçš„åå—就是æå–出æ¥çš„外部特å¾åQŒæ ¹æ®ä¸Šä¸‹æ–‡æƒ…况使用ã€?
享元模å¼åQšFLYWEIGHT在拳å‡ÀL¯”èµ›ä¸æŒ‡æœ€è½»é‡¾U§ã€‚äín元模å¼ä»¥å…׃ín的方å¼é«˜æ•ˆçš„æ”¯æŒå¤§é‡çš„细¾_’度对象。äín元模å¼èƒ½åšåˆ°å…׃ín的关键是区分内蕴状æ€å’Œå¤–蕴状æ€ã€‚内蕴状æ€å˜å‚¨åœ¨äº«å…ƒå†…部åQŒä¸ä¼šéšçŽ¯å¢ƒçš„æ”¹å˜è€Œæœ‰æ‰€ä¸åŒã€‚å¤–è•´çŠ¶æ€æ˜¯éšçŽ¯å¢ƒçš„æ”¹å˜è€Œæ”¹å˜çš„。外蕴状æ€ä¸èƒ½åª„å“内蕴状æ€ï¼Œå®ƒä»¬æ˜¯ç›¸äº’独立的。将å¯ä»¥å…׃ín的状æ€å’Œä¸å¯ä»¥å…±äº«çš„状æ€ä»Žå¸¸è§„¾cÖM¸åŒºåˆ†å¼€æ¥ï¼Œž®†ä¸å¯ä»¥å…׃ín的状æ€ä»Ž¾c»é‡Œå‰”除出去。客æˆïL«¯ä¸å¯ä»¥ç›´æŽ¥åˆ›å»ø™¢«å…׃ín的对象,而应当ä‹Éç”¨ä¸€ä¸ªå·¥åŽ‚å¯¹è±¡è´Ÿè´£åˆ›å»ø™¢«å…׃ín的对象。äín元模å¼å¤§òq…度的é™ä½Žå†…å˜ä¸å¯¹è±¡çš„æ•°é‡ã€?
12ã€PROXY—跟MM在网上èŠå¤©ï¼Œä¸€å¼€å¤´æ€ÀL˜¯â€œhi,ä½ å¥½â€?â€œä½ ä»Žå“ªå„¿æ¥å‘€åQŸâ€â€œä½ 多大了?â€â€œèín高多ž®‘å‘€åQŸâ€è¿™äº›è¯åQŒçœŸçƒ¦ähåQŒå†™ä¸ªç¨‹åºåšä¸ºæˆ‘çš„Proxyå§ï¼Œå‡¡æ˜¯æŽ¥æ”¶åˆ°è¿™äº›è¯éƒ½è®¾¾|®å¥½äº†è‡ªåŠ¨çš„å›žç”åQŒæŽ¥æ”¶åˆ°å…¶ä»–çš„è¯æ—¶å†é€šçŸ¥æˆ‘回½{”ï¼Œæ€Žä¹ˆæ øP¼Œé…·å§ã€?
ä»£ç†æ¨¡å¼åQšä»£ç†æ¨¡å¼ç»™æŸä¸€ä¸ªå¯¹è±¡æä¾›ä¸€ä¸ªä»£ç†å¯¹è±¡ï¼Œòq¶ç”±ä»£ç†å¯¹è±¡æŽ§åˆ¶å¯ÒŽºå¯¹è±¡çš„å¼•ç”¨ã€‚ä»£ç†å°±æ˜¯ä¸€ä¸ªäh或一个机构代表å¦ä¸€ä¸ªäh或者一个机构采å–行动。æŸäº›æƒ…况下åQŒå®¢æˆ·ä¸æƒÏxˆ–者ä¸èƒ½å¤Ÿç›´æŽ¥å¼•用一个对象,代ç†å¯¹è±¡å¯ä»¥åœ¨å®¢æˆ·å’Œç›®æ ‡å¯¹è±¡ç›´æŽ¥èµ·åˆ°ä¸ä»‹çš„作用。客æˆïL«¯åˆ†èöLä¸å‡ºä»£ç†ä¸»é¢˜å¯¹è±¡ä¸ŽçœŸå®žä¸»é¢˜å¯¹è±¡ã€‚ä»£ç†æ¨¡å¼å¯ä»¥åƈä¸çŸ¥é“真æ£çš„被代ç†å¯¹è±¡ï¼Œè€Œä»…ä»…æŒæœ‰ä¸€ä¸ªè¢«ä»£ç†å¯¹è±¡çš„æŽ¥å£ï¼Œ˜q™æ—¶å€™ä»£ç†å¯¹è±¡ä¸èƒ½å¤Ÿåˆ›å¾è¢«ä»£ç†å¯¹è±¡ï¼Œè¢«ä»£ç†å¯¹è±¡å¿…™åÀLœ‰¾pÈ»Ÿçš„å…¶ä»–è§’è‰²ä»£ä¸ºåˆ›å»ºåÆˆä¼ å…¥ã€?
è¡ŒäØ“æ¨¡å¼
13ã€CHAIN OF RESPONSIBLEITY—晚上去上英è¯è¯¾åQŒäؓ了好开溜ååˆîCº†æœ€åŽä¸€æŽ’,哇,å‰é¢åäº†å¥½å‡ ä¸ªæ¼‚äº®çš„MM哎,扑ּ ¾U¸æ¡åQŒå†™ä¸Šâ€œHi,å¯ä»¥åšæˆ‘çš„å¥³æœ‹å‹å—ï¼Ÿå¦‚æžœä¸æ„¿æ„请å‘å‰ä¼ â€ï¼Œ¾U¸æ¡ž®×ƒ¸€ä¸ªæŽ¥ä¸€ä¸ªçš„ä¼ ä¸ŠåŽÖMº†åQŒç³Ÿ¾p•ï¼Œä¼ åˆ°½W¬ä¸€æŽ’çš„MM把纸æ¡ä¼ ¾l™è€å¸ˆäº†ï¼Œå¬è¯´æ˜¯ä¸ªè€å¤„奛_‘€åQŒå¿«è·?
è´£ä“Q链模å¼ï¼šåœ¨è´£ä»»é“¾æ¨¡å¼ä¸ï¼Œå¾ˆå¤šå¯¹è±¡ç”±æ¯ä¸€ä¸ªå¯¹è±¡å¯¹å…¶ä¸‹å®¶çš„引用而接
èµäh¥å½¢æˆä¸€æ¡é“¾ã€‚请求在˜q™ä¸ªé“¾ä¸Šä¼ 递,直到链上的æŸä¸€ä¸ªå¯¹è±¡å†³å®šå¤„ç†æ¤è¯äh±‚ã€‚å®¢æˆ·åÆˆä¸çŸ¥é“链上的哪一个对象最¾lˆå¤„ç†è¿™ä¸ªè¯·æ±‚,¾pÈ»Ÿå¯ä»¥åœ¨ä¸å½±å“客户端的情况下动æ€çš„釿–°¾l„织铑֒Œåˆ†é…è´£ä“Q。处ç†è€…有两个选择åQšæ‰¿æ‹…è´£ä»ÀLˆ–者把责ä“Q推给下家。一个请求å¯ä»¥æœ€¾lˆä¸è¢«ä“Q何接收端对象所接å—ã€?
14ã€COMMAND—俺有一个MM安™‡Œ½Ž¡å¾—特别严,没法è§é¢åQŒåªå¥½å€ŸåŠ©äºŽå¥¹å¼Ÿå¼Ÿåœ¨æˆ‘ä»¬ä¿©ä¹‹é—´ä¼ é€ä¿¡æ¯ï¼Œå¥¹å¯¹æˆ‘有什么指½Cºï¼Œž®±å†™ä¸€å¼ 纸æ¡è®©å¥¹å¼Ÿå¼Ÿå¸¦¾l™æˆ‘。这ä¸ï¼Œå¥¹å¼Ÿå¼Ÿåˆä¼ é€è¿‡æ¥ä¸€ä¸ªCOMMANDåQŒäؓ了感谢他åQŒæˆ‘请他åƒäº†¼„—æ‚é…±é¢åQŒå“ªçŸ¥é“他说åQšâ€œæˆ‘åŒæ—¶¾l™æˆ‘å§å§ä¸‰ä¸ªç”ähœ‹å‹é€COMMANDåQŒå°±æ•îC½ 最ž®æ°”åQŒæ‰è¯ähˆ‘åƒé¢ã€‚â€ï¼Œ:-(
命ä×o模å¼åQšå‘½ä»¤æ¨¡å¼æŠŠä¸€ä¸ªè¯·æ±‚或者æ“作å°è£…到一个对象ä¸ã€‚å‘½ä»¤æ¨¡å¼æŠŠå‘出命ä×o的责ä»Õd’Œæ‰§è¡Œå‘½ä×o的责ä»Õdˆ†å‰²å¼€åQŒå§”‹z„¡»™ä¸åŒçš„对象。命令模å¼å…许请求的一方和å‘é€çš„一方独立开æ¥ï¼Œä½¿å¾—è¯äh±‚的一方ä¸å¿…çŸ¥é“æŽ¥æ”¶è¯·æ±‚çš„ä¸€æ–¹çš„æŽ¥å£åQŒæ›´ä¸å¿…知é“è¯äh±‚是怎么被接æ”Óž¼Œä»¥åŠæ“ä½œæ˜¯å¦æ‰§è¡ŒåQŒä½•æ—¶è¢«æ‰§è¡Œä»¥åŠæ˜¯æ€Žä¹ˆè¢«æ‰§è¡Œçš„。系¾lŸæ”¯æŒå‘½ä»¤çš„æ’¤æ¶ˆã€?
15ã€INTERPRETER—俺有一个《æˆöMM真ç»ã€‹ï¼Œä¸Šé¢æœ‰å„¿UæˆöMM的攻略,比如说去åƒè¥¿˜¡çš„æ¥éª¤ã€åŽ»çœ‹ç”µå½Þqš„æ–ÒŽ³•½{‰ç‰åQŒè·ŸMM¾U¦ä¼šæ—Óž¼Œåªè¦åšä¸€ä¸ªInterpreteråQŒç…§ç€ä¸Šé¢çš„脚本执行就å¯ä»¥äº†ã€?
解释器模å¼ï¼š¾l™å®šä¸€ä¸ªè¯a€åŽï¼Œè§£é‡Šå™¨æ¨¡å¼å¯ä»¥å®šä¹‰å‡ºå…¶æ–‡æ³•的一¿U表½Cºï¼Œòq¶åŒæ—¶æä¾›ä¸€ä¸ªè§£é‡Šå™¨ã€‚客æˆïL«¯å¯ä»¥ä½¿ç”¨˜q™ä¸ªè§£é‡Šå™¨æ¥è§£é‡Š˜q™ä¸ªè¯è¨€ä¸çš„å¥å。解释器模弞®†æ˜q°æ€Žæ ·åœ¨æœ‰äº†ä¸€ä¸ªç®€å•的文法åŽï¼Œä½¿ç”¨æ¨¡å¼è®¾è®¡è§£é‡Š˜q™äº›è¯å¥ã€‚在解释器模å¼é‡Œé¢æåˆ°çš„è¯è¨€æ˜¯æŒ‡ä»ÖM½•解释器对象能够解释的ä»ÖM½•¾l„åˆã€‚在解释器模å¼ä¸éœ€è¦å®šä¹‰ä¸€ä¸ªä»£è¡¨æ–‡æ³•的命ä×o¾cÈš„½{‰çñ”¾l“æž„åQŒä¹Ÿž®±æ˜¯ä¸€¾pÕdˆ—的组åˆè§„则。æ¯ä¸€ä¸ªå‘½ä»¤å¯¹è±¡éƒ½æœ‰ä¸€ä¸ªè§£é‡Šæ–¹æ³•,代表对命令对象的解释。命令对象的½{‰çñ”¾l“æž„ä¸çš„对象的ä“Q何排列组åˆéƒ½æ˜¯ä¸€ä¸ªè¯a€ã€?
Â
16ã€ITERATOR—我çˆ×ƒ¸Šäº†MaryåQŒä¸™å¾ä¸€åˆ‡çš„å‘她求婚ã€?
MaryåQšâ€œæƒ³è¦æˆ‘è·Ÿä½ ¾l“婚åQŒå¾—½{”应我的æ¡äšgâ€?
我:“什么æ¡ä»¶æˆ‘都ç”åº”ï¼Œä½ è¯´å§â€?
MaryåQšâ€œæˆ‘看上了那个一克拉的钻石â€?
我:“我乎ͼŒæˆ‘ä¹°åQŒè¿˜æœ‰å—åQŸâ€?
MaryåQšâ€œæˆ‘çœ‹ä¸Šäº†æ¹–è¾¹çš„é‚£æ ‹åˆ«å¢…â€?
我:“我乎ͼŒæˆ‘ä¹°åQŒè¿˜æœ‰å—åQŸâ€?
MaryåQšâ€œä½ çš„å°å¼Ÿå¼Ÿå¿…é¡»è¦æœ‰50cm镎ì€?
我脑袋嗡的一壎ͼŒå在椅å上,一咬牙åQšâ€œæˆ‘剪,我剪åQŒè¿˜æœ‰å—åQŸâ€?
…�
˜q代忍¡å¼ï¼š˜q代忍¡å¼å¯ä»¥é¡ºåºè®¿é—®ä¸€ä¸ªèšé›†ä¸çš„å…ƒç´ è€Œä¸å¿…暴露èšé›†çš„内部表象。多个对象èšåœ¨ä¸€èµ·åÅžæˆçš„æ€ÖM½“¿UîC¹‹ä¸ø™šé›†ï¼Œèšé›†å¯¹è±¡æ˜¯èƒ½å¤ŸåŒ…容一¾l„对象的容器对象。è„P代忍¡å¼ž®†è„P代逻辑ž®è£…åˆîC¸€ä¸ªç‹¬ç«‹çš„å对象ä¸åQŒä»Žè€Œä¸Žèšé›†æœ¬èín隔开。è„P代忍¡å¼½Ž€åŒ–了èšé›†çš„界é¢ã€‚æ¯ä¸€ä¸ªèšé›†å¯¹è±¡éƒ½å¯ä»¥æœ‰ä¸€ä¸ªæˆ–一个以上的˜q代å对象,æ¯ä¸€ä¸ªè„P代åçš„è„P代状æ€å¯ä»¥æ˜¯å½¼æ¤ç‹¬ç«‹çš„。è„P代算法å¯ä»¥ç‹¬ç«‹äºŽèšé›†è§’色å˜åŒ–ã€?
17ã€MEDIATOR—四个MM打麻ž®†ï¼Œç›æ€º’之间è°åº”该给è°å¤šž®‘é’±½Ž—䏿¸…楚了,òq怺当时我在æ—è¾¹åQŒæŒ‰ç…§å„自的½{¹ç 数算钱,赚了é’Þqš„从我˜q™é‡Œæ‹¿ï¼Œèµ”了é’Þqš„也付¾l™æˆ‘åQŒä¸€åˆ‡å°±OK啦,俺得åˆîCº†å››ä¸ªMM的电è¯ã€?
è°ƒåœè€…模å¼ï¼šè°ƒåœè€…模å¼åŒ…装了一¾pÕdˆ—å¯¹è±¡ç›æ€º’作用的方å¼ï¼Œä½¿å¾—˜q™äº›å¯¹è±¡ä¸å¿…ç›æ€º’明显作用。从而ä‹É他们å¯ä»¥æ¾æ•£å¶åˆã€‚当æŸäº›å¯¹è±¡ä¹‹é—´çš„作用å‘ç”Ÿæ”¹å˜æ—¶åQŒä¸ä¼šç«‹å›_ª„å“其他的一些对象之间的作用。ä¿è¯è¿™äº›ä½œç”¨å¯ä»¥å½¼æ¤ç‹¬ç«‹çš„å˜åŒ–。调åœè€…模å¼å°†å¤šå¯¹å¤šçš„ç›æ€º’作用转化ä¸ÞZ¸€å¯¹å¤šçš„相互作用。调åœè€…模å¼å°†å¯¹è±¡çš„行为和å作抽象化,把对象在ž®å°ºåº¦çš„è¡ŒäØ“ä¸Šä¸Žå…¶ä»–å¯¹è±¡çš„ç›¸äº’ä½œç”¨åˆ†å¼€å¤„ç†ã€?
18ã€MEMENTOâ€”åŒæ—¶è·Ÿå‡ 个MMèŠå¤©æ—Óž¼Œä¸€å®šè¦è®°æ¸…楚刚æ‰è·ŸMM说了些什么è¯åQŒä¸ç„¶MMå‘现了会ä¸é«˜å…´çš„哦,òq怺我有个备忘录åQŒåˆšæ‰ä¸Žå“ªä¸ªMMè¯´äº†ä»€ä¹ˆè¯æˆ‘都拯‚´ä¸€ä»½æ”¾åˆ°å¤‡å¿˜å½•里é¢ä¿å˜åQŒè¿™æ ·å¯ä»¥éšæ—¶å¯Ÿçœ‹ä»¥å‰çš„记录啦ã€?
备忘录模å¼ï¼šå¤‡å¿˜å½•对象是一个用æ¥å˜å‚¨å¦å¤–一个对象内部状æ€çš„快照的对象。备忘录模å¼çš„ç”¨æ„æ˜¯åœ¨ä¸ç ´åž®è£…çš„æ¡ä»¶ä¸‹åQŒå°†ä¸€ä¸ªå¯¹è±¡çš„çŠ¶æ€æ‰ä½ï¼Œòq¶å¤–部化åQŒå˜å‚¨è“væ¥ï¼Œä»Žè€Œå¯ä»¥åœ¨ž®†æ¥åˆé€‚的时候把˜q™ä¸ªå¯¹è±¡˜q˜åŽŸåˆ°å˜å‚¨è“væ¥çš„状æ€ã€?
19ã€OBSERVER—想知é“å’׃»¬å…¬å¸æœ€æ–°MM情报å—ï¼ŸåŠ å…¥å…¬å¸çš„MM情报邮äšg¾l„就行了åQŒtomè´Ÿè´£æœé›†æƒ…报åQŒä»–å‘现的新情报ä¸ç”¨ä¸€ä¸ªä¸€ä¸ªé€šçŸ¥æˆ‘们åQŒç›´æŽ¥å‘布给邮äšg¾l„ï¼Œæˆ‘ä»¬ä½œäØ“è®¢é˜…è€…ï¼ˆè§‚å¯Ÿè€…ï¼‰ž®±å¯ä»¥åŠæ—¶æ”¶åˆ°æƒ…报啦
观察者模å¼ï¼šè§‚察者模å¼å®šä¹‰äº†ä¸€¿U一队多的ä¾èµ–å…³¾p»ï¼Œè®©å¤šä¸ªè§‚å¯Ÿè€…å¯¹è±¡åŒæ—¶ç›‘嬿Ÿä¸€ä¸ªä¸»é¢˜å¯¹è±¡ã€‚这个主题对象在状æ€ä¸Šå‘生å˜åŒ–æ—Óž¼Œä¼šé€šçŸ¥æ‰€æœ‰è§‚察者对象,使他们能够自动更新自己ã€?
20ã€STATE—跟MM交往æ—Óž¼Œä¸€å®šè¦æ³¨æ„她的状æ€å“¦åQŒåœ¨ä¸åŒçš„çŠ¶æ€æ—¶å¥¹çš„è¡ŒäØ“ä¼šæœ‰ä¸åŒåQŒæ¯”å¦‚ä½ ¾U¦å¥¹ä»Šå¤©æ™šä¸ŠåŽÈœ‹ç”µåª„åQŒå¯¹ä½ 没兴趣的MMž®×ƒ¼šè¯´â€œæœ‰äº‹æƒ…啦â€ï¼Œå¯¹ä½ ä¸è®¨åŽŒä½†˜q˜æ²¡å–œæ¬¢ä¸Šçš„MMž®×ƒ¼šè¯´â€œå¥½å•Šï¼Œä¸è¿‡å¯ä»¥å¸¦ä¸Šæˆ‘åŒäº‹ä¹ˆåQŸâ€ï¼Œå·²ç»å–œæ¬¢ä¸Šä½ çš„MMž®×ƒ¼šè¯´â€œå‡ 炚w’ŸåQŸçœ‹å®Œç”µå½±å†åŽÀLˆöå§æ€Žä¹ˆæ øP¼Ÿâ€ï¼Œå½“ç„¶ä½ çœ‹ç”µåª„˜q‡ç¨‹ä¸è¡¨çŽ°è‰¯å¥½çš„è¯ï¼Œä¹Ÿå¯ä»¥æŠŠMM的状æ€ä»Žä¸è®¨åŽŒä¸å–œæ¬¢å˜æˆå–œæ¬¢å“¦ã€?
çŠ¶æ€æ¨¡å¼ï¼šçŠ¶æ€æ¨¡å¼å…è®æ€¸€ä¸ªå¯¹è±¡åœ¨å…¶å†…éƒ¨çŠ¶æ€æ”¹å˜çš„æ—¶å€™æ”¹å˜è¡Œä¸ºã€‚这个对象看上去象是改å˜äº†å®ƒçš„ç±»ä¸€æ —÷€‚çŠ¶æ€æ¨¡å¼æŠŠæ‰€ç ”ç©¶çš„å¯¹è±¡çš„è¡ŒäØ“åŒ…è£…åœ¨ä¸åŒçš„状æ€å¯¹è±¡é‡ŒåQŒæ¯ä¸€ä¸ªçжæ€å¯¹è±¡éƒ½å±žäºŽä¸€ä¸ªæŠ½è±¡çжæ€ç±»çš„一个å¾c…R€‚çŠ¶æ€æ¨¡å¼çš„æ„å›¾æ˜¯è®©ä¸€ä¸ªå¯¹è±¡åœ¨å…¶å†…éƒ¨çŠ¶æ€æ”¹å˜çš„æ—¶å€™ï¼Œå…¶è¡Œä¸ÞZ¹Ÿéšä¹‹æ”¹å˜ã€‚çŠ¶æ€æ¨¡å¼éœ€è¦å¯¹æ¯ä¸€ä¸ªç³»¾lŸå¯èƒ½å–得的状æ€åˆ›ç«‹ä¸€ä¸ªçжæ€ç±»çš„å¾c…R€‚当¾pÈ»Ÿçš„状æ€å˜åŒ–æ—¶åQŒç³»¾lŸä¾¿æ”¹å˜æ‰€é€‰çš„åç±»ã€?/p>
  21ã€STRATEGY—跟ä¸åŒ¾cÕdž‹çš„MM¾U¦ä¼šåQŒè¦ç”¨ä¸åŒçš„½{–ç•¥åQŒæœ‰çš„请电媄比较好,有的则去åƒå°åƒæ•ˆæžœä¸é”™ï¼Œæœ‰çš„åŽÀL“vè¾Ò޵ªæ¼«æœ€åˆé€‚,å•ç›®çš„éƒ½æ˜¯äØ“äº†å¾—åˆ°MM的芳心,我的˜q½MMé”¦å›Šä¸æœ‰å¥½å¤šStrategy哦ã€?
½{–略模å¼åQšç–略模å¼é’ˆå¯¹ä¸€¾l„算法,ž®†æ¯ä¸€ä¸ªç®—法å°è£…到å…ähœ‰å…±åŒæŽ¥å£çš„独立的¾cÖM¸åQŒä»Žè€Œä‹É得它们å¯ä»¥ç›¸äº’替æ¢ã€‚ç–略模å¼ä‹É得算法å¯ä»¥åœ¨ä¸åª„å“到客户端的情况下å‘生å˜åŒ–。ç–ç•¥æ¨¡å¼æŠŠè¡ŒäØ“å’ŒçŽ¯å¢ƒåˆ†å¼€ã€‚çŽ¯å¢ƒç±»è´Ÿè´£¾l´æŒå’ŒæŸ¥è¯¢è¡Œä¸ºç±»åQŒå„¿U算法在具体的ç–ç•¥ç±»ä¸æä¾›ã€‚ç”±äºŽç®—æ³•å’ŒçŽ¯å¢ƒç‹¬ç«‹å¼€æ¥ï¼Œ½Ž—法的增å‡ï¼Œä¿®æ”¹éƒ½ä¸ä¼šåª„å“到环境和客æˆïL«¯ã€?
22ã€TEMPLATE METHOD——看˜q‡ã€Šå¦‚何说æœå¥³ç”Ÿä¸ŠåºŠã€‹è¿™éƒ¨ç»å…¸æ–‡ç« å—åQŸå¥³ç”Ÿä»Žè®¤è¯†åˆîC¸ŠåºŠçš„ä¸å˜çš„æ¥éª¤åˆ†ä¸ºåéyé‡ã€æ‰“ç ´åƒµå±€ã€å±•å¼€˜q½æ±‚ã€æŽ¥å…R€å‰æˆã€åŠ¨æ‰‹ã€çˆ±æŠšã€è¿›åŽÕd…«å¤§æ¥éª?Template method)åQŒä½†æ¯ä¸ªæ¥éª¤é’ˆå¯¹ä¸åŒçš„æƒ…况,都有ä¸ä¸€æ ïLš„åšæ³•åQŒè¿™ž®Þp¦çœ‹ä½ éšæœºåº”å˜å•?具体实现)åQ?
æ¨¡æ¿æ–ÒŽ³•模å¼åQšæ¨¡æ¿æ–¹æ³•模å¼å‡†å¤‡ä¸€ä¸ªæŠ½è±¡ç±»åQŒå°†éƒ¨åˆ†é€»è¾‘以具体方法以åŠå…·ä½“æž„é€ åçš„åÅžå¼å®žçŽŽÍ¼Œç„¶åŽå£°æ˜Žä¸€äº›æŠ½è±¡æ–¹æ³•楘q«ä‹Éå类实现剩余的逻辑。ä¸åŒçš„åç±»å¯ä»¥ä»¥ä¸åŒçš„æ–¹å¼å®žçް˜q™äº›æŠ½è±¡æ–ÒŽ³•åQŒä»Žè€Œå¯¹å‰©ä½™çš„逻辑有ä¸åŒçš„实现。先制定一个顶¾U§é€»è¾‘框架åQŒè€Œå°†é€»è¾‘的细节留¾l™å…·ä½“çš„åç±»åŽÕd®žçްã€?
23ã€VISITORâ€”æƒ…äºø™Š‚åˆîCº†åQŒè¦¾l™æ¯ä¸ªMMé€ä¸€æŸé²œèŠ±å’Œä¸€å¼ å¡ç‰‡ï¼Œå¯æ˜¯æ¯ä¸ªMMé€çš„花都è¦é’ˆå¯¹å¥¹ä¸ªäh的特点,æ¯å¼ å¡ç‰‡ä¹Ÿè¦æ ÒŽ®ä¸ªäh的特ç‚ÒŽ¥æŒ‘,我一个äh哪æžå¾—清楚,˜q˜æ˜¯æ‰¾èŠ±åº—è€æ¿å’Œç¤¼å“åº—è€æ¿åšä¸€ä¸‹VisitoråQŒè®©èŠ±åº—è€æ¿æ ÒŽ®MM的特炚w€‰ä¸€æŸèбåQŒè®©½C¼å“åº—è€æ¿ä¹Ÿæ ¹æ®æ¯ä¸ªähç‰¹ç‚¹é€‰ä¸€å¼ å¡åQŒè¿™æ ·å°±è½ÀL¾å¤šäº†åQ?
讉K—®è€…模å¼ï¼šè®‰K—®è€…模å¼çš„目的是å°è£…ä¸€äº›æ–½åŠ äºŽæŸç§æ•°æ®¾l“æž„å…ƒç´ ä¹‹ä¸Šçš„æ“作。一旦这些æ“作需è¦ä¿®æ”¹çš„è¯ï¼ŒæŽ¥å—˜q™ä¸ªæ“作的数æ®ç»“æž„å¯ä»¥ä¿æŒä¸å˜ã€‚访问者模å¼é€‚用于数æ®ç»“构相å¯ÒŽœªå®šçš„¾pÈ»ŸåQŒå®ƒæŠŠæ•°æ®ç»“构和作用于结构上的æ“作之间的耦åˆè§£è„±å¼€åQŒä‹Éå¾—æ“作集åˆå¯ä»¥ç›¸å¯¹è‡ªç”Þqš„æ¼”化。访问者模å¼ä‹Éå¾—å¢žåŠ æ–°çš„æ“作å˜çš„很å®ÒŽ˜“åQŒå°±æ˜¯å¢žåŠ ä¸€ä¸ªæ–°çš„è®¿é—®è€…ç±»ã€‚è®¿é—®è€…æ¨¡å¼å°†æœ‰å…³çš„行为集ä¸åˆ°ä¸€ä¸ªè®¿é—®è€…对象ä¸åQŒè€Œä¸æ˜¯åˆ†æ•£åˆ°ä¸€ä¸ªä¸ªçš„节点类ä¸ã€‚当使用讉K—®è€…æ¨¡å¼æ—¶åQŒè¦ž®†å°½å¯èƒ½å¤šçš„对象‹¹è§ˆé€»è¾‘攑֜¨è®‰K—®è€…ç±»ä¸ï¼Œè€Œä¸æ˜¯æ”¾åˆ°å®ƒçš„å¾cÖM¸ã€‚访问者模å¼å¯ä»¥è·¨˜q‡å‡ 个类的牾U§ç»“构访问属于ä¸åŒçš„½{‰çñ”¾l“构的æˆå‘˜ç±»ã€?/p>
è½?a >http://java.ccidnet.com/art/3749/20060622/586221_3.html
Â
Employee
class could represent the set of all employees. An instance, on the other hand, is the instantiation of a class; that is, one of its members. For example, Victoria
could be an instance of the Employee
class, representing a particular individual as an employee. An instance has exactly the properties of its parent class (no more, no less).
A prototype-based language, such as JavaScript, does not make this distinction: it simply has objects. A prototype-based language has the notion of a prototypical object, an object used as a template from which to get the initial properties for a new object. Any object can specify its own properties, either when you create it or even at runtime. In addition, any object can be associated as the prototype for another object, allowing the second object to share the first object's properties.
In class-based languages, you define a class in a separate class definition. In that definition you can specify special methods, called constructors, to use to create instances of the class. A constructor method can specify initial values for the instance's properties and perform other processing appropriate at creation time. You use the new operator in association with the constructor method to create class instances.
JavaScript follows a similar model, but does not have a class definition separate from the constructor. Instead, you define a constructor function to create objects with a particular initial set of properties and values. Any JavaScript function can be used as a constructor. You use the new operator with a constructor function to create a new object.
In a class-based language, you create a hierarchy of classes through the class definitions. In a class definition, you can specify that the new class is a subclass of an already existing class. The subclass inherits all the properties of the superclass and additionally can add new properties or modify the inherited ones. For example, assume the Employee
class includes only name
and dept
properties and Manager
is a subclass of Employee
that adds the reports
property. In this case, an instance of the Manager
class would have all three properties: name
, dept
, and reports
.
JavaScript implements inheritance by allowing you to associate a prototypical object with any constructor function. So, you can create exactly the Employee
-Manager
example, but you use slightly different terminology. First you define the Employee
constructor function, specifying the name
and dept
properties. Next, you define the Manager
constructor function, specifying the reports
property. Finally, you assign a new Employee
object as the prototype
for the Manager
constructor function. Then, when you create a new Manager
, it inherits the name
and dept
properties from the Employee
object.
In class-based languages, you typically create a class at compile time and then you instantiate instances of the class either at compile time or at runtime. You cannot change the number or the type of properties of a class after you define the class. In JavaScript, however, at runtime you can add or remove properties from any object. If you add a property to an object that is used as the prototype for a set of objects, the objects for which it is the prototype also get the new property.
Table 1 gives a short summary of some of these differences. The rest of this paper describes the details of using JavaScript constructors and prototypes to create an object hierarchy and compares this to how you would do it in Java.
Table 1Â Comparison of class-based (Java) and prototype-based (JavaScript) object systems
Employee
has the properties name
(whose value defaults to the empty string) and dept
(whose value defaults to "general"
). Manager
is based on Employee
. It adds the reports
property (whose value defaults to an empty array, intended to have an array of Employee
objects as its value). WorkerBee
is also based on Employee
. It adds the projects
property (whose value defaults to an empty array, intended to have an array of strings as its value). SalesPerson
is based on WorkerBee
. It adds the quota
property (whose value defaults to 100). It also overrides the dept
property with the value "sales"
, indicating that all salespersons are in the same department. Engineer
is based on WorkerBee
. It adds the machine
property (whose value defaults to the empty string) and also overrides the dept
property with the value "engineering"
. Employee
definitions below are similar. The only difference is that you need to specify the type for each property in Java but not in JavaScript and you need to create an explicit constructor method for the Java class.
Manager
and WorkerBee definitions show the difference in how you specify the next object higher in the inheritance chain. In JavaScript, you add a prototypical instance as the value of the prototype
property of the constructor function. You can do so at any time after you define the constructor. In Java, you specify the superclass within the class definition. You cannot change the superclass outside the class definition.
Engineer
and SalesPerson
definitions create objects that descend from WorkerBee
and hence from Employee
. An object of these types has properties of all the objects above it in the chain. In addition, these definitions override the inherited value of the dept
property with new values specific to these objects.
NOTE: As described earlier, the term instance has a specific technical meaning in class-based languages. In these languages, an instance is an individual member of a class and is fundamentally different from a class. In JavaScript, "instance" does not have this technical meaning because JavaScript does not have this difference between classes and instances. However, in talking about JavaScript, "instance" can be used informally to mean an object created using a particular constructor function. So, in this example, you could informally say thatFigure 3Â Â Â Creating objects with the simple definitionsjane
is an instance ofEngineer
. Similarly, although the terms parent, child, ancestor, and descendant do not have formal meanings in JavaScript, we can use them informally to refer to objects higher or lower in the prototype chain.
mark
object as a WorkerBee
as shown in Figure 3 with this statement:
mark = new WorkerBee;When JavaScript sees the
new
operator, it creates a new generic object and passes this new object as the value of the this
keyword to the WorkerBee
constructor function. The constructor function explicitly sets the value of the projects
property. It also sets the value of the internal __proto__
property to the value of WorkerBee.prototype
. (That property name has 2 underscore characters at the front and 2 at the end.) The __proto__
property determines the prototype chain used to return property values. Once these properties are set, JavaScript returns the new object and the assignment statement sets the variable mark
to that object.
This process doesn't explicitly put values in the mark
object (local values) for the properties mark
inherits from the prototype chain. When you ask for the value of a property, JavaScript first checks to see if the value exists in that object. If it does, that value is returned. If the value isn't there locally, JavaScript checks the prototype chain (using the __proto__
property). If an object in the prototype chain has a value for the property, that value is returned. If no such property is found, JavaScript says the object doesn't have the property. In this way, the mark
object has the following properties and values:
mark.name = "";The
mark.dept = "general";
mark.projects = [];
mark
object inherits values for the name
and dept
properties from the prototypical object in mark.__proto__
. It is assigned a local value for the projects
property by the WorkerBee
constructor. Simply put, this gives you inheritance of properties and their values in JavaScript. Some subtleties of this process are discussed in .
Because these constructors don't let you supply instance-specific values, this information is generic. The property values are the default ones shared by all new objects created from WorkerBee
. You can, of course, change the values of any of these properties. So, you could give specific information for mark
as shown here:
mark.name = "Doe, Mark";
mark.dept = "admin";
mark.projects = ["navigator"];
mark.bonus = 3000;Now, the
mark
object has a bonus
property, but no other WorkerBee
has this property.
If you add a new property to an object that is being used as the prototype for a constructor function, you add that property to all objects that inherit properties from the prototype. For example, you can add a specialty
property to all employees with the following statement:
Employee.prototype.specialty = "none";As soon as JavaScript executes this statement, the
mark
object also has the specialty
property with the value of "none"
. Figure 4 shows the effect of adding this property to the Employee
prototype and then overriding it for the Engineer
prototype.
Figure 4Â Â Â Adding properties
this.name = name || "";The JavaScript logical OR operator (
||
) evaluates its first argument. If that argument is converts to true, the operator returns it. Otherwise, the operator returns the value of the second argument. Therefore, this line of code tests to see if name
has a useful value for the name
property. If it does, it sets this.name
to that value. Otherwise, it sets this.name
to the empty string. This paper uses this idiom for brevity; however, it can be puzzling at first glance.
With these definitions, when you create an instance of an object, you can specify values for the locally defined properties. As shown in Figure 5, you can use this statement to create a new Engineer
:
jane = new Engineer("belau");Jane's properties are now:
jane.name == "";Notice that with these definitions, you cannot specify an initial value for an inherited property such as
jane.dept == "general";
jane.projects == [];
jane.machine == "belau"
name
. If you want to specify an initial value for inherited properties in JavaScript, you need to add more code to the constructor function.
So far, the constructor function has created a generic object and then specified local properties and values for the new object. You can have the constructor add more properties by directly calling the constructor function for an object higher in the prototype chain. Figure 6 shows these new definitions.
Figure 6Â Â Â Specifying properties in a constructor, take 2
Engineer
constructor:
function Engineer (name, projs, mach) {Assume we create a new
this.base = WorkerBee;
this.base(name, "engineering", projs);
this.projects = mach || "";
}
Engineer
object as follows:
jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");JavaScript follows these steps:
1.   First, thenew
operator creates a generic object and sets its__proto__
property toEngineer.prototype
.
2.   Thenew
operator then passes the new object to theEngineer
constructor as the value of thethis
keyword.
3.   Next, the constructor creates a new property calledbase
for that object and assigns the value of theWorkerBee
constructor to thebase
property. This makes theWorkerBee
constructor a method of theEngineer
object.
NOTE: The name of thebase
property is not special. You can use any legal property name;base
is simply evocative of its purpose.
4.   Next, the constructor calls thebase
method, passing as its arguments two of the arguments passed to the constructor ("Doe, Jane"
and["navigator", "javascript"]
) and also the string"engineering"
. Explicitly using"engineering"
in the constructor indicates that allEngineer
objects have the same value for the inheriteddept
property and this value overrides the value inherited fromEmployee
.
5.   Becausebase
is a method ofEngineer
, within the call tobase
, JavaScript binds thethis
keyword to the object created in step 1. Thus, theWorkerBee
function in turn passes the"Doe, Jane"
and["navigator", "javascript"]
arguments to theEmployee
constructor function. Upon return from theEmployee
constructor function, theWorkerBee
function uses the remaining argument to set theprojects
property.
6.   Upon return from thebase
method, theEngineer
constructor initializes the object'smachine
property to"belau"
.
7.   Upon return from the constructor, JavaScript assigns the new object to the jane
variable.
You might think that, having called the WorkerBee
constructor from inside the Engineer
constructor, you've set up inheritance appropriately for Engineer
objects. This is not the case. Calling the WorkerBee
constructor ensures that an Engineer
object starts out with the properties specified in all constructor functions that are called. However, if you later add properties to the Employee
or WorkerBee
prototypes, those properties are not inherited by the Engineer
object. For example, assume you have these statements:
function Engineer (name, projs, mach) {The
this.base = WorkerBee;
this.base(name, "engineering", projs);
this.projects = mach || "";
}
jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");
Employee.prototype.specialty = "none";
jane
object does not inherit the specialty
property. You still need to explicitly set up the prototype to ensure dynamic inheritance. Assume instead you have these statements:
function Engineer (name, projs, mach) {Now the value of the
this.base = WorkerBee;
this.base(name, "engineering", projs);
this.projects = mach || "";
}
Engineer.prototype = new WorkerBee;
jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");
Employee.prototype.specialty = "none";
jane
object's specialty
property is "none"
.
__proto__
property). function Employee () {
this.name = "";
this.dept = "general";
}
function WorkerBee () {With these definitions, assume you create
this.projects = [];
}
WorkerBee.prototype = new Employee;
amy
as an instance of WorkerBee
with this statement:
amy = new WorkerBee;The
amy
object has one local property, projects
. The values for the name
and dept
properties are not local to amy
and so are gotten from the amy
object's __proto__
property. Thus, amy
has these property values:
amy.name == "";Now assume you change the value of the
amy.dept = "general";
amy.projects == [];
name
property in the prototype associated with Employee
:
Employee.prototype.name = "Unknown"At first glance, you might expect that new value to propagate down to all the instances of
Employee
. However, it does not.
When you create any instance of the Employee
object, that instance gets a local value for the name
property (the empty string). This means that when you set the WorkerBee
prototype by creating a new Employee
object, WorkerBee.prototype
has a local value for the name
property. Therefore, when JavaScript looks up the name
property of the amy
object (an instance of WorkerBee
), JavaScript finds the local value for that property in WorkerBee.prototype
. It therefore does not look farther up the chain to Employee.prototype
.
If you want to change the value of an object property at runtime and have the new value be inherited by all descendants of the object, you cannot define the property in the object's constructor function. Instead, you add it to the constructor's associated prototype. For example, assume you change the code above to the following:
function Employee () {
   this.dept = "general";
}
Employee.prototype.name = "";
function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee;
amy = new WorkerBee;
Employee.prototype.name = "Unknown";In this case, the
name
property of amy
becomes "
Unknown"
.
As these examples show, if you want to have default values for object properties and you want to be able to change the default values at runtime, you should set the properties in the constructor's prototype, not in the constructor function itself.
instanceof
operator for this purpose. JavaScript does not provide instanceof
, but you can write such a function yourself.
As discussed in , when you use the new
operator with a constructor function to create a new object, JavaScript sets the __proto__
property of the new object to the value of the prototype
property of the constructor function. You can use this to test the prototype chain.
For example, assume you have the same set of definitions we've been using, with the prototypes set appropriately. Create an __proto__
object as follows:
chris = new Engineer("Pigman, Chris", ["jsd"], "fiji");With this object, the following statements are all true:
chris.__proto__ == Engineer.prototype;Given this, you could write an
chris.__proto__.__proto__ == WorkerBee.prototype;
chris.__proto__.__proto__.__proto__ == Employee.prototype;
chris.__proto__.__proto__.__proto__.__proto__ == Object.prototype;
chris.__proto__.__proto__.__proto__.__proto__.__proto__ == null;
instanceOf
function as follows:
function instanceOf(object, constructor) {With this definition, the following expressions are all true:
   while (object != null) {
      if (object == constructor.prototype)
         return true;
      object = object.__proto__;
   }
   return false;
}
instanceOf (chris, Engineer)But this expression is false:
instanceOf (chris, WorkerBee)
instanceOf (chris, Employee)
instanceOf (chris, Object)
instanceOf (chris, SalesPerson)
Employee
:
var idCounter = 1;
function Employee (name, dept) {With this definition, when you create a new
   this.name = name || "";
   this.dept = dept || "general";
   this.id = idCounter++;
}
Employee
, the constructor assigns it the next ID in sequence and then increments the global ID counter. So, if your next statement were:
victoria = new Employee("Pigbert, Victoria", "pubs")
harry = new Employee("Tschopik, Harry", "sales")
victoria.id
is 1 and harry.id
is 2. At first glance that seems fine. However, idCounter
gets incremented every time an Employee
object is created, for whatever purpose. If you create the entire Employee
hierarchy we've been working with, the Employee
constructor is called every time we set up a prototype. That is, assume you have this code:
var idCounter = 1;
function Employee (name, dept) {
   this.name = name || "";
   this.dept = dept || "general";
   this.id = idCounter++;
}
function Manager (name, dept, reports) {...}
Manager.prototype = new Employee;
function WorkerBee (name, dept, projs) {...}
WorkerBee.prototype = new Employee;
function Engineer (name, projs, mach) {...}
Engineer.prototype = new WorkerBee;
function SalesPerson (name, projs, quota) {...}
SalesPerson.prototype = new WorkerBee;
mac = new Engineer("Wood, Mac");Further assume that the definitions we've omitted here have the
base
property and call the constructor above them in the prototype chain. In this case, by the time the mac
object is created, mac.id
is 5.
Depending on the application, it may or may not matter that the counter has been incremented these extra times. If you care about the exact value of this counter, one possible solution involves instead using this constructor:
function Employee (name, dept) {When you create an instance of
   this.name = name || "";
   this.dept = dept || "general";
   if (name)
      this.id = idCounter++;
}
Employee
to use as a prototype, you do not supply arguments to the constructor. Using this definition of the constructor, when you do not supply arguments, the constructor does not assign a value to the id and does not update the counter. Therefore, for an Employee
to get an assigned id, you must specify a name for the employee. In our example, mac.id
would be 1.
function Hobbyist (hobby) {
   this.hobby = hobby || "scuba";
}
function Engineer (name, projs, mach, hobby) {
   this.base1 = WorkerBee;
   this.base1(name, "engineering", projs);
   this.base2 = Hobbyist;
   this.base2(hobby);
   this.projects = mach || "";
}
Engineer.prototype = new WorkerBee;
dennis = new Engineer("Doe, Dennis", ["collabra"], "hugo")Further assume that the definition of
WorkerBee
is as we've previously seen it. In this case, the dennis object has these properties:
dennis.name == "Doe, Dennis"So
dennis.dept == "engineering"
dennis.projects == ["collabra"]
dennis.machine == "hugo"
dennis.hobby == "scuba"
dennis
does get the hobby
property from the Hobbyist
constructor. However, assume you then add a property to the Hobbyist
constructor's prototype:
Hobbyist.prototype.equipment = ["mask", "fins", "regulator", "bcd"]The
dennis
object does not inherit this new property.
Last Updated: 12/18/97 15:19:54
ç ”ç©¶¾~–程è¯è¨€ä¸çš„“丑ž®é¸â€?/p>
![]() |
|
¾U§åˆ«: åˆçñ”
Bruce Tate
(bruce.tate@j2life.com), 总è£, RapidRed
2007 òq?1 æœ?18 æ—?/p>
JavaScript 常被äºÞZ»¬è®¤äؓ是编½E‹è¯a€ä¸æ— ‘Œ™½»é‡çš„一员。这¿U观点的形æˆå¯ä»¥â€œå½’功â€äºŽå…¶å¼€å‘工兗÷€å¤æ‚且ä¸ä¸€è‡´çš„é¢å‘ HTML ™åµé¢çš„æ–‡æ¡£å¯¹è±¡æ¨¡åž‹ä»¥åŠä¸ä¸€è‡´çš„‹¹è§ˆå™¨å®žçŽ°ã€‚ä½† JavaScript ¾l对ä¸ä»…仅是一个玩兯‚¿™ä¹ˆç®€å•。在本文ä¸ï¼ŒBruce Tate 呿‚¨ä»‹ç»äº?JavaScript çš„è¯a€ç‰ÒŽ€§ã€?
å‡ ä¹Žæ¯ä¸ª Web å¼€å‘äh员都曾有˜q‡è¯…å’?JavaScript çš„ç»åŽ†ã€‚è¿™ä¸ªå¤‡å—争议的è¯è¨€å—ç¯äºŽå…¶å¤æ‚的称为文档对象模åž?(DOM)的编½E‹æ¨¡åž‹ã€ç³Ÿ¾p•的实现和调试工具以åŠä¸ä¸€è‡´çš„‹¹è§ˆå™¨å®žçŽ°ã€‚ç›´åˆ°æœ€˜q‘,很多开å‘ähå‘˜è¿˜è®¤äØ“ Javascript 从最好的斚w¢è¯´æ˜¯æ— å¯é¿å…之羼œ¸ï¼Œä»Žæœ€å的斚w¢è¯´ä¸˜q‡æ˜¯ä¸€¿U玩å…ïLŞ了ã€?
ç„¶è€?JavaScript 现在开始日益é‡è¦è“væ¥ï¼Œè€Œä¸”æˆäؓ了广泛应用于 Web å¼€å‘的脚本è¯è¨€ã€‚JavaScript çš„å¤è‹ä‹É一些业界领袖äh物也ä¸å¾—ä¸å¼€å§‹é‡æ–°å®¡è§†è¿™¿Uç¼–½E‹è¯a€ã€‚诸å¦?Ajax (Asynchronous JavaScript + XML) ˜q™æ ·çš„ç¼–½E‹æŠ€æœ¯è®© Web ¾|‘é¡µæ›´åŠ ˜q·äh。而完整的 Web 开呿¡†æžÓž¼Œæ¯”如 Apache CocoonåQŒåˆ™è®?JavaScript 的应用越æ¥è¶Šå¤šï¼Œä½¿å…¶ä¸åªé™äºŽæ˜¯ä¸€¿U用于制ä½?Web ™åµé¢çš„简å•脚本。JavaScript 的一¿Uç§°ä¸?ActionScript 的派生物也推动了 Macromedia çš?Flash 客户端框架的å‘展。è¿è¡Œåœ¨ JVM 上的实现 Rhino è®?JavaScript æˆäØ“äº?Javaâ„?å¼€å‘äh员所首选的一¾c»è„šæœ¬è¯a€åQˆå‚è§?å‚考资æ–?/font>åQ‰ã€?
我的好å‹å…¼åŒäº?Stuart Halloway æ˜?Ajax æ–šw¢çš„专å®Óž¼Œæ›‘Öœ¨å…¶æ•™æŽˆçš„ JavaScript 评¡¨‹ä¸åš˜q‡è¿™æ ïLš„开场白åQšâ€œåˆ° 2011 òqß_¼ŒJavaScript ž®†è¢«å…¬è®¤ä¸ºæ˜¯ä¸€¿U拥有开å‘çŽ°ä»£åº”ç”¨ç¨‹åºæ‰€éœ€çš„一整套新特性的è¯è¨€â€?。他¾l§è€Œä»‹¾l说 JavaScript ½E‹åºè¦æ¯”¾cÖM¼¼çš?Java ½E‹åºç´§å¯†åå€ï¼Œòq¶ç‘ô¾l展½CÞZº†ä½¿å…¶ä¹‹æ‰€ä»¥å¦‚æ¤çš„一些è¯a€ç‰ÒŽ€§ã€?
![]() |
|
在这½‹‡æ–‡ç« ä¸åQŒæˆ‘ž®†å¸¦æ‚¨æŽ¢½I?JavaScript 的一些特性,看看˜q™äº›ç‰ÒŽ€§å¦‚何让它如æ¤å…·æœ‰å¸å¼•力åQ?
您å¯èƒ½å·²¾l熟悉动æ€ç±»åž‹æ¨¡åž‹ã€é«˜é˜¶å‡½æ•°åÅžå¼çš„函数å¼ç¼–½E‹ä»¥åŠå¼€æ”‘Ö¯¹è±¡æ¨¡åž‹è¿™äº›æ¦‚å¿µï¼Œå› äØ“æˆ‘åœ¨å…¶ä»–çš?i>跨越边界 ¾pÕdˆ—æ–‡ç« ä¸å·²¾l作˜q‡ç›¸å…³çš„介ç»ã€‚如果您从未˜q›è¡Œ˜q‡ä“Q何æ£å¼çš„ JavaScript å¼€å‘,您很å¯èƒ½ä¼šè®¤ä¸ø™¿™äº›ç‰¹æ€§å±žäºŽéžå¸¸å¤æ‚çš„è¯è¨€åQŒä¾‹å¦?Pythonã€Lispã€Smalltalk å’?HaskellåQŒè€Œç»éžåƒ JavaScript ˜q™æ ·çš„è¯a€æ‰€èƒ½æä¾›çš„ã€‚å› æ¤ï¼Œæˆ‘å°†ç”¨å®žé™…çš„ä»£ç ½CÞZ¾‹æ¥è¯´æ˜Žè¿™äº›æ¦‚å¿üc€?
æ‚¨æ— éœ€è®„¡½® JavaScript。如果您å¯ä»¥åœ¨æµè§ˆå™¨ä¸é˜…è¯ÀL¤½‹‡æ–‡ç« ,ž®Þp¯æ˜Žæ‚¨å·²ç»å‡†å¤‡ž®ÞqÈA了。本文包å«çš„æ‰€æœ‰ç¼–½E‹ç¤ºä¾‹éƒ½å¯ä»¥åœ¨å¤§å¤šæ•°‹¹è§ˆå™¨å†…˜q行。我使用的是 Firefoxã€?
用在 <script type='text/javascript'>
å’?</script>
æ ‡è®°ä¹‹é—´æ‰€åŒ…å«çš?JavaScript åŠ è²½Ž€å•çš„ Web ™åµé¢ã€‚清å?1 å¯ä»¥æ˜„¡¤º Hello, World 文本åQ?
<script type='text/javascript'> alert('Hello, World.') </script> |
è¦è¿è¡Œæ¤ä»£ç åQŒåªéœ€åˆ›å¾ä¸€ä¸ªåä¸?example1.html çš„æ–‡ä»¶ã€‚å°†æ¸…å• 1 的代ç å¤åˆ¶åˆ°è¯¥æ–‡ä»¶å†…åQŒåƈ在æµè§ˆå™¨ä¸åŠ è½½æ¤æ–‡äšgåQˆå‚çœ?ä¸‹è² éƒ¨åˆ†ä»¥èŽ·å¾—æœ¬æ–‡ä‹É用的所有示ä¾?HTML æ–‡äšgåQ‰ã€‚注æ„åˆ°æ¯æ¬¡é‡è²æ¤é¡µé¢æ—¶åQŒè¯¥ä»£ç éƒ½ä¼šç«‹å³æ‰§è¡Œã€?/p>
alert
是个函数调用åQŒåªæœ‰ä¸€ä¸ªå—½W¦ä¸²ä½œäØ“å‚æ•°ã€‚图 1 昄¡¤ºäº†ç”±æ¸…å• 1 ä¸çš„代ç 弹出的è¦å‘Šæ¡†åQŒæ˜¾½Cºæ–‡æœ?“Hello, Worldâ€ã€‚如果代ç 在 HTML body 之内åQˆç›®å‰åƈ未指定ä“Qä½?bodyåQŒä½†‹¹è§ˆå™¨èƒ½æŽ¥å—ä¸è§„则的 HTMLåQŒåƈ且整个页é¢éƒ½é»˜ç„¶ä½œäؓ一ä¸?body 被处ç†ï¼‰ã€‚页é¢ä¸€æ—¦åŠ è½½ï¼ŒJavaScript ž®×ƒ¼šç«‹å³æ‰§è¡Œã€?
如果è¦åšg˜qŸæ‰§è¡Œï¼Œå¯ä»¥åœ?HTML <head>
å…ƒç´ å£°æ˜Ž JavaScript 函数åQŒå¦‚æ¸…å• 2 所½Cºï¼š
<head> <script type='text/javascript'> function hello() { alert('Hello, World.') } </script> </head> <body> <button onclick="hello();">Say Hello</button> </body> |
ž®†æ¸…å?2 ä¸çš„代ç 输入åˆîC¸€ä¸?HTML æ–‡äšgåQŒåœ¨‹¹è§ˆå™¨å†…åŠ è²è¯¥æ–‡ä»Óž¼Œå•击 Say Hello 按钮åQŒç»“果如å›?2 所½Cºï¼š
![]() ![]() |
![]()
|
ä»?æ¸…å• 2åQŒå¯ä»¥å¤§è‡´ä½“会到一äº?JavaScript 在擾Uµå‡½æ•°æ–¹é¢çš„能力。将函数åç§°ä¼ é€’ç»™ HTML button
æ ‡è®°òq¶åˆ©ç”?HTML 的内¾|®äº‹ä»¶æ¨¡åž‹ã€‚ä‹Éç”?JavaScript æ—Óž¼Œæˆ‘会¾l常在å˜é‡æˆ–数组ä¸å˜å‚¨å‡½æ•ŽÍ¼ˆåœ¨æœ¬æ–‡åŽé¢çš„ 对象模型 一节,您会看到 JavaScript 对象模型½{–略大é‡ä½¿ç”¨äº†æ¤æŠ€å·§ï¼‰ã€‚例如,查看一下清å?3åQ?
<head> <script type='text/javascript'> hot = function hot() { alert('Sweat.') } cold = function cold() { alert('Shiver.') } function swap() { temp = hot hot = cold cold = temp alert('Swapped.') } </script> </head> <body> <button onclick="hot();">Hot</button> <button onclick="cold();">Cold</button> <button onclick="swap();">Swap</button> </body> |
函数æ˜?JavaScript ä¸çš„一¾cÕd¯¹è±¡ï¼Œå¯ä»¥è‡ªç”±åœ°æ“¾Uµå®ƒä»¬ã€‚首先我声明两个函数åQ?code>hot å’?cold
ã€‚åÆˆåˆ†åˆ«åœ¨ä¸åŒçš„å˜é‡å˜å‚¨å®ƒä»¬ã€‚å•å‡?Hot æˆ?Cold 按钮会调用对应的函数åQŒç”Ÿæˆä¸€ä¸ªå‘Šè¦ã€‚接下æ¥åQŒå£°æ˜Žå¦ä¸€ä¸ªå‡½æ•°ç”¨æ¥äº¤æ?Hot å’?Cold 按钮的å€û|¼Œž®†æ¤å‡½æ•°ä¸Žç¬¬ä¸‰ä¸ªæŒ‰é’®å…Œ™”åQŒè¯¥æŒ‰é’®æ˜„¡¤ºå¦‚图 3 所½Cºçš„告è¦åQ?
˜q™ä¸ªä¾‹å说明å¯ä»¥åƒå¤„ç†å…¶ä»–å˜é‡ä¸€æ ·å¤„ç†å‡½æ•°ã€‚C å¼€å‘äh员很å®ÒŽ˜“ž®†æ¤æ¦‚念看作æ˜?i>函数指针 功能åQŒä½† JavaScript çš„é«˜é˜¶å‡½æ•°çš„åŠŸèƒ½æ›´äØ“å¼ºå¤§ã€‚è¯¥ç‰ÒŽ€§è®© JavaScript ½E‹åºå‘˜èƒ½å¤Ÿåƒå¤„ç†å…¶ä»–å˜é‡¾cÕdž‹ä¸€æ ¯‚½»æ‘Ö¤„ç†åŠ¨ä½œæˆ–å‡½æ•°ã€?/p>
ž®†å‡½æ•°ç”¨ä½œå‡½æ•°çš„傿•°åQŒæˆ–ž®†å‡½æ•îC½œä¸ºå€ÆD¿”回,˜q™äº›æ¦‚念属于高阶函数的领域。清å?4 å¯?æ¸…å• 3 åšäº†ä¸€ç‚¹ç‚¹ä¿®æ”¹åQŒæ˜¾½CÞZº†èƒ½è¿”回函数的高阶函数åQ?/p>
æ¸…å• 4. 高阶函数
<head> <script type='text/javascript'> function temperature() { return current } hot = function hot() { alert('Hot.') } cold = function cold() { alert('Cold.') } current = hot function swap() { if(current == hot) { current = cold } else { current = hot } } </script> </head> <body> <button onclick="funct = temperature()();">Temperature</button> <button onclick="swap();">Swap</button> </body> |
˜q™ä¸ªä¾‹å解决了一个常è§é—®é¢˜ï¼šå¦‚何ž®†æ›´æ”¹ä¸çš„è¡Œä¸ºé™„åŠ åˆ°ç”¨æˆ·æŽ¥å£äº‹äšgåQŸé€šè¿‡é«˜é˜¶å‡½æ•°åQŒè¿™å¾ˆå®¹æ˜“åšåˆ°ã€?code>temperature 高阶函数˜q”回 current
çš„å€û|¼Œè€?current åˆå¯ä»¥æœ‰ hot
�cold
函数。看一下这个有些陈旧的函数调用åQ?code>temperature()()。第一¾l„括åïL”¨äºŽè°ƒç”?temperature
函数。第二组括å·è°ƒç”¨ç”?temperature
˜q”回 的函数。图 4 昄¡¤ºäº†è¾“出:
高阶函数是函数弾~–程的基¼‹€åQŒå¯¹æ¯”é¢å‘对象编½E‹ï¼Œå‡½æ•°å¼ç¼–½E‹ä»£è¡¨äº†æ›´é«˜¾U§åˆ«çš„æŠ½è±¡ã€‚但 JavaScript çš„å®žåŠ›åÆˆä¸ä»…é™äºŽé«˜é˜¶å‡½æ•°ã€‚JavaScript 的动æ€ç±»åž‹å°±æžäØ“é€‚åˆ UI å¼€å‘ã€?/p>
![]() ![]() |
![]()
|
é€šè¿‡é™æ€ç±»åž‹ï¼Œ¾~–译器å¯ä»¥æ£€æŸ¥å‚æ•°å’Œå˜é‡çš„值或针对一个给定æ“作所å…许的返回倹{€‚其优势是编译器å¯ä»¥åšé¢å¤–的错误‹‚€æŸ¥ã€‚è€Œä¸”é™æ€ç±»åž‹è¿˜å¯ä»¥ä¸ø™¯¸å¦?IDE ˜q™æ ·çš„å·¥å…äh供更多信æ¯ï¼Œå¸¦æ¥å…¶ä»–一些特性,比如更好的代ç 完æˆåŠŸèƒ½ã€‚ä½†é™æ€ç±»åž‹ä¹Ÿå˜åœ¨ç€å¦‚下一些劣势:
陿€ç±»åž‹æ›´é€‚åˆç”¨äºŽæž„å¾ä¸é—´ä»¶æˆ–æ“作¾pÈ»Ÿçš„è¯a€ä¸ã€‚UI å¼€å‘常帔Rœ€è¦æ›´é«˜çš„æ•ˆçŽ‡å’Œçµ‹zÀL€§ï¼Œæ‰€ä»¥æ›´é€‚åˆé‡‡ç”¨åЍæ€ç±»åž‹ã€‚我æ·ÞqŸ¥˜q™ç§å𿳕å˜åœ¨å±é™©ã€‚相信ä‹É用过 JavaScript çš?Web å¼€å‘äh员都曄¡»ä¸ºç¼–译器本应‹‚€‹¹‹åˆ°çš„错误类型的å˜é‡è€Œç»žž®½è„‘æ±ã€‚但它所带æ¥çš„ä¼˜åŠ¿åŒæ ·ä¸å¯å¦è®¤ã€‚下é¢å°†ä¸¾ä¾‹åŠ ä»¥è¯´æ˜Žã€?/p>
首先åQŒè€ƒè™‘ä¸€ä¸ªå¯¹è±¡çš„æƒ…å†µã€‚åœ¨æ¸…å• 5 ä¸ï¼Œåˆ›å¾ä¸€ä¸ªæ–°å¯¹è±¡åQŒåƈ讉K—®ä¸€ä¸ªä¸å˜åœ¨çš„属性,å䨓 color
åQ?/p>
æ¸…å• 5. 引入一个属æ€?/b>
<script type='text/javascript'>
blank_object = new Object();
blank_object.color = 'blue'
alert('The color is ' + blank_object.color)
</script>
å½“åŠ è½½åÆˆæ‰§è¡Œæ¤åº”ç”¨ç¨‹åºæ—¶åQŒä¼šå¾—到如图 5 所½Cºçš„¾l“æžœåQ?/p>
�5. 引入属�/b>
JavaScript òq¶ä¸ä¼šæŠ¥å‘?blue
属性ä¸å˜åœ¨çš„é”™è¯¯ã€‚é™æ€ç±»åž‹çš„æ‹¥æŠ¤è€…大都会被本例所å“å€’ï¼Œå› äØ“æœ¬ä¾‹ä¸çš„错误被很好地éšåŒ¿äº†ã€‚虽然这¿Uåšæ³•多ž®‘ä¼šè®©æ‚¨æ„Ÿè§‰æœ‰äº›ä¸æ£å½“,但您也ä¸èƒ½å¦è®¤å®ƒå·¨å¤§çš„诱惑力。您å¯ä»¥å¾ˆå¿«å¼•入属性。如果将本例和本文之å‰çš„例å¾l“åˆèµäh¥åQŒè¿˜å¯ä»¥å¼•å…¥è¡ŒäØ“ã€‚è®°ä½ï¼Œå˜é‡å¯ä»¥ä¿å˜å‡½æ•°åQ所以,åŸÞZºŽåЍæ€ç±»åž‹å’Œé«˜é˜¶å‡½æ•°åQŒæ‚¨å¯ä»¥åœ¨ä“Q何时候呾cÖM¸å¼•å…¥ä»ÀL„的行为ã€?
å¯ä»¥è½ÀL¾åœ°é‡å†?æ¸…å• 5åQŒä‹Éå…¶å¦‚æ¸…å• 6 所½Cºï¼š
<script type='text/javascript'> blank_object = new Object(); blank_object.color = function() { return 'blue'} alert('The color is ' + blank_object.color()) </script> |
从上例å¯ä»¥çœ‹å‡ºï¼Œåœ?JavaScript çš„ä¸åŒæ¦‚念之间å¯ä»¥å¦‚æ¤è½»æ‘Öœ°æ¥å›žå˜æ¢åQŒå…¶å«ä¹‰ä¸Šçš„å˜åŒ–很大 —â€?比如åQŒæ˜¯å¼•å…¥è¡ŒäØ“˜q˜æ˜¯å¼•å…¥æ•°æ® â€”â€?ä½†è¯æ³•上的å˜åŒ–å´å¾ˆå°ã€‚该è¯è¨€å¾ˆå¥½çš„åšg展性是它的一¿Uä¼˜åŠ¿ï¼Œä½†åŒæ ·ä¹Ÿæ˜¯å…¶¾~ºç‚¹æ‰€åœ¨ã€‚实际上åQŒè¯¥è¯è¨€æœ¬èín的对象模型就æ˜?JavaScript 延展½E‹åº¦çš„一¿U体现ã€?/p>
![]() ![]() |
![]()
|
到目å‰äØ“æ¢ï¼Œæ‚¨åº”该对 JavaScript 有一个棼‹®çš„评äh了,它ç»éžåªå¦‚一个玩具那么简å•。事实上åQŒå¾ˆå¤šäh都ä‹Éç”¨è¿‡å…¶å¯¹è±¡æ¨¡åž‹åˆ›å»ø™¿‡æžäØ“å¤æ‚ã€è®¾è®¡è‰¯å¥½çš„é¢å‘对象软äšg。但对象模型ž®¤å…¶æ˜¯ç”¨äºŽç‘ô承的对象模型åˆéžæ‚¨ä¸€è´¯è®¤ä¸ºçš„é‚£æ ·ã€?
Java è¯è¨€æ˜¯åŸºäºŽç±»çš„。当构å¾åº”用½E‹åºæ—Óž¼Œä¹ŸåŒæ—¶æž„å»ÞZº†å¯ä»¥ä½œäؓ所有对象的模æ¿çš„æ–°¾c…R€‚ç„¶åŽè°ƒç”?new
æ¥å®žä¾‹åŒ–该模æ¿ï¼Œåˆ›å¾ä¸€ä¸ªæ–°å¯¹è±¡ã€‚而在 JavaScript ä¸ï¼Œæ‰€åˆ›å¾çš„æ˜¯ä¸€ä¸ªåŽŸåž‹ï¼Œæ¤åŽŸåž‹æ˜¯ä¸€ä¸ªå®žä¾‹ï¼Œå¯ä»¥åˆ›å¾æ‰€æœ‰æœªæ¥çš„对象ã€?
现在先暂且放下这些抽象的概念åQŒåŽ»æŸ¥çœ‹ä¸€äº›å®žé™…ä»£ç ã€‚æ¯”å¦‚ï¼Œæ¸…å• 7 创å¾äº†ä¸€ä¸ªç®€å•çš„ Animal
åQŒå®ƒå…ähœ‰ name
属性和 speak
动作。其他动物会从这个基¼‹€¾l§æ‰¿ã€?
<script type='text/javascript'> Animal = function() { this.name = "nobody" this.speak = function () { return "Who am I?" } } myAnimal = new Animal(); alert('The animal named ' + myAnimal.name + ' says ' + myAnimal.speak()); </script> |
æ¸…å• 7 的结果如å›?6 所½Cºï¼š
对于 Java å¼€å‘äh员而言åQŒæ¸…å?7 ä¸çš„代ç 看è“væ¥å¤šž®‘有点生ç–和奇怪。实际上对于没有亲自构徘q‡å¯¹è±¡çš„许多 JavaScript å¼€å‘äh员æ¥è¯ß_¼Œ˜q™äº›ä»£ç åŒæ ·çœ‹è“væ¥æœ‰ç‚¹ç”Ÿç–和奇怪。也许,下é¢çš„解释å¯ä»¥è®©å¤§å®¶èƒ½å¤Ÿæ›´å¥½åœ°ç†è§£è¿™ŒDµä»£ç ã€?
实际上,您åªéœ€é‡ç‚¹å…Ïx³¨å…¶ä¸ä¸‰æ®µä¿¡æ¯ã€‚首先,JavaScript 用嵌套函数表½Cºå¯¹è±¡ã€‚è¿™æ„å‘³ç€æ¸…å• 7 ä¸çš„ Animal
的定义是一¿Uæœ‰æ•ˆçš„è¯æ³•。第二,JavaScript åŸÞZºŽåŽŸåž‹æˆ–çŽ°æœ‰çš„å¯¹è±¡çš„å®žä¾‹æ¥æž„é€ å¯¹è±¡ï¼Œè€ŒéžåŸÞZºŽ¾cÀL¨¡æÑ€?code>funct() 是一¿U调用,ä½?new Animal()
å´åŸºäº?Animal
å†…çš„åŽŸåž‹æž„é€ ä¸€ä¸ªå¯¹è±¡ã€‚æœ€åŽï¼Œåœ?JavaScript ä¸ï¼Œå¯¹è±¡åªæ˜¯å‡½æ•°å’Œå˜é‡çš„集åˆã€‚æ¯ä¸ªå¯¹è±¡åƈä¸ä¸Ž¾cÕdž‹ç›¸å…³åQŒæ‰€ä»¥å¯ä»¥è‡ªç”±åœ°ä¿®æ”¹˜q™ç§¾l“æž„ã€?
回到 æ¸…å• 7。如您所è§ï¼ŒJavaScript åŸÞZºŽåœ?Animal
䏿Œ‡å®šçš„原型定义一个新对象åQ?code>myAnimal。ç‘ô而å¯ä»¥ä‹É用原型ä¸çš„属性和函数åQŒç”šæˆ–é‡å®šä¹‰å‡½æ•°å’Œå±žæ€§ã€‚è¿™¿U絋zÀL€§å¯èƒ½ä¼šè®?Java å¼€å‘äh员å—ä¸äº†åQŒå› ä¸ÞZ»–们ä¸ä¹ 惯˜q™ç§è¡ŒäØ“åQŒä½†å®ƒçš„¼‹®æ˜¯ä¸€¿Uå分强大的模型ã€?/p>
çŽ°åœ¨æˆ‘è¿˜è¦æ›´æ·±å…¥ä¸€æ¥ã€‚您˜q˜å¯ä»¥ä‹É用åä¸?prototype
实例å˜é‡æ¥æŒ‡å®šå¯¹è±¡çš„基础。方法是讄¡½® prototype
实例å˜é‡ä½¿å…¶æŒ‡å‘¾l§æ‰¿é“„¡š„父。如æ¤è®¾¾|?prototype
之åŽåQŒæ‚¨æ‰€åˆ›å¾çš„对象会为未指定的那些对象ç‘ôæ‰¿å±žæ€§å’Œå‡½æ•°ã€‚è¿™æ ·ä¸€æ¥ï¼Œæ‚¨å°±å¯ä»¥æ¨¡ä»¿é¢å‘对象的ç‘ô承概å¿üc€‚ä»¥æ¸…å• 8 ä¸ÞZ¾‹åQ?
<script type='text/javascript'> Animal = function() { this.name = "nobody" this.speak = function () { return "Who am I?" } } Dog = function() { this.speak = function() { return "Woof!" } } Dog.prototype = new Animal(); myAnimal = new Dog(); alert('The animal named ' + myAnimal.name + ' says ' + myAnimal.speak()); </script> |
在清å?8 ä¸ï¼Œåˆ›å¾äº†ä¸€ä¸?Dog
原型。æ¤åŽŸåž‹åŸÞZºŽ Animal
ã€?code>Dog é‡å®šä¹?speak()
æ–ÒŽ³•但å´ä¸ä¼šå¯?name()
æ–ÒŽ³•åšä“Q何改动。éšåŽï¼Œž®†åŽŸåž?Dog
讄¡½®æˆ?Animal
。图 7 昄¡¤ºäº†å…¶¾l“æžœåQ?/p>
å›?7. 通过原型¾l§æ‰¿
˜q™ä¹Ÿå±•示äº?JavaScript 是如何解军_ˆ°å±žæ€§æˆ–æ–ÒŽ³•的引用问题的åQ?/p>
prototype
的实例å˜é‡ï¼ŒJavaScript ž®×ƒ¼šåœ¨æ¤å®žä¾‹ä¸å¯»æ‰¾ä“Q何未定义的实例å˜é‡æˆ–属性。这¿U查找是递归的:如果 åœ?prototype
内定义的实例ä¸èƒ½æ‰‘Öˆ°å±žæ€§æˆ–函数åQŒå®ƒž®×ƒ¼šåœ?i>å…?/i> åŽŸåž‹ä¸æŸ¥æ‰¾ï¼Œä¾æ¤¾cÀLލã€?那么åQŒJavaScript çš„ç‘ôæ‰¿æ¨¡åž‹åˆ°åº•æ˜¯ä»€ä¹ˆæ ·çš„ï¼Ÿ˜q™å–决于您如何对它进行定义。您需è¦å®šä¹‰ç‘ô承行ä¸ÞZ»¥ä¾¿å¯ä»¥è¦†ç›–它。然而,从本质上è®ÔŒ¼ŒJavaScript æ›´åƒæ˜¯ä¸€¿U函数å¼è¯è¨€åQŒè€Œéžé¢å‘对象的è¯a€åQŒå®ƒä½¿ç”¨ä¸€äº›æ™ºèƒ½çš„è¯æ³•å’Œè¯ä¹‰æ¥ä»¿çœŸé«˜åº¦å¤æ‚的行为。其对象模型æžäØ“ç‰|´»ã€å¼€æ”‘Ö’Œå¼ºå¤§åQŒå…·æœ‰å…¨éƒ¨çš„å射性。有些ähå¯èƒ½ä¼šè¯´å®ƒå¤ª˜q‡çµ‹z…R€‚è€Œæˆ‘çš„å¿ å‘Šåˆ™æ˜¯ï¼ŒæŒ‰å…·ä½“ä½œä¸šçš„éœ€è¦é€‰æ‹©åˆé€‚的工具ã€?/p>
![]() ![]() |
![]()
|
JavaScript 对象模型构å¾åœ¨è¯¥è¯è¨€çš„å…¶ä»–åŠŸèƒ½ä¹‹ä¸Šæ¥æ”¯æŒå¤§é‡çš„库åQŒæ¯”å¦?DojoåQˆå‚è§?å‚考资æ–?/font>åQ‰ã€‚è¿™¿U絋zÀL€§è®©æ¯ä¸ªæ¡†æž¶èƒ½å¤Ÿä»¥ä¸€¿Uç²¾¾l†çš„æ–¹å¼æ›´æ”¹å¯¹è±¡æ¨¡åž‹ã€‚在æŸç§½E‹åº¦ä¸Šï¼Œ˜q™ç§ç‰|´»æ€§æ˜¯ä¸€¿Uæžå¤§çš„¾~ºç‚¹ã€‚它å¯ä»¥å¯ÆD‡´å¯æ€•的互æ“作性问题(ž®½ç®¡è¯¥è¯a€çš„絋zÀL€§å¯ä»¥éƒ¨åˆ†ç¼“解这些问题)ã€?
而å¦ä¸€æ–šw¢åQŒçµ‹zÀL€§åˆæ˜¯ä¸€¿U巨大的优势。Java è¯è¨€ä¸€ç›´è‹¦äºŽæ— 法充分增强其ç‰|´»æ€§ï¼ŒåŽŸå› æ˜¯å®ƒçš„åŸºæœ¬å¯¹è±¡æ¨¡åž‹è¿˜æœªçµ‹zÕdˆ°å¯ä»¥è¢«æ‰©å±•çš„½E‹åº¦ã€‚一个典型的ä¼ä¸š¾U§å¼€å‘ähå‘˜äØ“èƒ½å¤ŸæˆåŠŸä½¿ç”¨ Java è¯è¨€å¿…é¡»è¦å¦ä¹ 很多东西,而新出现的一些优¿U€çš„开放æºç 项目和新技术,比如é¢å‘æ–šw¢¾~–程ã€Spring ¾~–程框架和å—节ç 增强库,则带æ¥äº†å¤§é‡è¦å¦çš„代ç ã€?
最åŽï¼ŒJavaScript 优秀的絋zÀL€§çš„¼‹®è®©æ‚¨ä½“会到了一些高阶è¯a€çš„å¼ºå¤§åŠŸèƒ½ã€‚å½“ç„¶æ‚¨æ— éœ€é€‰æ‹©ä¸ºæ¯ä¸ªé¡¹ç›®æˆ–大多数项目都åšè¿™æ ïLš„æƒè¡¡å’ŒæŠ˜è¡—÷€‚但了解一¿Uè¯a€çš„优势和劣势 —â€?通过å‚考大é‡ä¿¡æ¯ï¼Œè€Œä¸ä»…ä»…åŸÞZºŽòq¿å‘Šå®£ä¼ 或公众æ„è§?—â€?会让您å¯ä»¥æ›´å¥½åœ°æŽ§åˆ¶ä½•时需è¦ä‹É用以åŠä½•æ—¶ä¸èƒ½ä‹É用这¿Uè¯a€ã€‚当您在修改 JavaScript Web ž®éƒ¨ä»¶æ—¶åQŒæ‚¨è‡›_°‘知é“该如何让æ¤è¯a€å‘挥它最大的优势。请¾l§ç®‹è·¨è¶Šè¾¹ç•Œå§ã€?/p>
OPEN c1
FETCH NEXT FROM c1 INTO @e--ä»Žæ¸¸æ ‡ä¸å–出数æ®
WHILE @@fetch_status = 0--åˆ¤æ–æ˜¯å¦åˆ°æœ€å?br />BEGIN
UPDATE emp_mgr
SET emp_mgr.NoOfReports = emp_mgr.NoOfReports + 1 -- Add 1 for newly
WHERE emp_mgr.emp = @e -- added employee.
FETCH NEXT FROM c1 INTO @e
END
CLOSE c1
DEALLOCATE c1--åˆ é™¤æ¸¸æ ‡å¼•ç”¨
åQˆè¯¥æ–‡è{载自MatrixåQŒåŽŸæ–‡åœ°å€åQ?a >http://www.matrix.org.cn/resource/article/44/44430_software+test.htmlåQ?/p>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=791907
<?xml version="1.0" encoding="UTF-8"?>
<Customers>
    <customer>
            <id>1</id>
            <gender>female</gender>
            <firstname>Jessica</firstname>
            <lastname>Lim</lastname>
            <phoneNumber>1234567</phoneNumber>
            <address>
                <primaryAddress>
                        <postalCode>350106</postalCode>
                        <addressLine1>#25-1</addressLine1>
                        <addressLine2>SHINSAYAMA 2-CHOME</addressLine2>
                </primaryAddress>
                <billingAddress>
                        <receiver>Ms Danielle</receiver>
                        <postalCode>350107</postalCode>
                        <addressLine1>#167</addressLine1>
                        <addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>
                </billingAddress>
            </address>
    </customer>
    <customer>
            <id>2</id>
            <gender>male</gender>
            <firstname>David</firstname>
            <lastname>Bill</lastname>
            <phoneNumber>808182</phoneNumber>
            <address>
                <primaryAddress>
                        <postalCode>319087</postalCode>
                        <addressLine1>1033 WS St.</addressLine1>
                        <addressLine2>Tima Road</addressLine2>
                </primaryAddress>
                <billingAddress>
                        <receiver>Mr William</receiver>
                        <postalCode>672993</postalCode>
                        <addressLine1>1033 WS St.</addressLine1>
                        <addressLine2>Tima Road</addressLine2>
                </billingAddress>
            </address>
    </customer>
</Customers>
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">
  <xb:namespace>
    <xb:package>sample.xmlbean</xb:package>
  </xb:namespace>
</xb:config>
      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  elementFormDefault="qualified">
        <xs:element name="Customers">
          <xs:complexType>
            <xs:sequence>
              <xs:element maxOccurs="unbounded" name="customer"
                          type="customerType"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      <xs:complexType name="customerType">
            <xs:sequence>
              <xs:element name="id" type="xs:int"/>
              <xs:element name="gender" type="xs:string"/>
              <xs:element name="firstname" type="xs:string"/>
              <xs:element name="lastname" type="xs:string"/>
              <xs:element name="phoneNumber" type="xs:string"/>
              <xs:element name="address" type="addressType"/>
            </xs:sequence>
      </xs:complexType>
        <xs:complexType name="addressType">
            <xs:sequence>
              <xs:element name="primaryAddress" type="primaryAddressType"/>
              <xs:element name="billingAddress" type="billingAddressType"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="primaryAddressType">
            <xs:sequence>
              <xs:element name="postalCode" type="xs:string"/>
              <xs:element name="addressLine1" type="xs:string"/>
              <xs:element name="addressLine2" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="billingAddressType">
            <xs:sequence>
                  <xs:element name="receiver" type="xs:string"/>
              <xs:element name="postalCode" type="xs:string"/>
              <xs:element name="addressLine1" type="xs:string"/>
              <xs:element name="addressLine2" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
      </xs:schema>
     Â
      scomp [options] [dirs]* [schemaFile.xsd]* [service.wsdl]* [config.xsdconfig]*
     Â
      scomp -src build\src  -out build\customerXmlBean.jar schema\customer.xsd
            -compiler C:\jdk142_04\bin\javac customer.xsdconfig
     Â
          CustomersDocument.java    -- 整个XML文档的Java Classæ˜ å°„
      CustomerType.java              -- 节点sustomerçš„æ˜ ž®?br />      AddressType.java                -- 节点addressçš„æ˜ ž®?br />      BillingAddressType.java        -- 节点billingAddressçš„æ˜ ž®?br />      PrimaryAddressType.java      -- 节点primaryAddressçš„æ˜ ž®?br />   Â
    package com.sample.reader;
    import java.io.File;
   Â
    import sample.xmlbean.*;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.xmlbeans.XmlOptions;
    public class CustomerXMLBean {
    private String filename = null;
   Â
    public CustomerXMLBean(String filename) {
            super();
            this.filename = filename;
    }
    public void customerReader() {
            try {
              File xmlFile = new File(filename);
              CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
              CustomerType[] customers = doc.getCustomers().getCustomerArray();
         Â
              for (int i = 0; i < customers.length; i++) {
                CustomerType customer = customers[i];
                println("Customer#" + i);
                println("Customer ID:" + customer.getId());
                println("First name:" + customer.getFirstname());
                println("Last name:" + customer.getLastname());
                println("Gender:" + customer.getGender());
                println("PhoneNumber:" + customer.getPhoneNumber());
                // Primary address
                PrimaryAddressType primaryAddress = customer.getAddress().getPrimaryAddress();
                println("PrimaryAddress:");
                println("PostalCode:" + primaryAddress.getPostalCode());
                println("AddressLine1:" + primaryAddress.getAddressLine1());
                println("AddressLine2:" + primaryAddress.getAddressLine2());
                // Billing address
                BillingAddressType billingAddress = customer.getAddress().getBillingAddress();
                println("BillingAddress:");
                println("Receiver:" + billingAddress.getReceiver());
                println("PostalCode:" + billingAddress.getPostalCode());
                println("AddressLine1:" + billingAddress.getAddressLine1());
                println("AddressLine2:" + billingAddress.getAddressLine2());
           Â
              }
            } catch (Exception ex) {
                    ex.printStackTrace();
            }
    }
    private void println(String str) {
          System.out.println(str);
    }
  public static void main(String[] args) {
      String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers.xml";
                 Â
    CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
                  customerXMLBean.customerReader();
    }
    }
   Â
      Customer#0
      Customer ID:1
      First name:Jessica
      Last name:Lim
      Gender:female
      PhoneNumber:1234567
      PrimaryAddress:
      PostalCode:350106
      AddressLine1:#25-1
      AddressLine2:SHINSAYAMA 2-CHOME
      BillingAddress:
      Receiver:Ms Danielle
      PostalCode:350107
      AddressLine1:#167
      AddressLine2:NORTH TOWER HARBOUR CITY
      Customer#1
      Customer ID:2
      First name:David
      Last name:Bill
      Gender:male
      PhoneNumber:808182
      PrimaryAddress:
      PostalCode:319087
      AddressLine1:1033 WS St.
      AddressLine2:Tima Road
      BillingAddress:
      Receiver:Mr William
      PostalCode:672993
      AddressLine1:1033 WS St.
      AddressLine2:Tima Road
   Â
    public void createCustomer() {
    try {
        // Create Document
        CustomersDocument doc = CustomersDocument.Factory.newInstance();
        // Add new customer
        CustomerType customer = doc.addNewCustomers().addNewCustomer();
        // set customer info
        customer.setId(3);
        customer.setFirstname("Jessica");
        customer.setLastname("Lim");
        customer.setGender("female");
        customer.setPhoneNumber("1234567");
        // Add new address
        AddressType address = customer.addNewAddress();
        // Add new PrimaryAddress
        PrimaryAddressType primaryAddress = address.addNewPrimaryAddress();
        primaryAddress.setPostalCode("350106");
        primaryAddress.setAddressLine1("#25-1");
        primaryAddress.setAddressLine2("SHINSAYAMA 2-CHOME");
        // Add new BillingAddress
        BillingAddressType billingAddress = address.addNewBillingAddress();
        billingAddress.setReceiver("Ms Danielle");
        billingAddress.setPostalCode("350107");
        billingAddress.setAddressLine1("#167");
        billingAddress.setAddressLine2("NORTH TOWER HARBOUR CITY");
        File xmlFile = new File(filename);
        doc.save(xmlFile);
        } catch (Exception ex) {
                ex.printStackTrace();
        }
  }
   Â
    public static void main(String[] args) {
    String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
        CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
        customerXMLBean.createCustomer();
    }
   Â
    <?xml version="1.0" encoding="UTF-8"?>
    <Customers>
    <customer>
            <id>3</id>
            <gender>female</gender>
            <firstname>Jessica</firstname>
            <lastname>Lim</lastname>
            <phoneNumber>1234567</phoneNumber>
            <address>
                    <primaryAddress>
                        <postalCode>350106</postalCode>
                        <addressLine1>#25-1</addressLine1>
                                      <addressLine2>SHINSAYAMA 2-CHOME</addressLine2>
                    </primaryAddress>
                    <billingAddress>
                        <receiver>Ms Danielle</receiver>
                        <postalCode>350107</postalCode>
                      <addressLine1>#167</addressLine1>
                      <addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>
                    </billingAddress>
                    </address>
            </customer>
    </Customers>
   Â
      public void updateCustomer(int id,String lastname) {
        try {
        File xmlFile = new File(filename);
        CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
        CustomerType[] customers = doc.getCustomers().getCustomerArray();
     Â
        for (int i = 0; i < customers.length; i++) {
          CustomerType customer = customers[i];
          if(customer.getId()==id){
                customer.setLastname(lastname);
                break;
            }
        }
        doc.save(xmlFile);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
          }
   Â
    public static void main(String[] args) {
    String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
                   Â
    CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
                   Â
    customerXMLBean.updateCustomer(3,"last");
    }
   Â
    public void deleteCustomer(int id) {
    try {
      File xmlFile = new File(filename);
    CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
    CustomerType[] customers = doc.getCustomers().getCustomerArray();
  for (int i = 0; i < customers.length; i++) {
        CustomerType customer = customers[i];
        if(customer.getId()==id){
                        customer.setNil() ;
                        break;
              }
  }
  doc.save(xmlFile);
  } catch (Exception ex) {
        ex.printStackTrace();
        }
  }
    public static void main(String[] args) {
    String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
                   Â
    CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
                   Â
    customerXMLBean.deleteCustomer(3);
    }