锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
铏界劧緗戜笂鍟嗗簵褰㈠紡澶氭牱,姣忎釜绔欑偣鏈夎嚜宸辯殑鐗硅壊,浣嗕篃鏈夊叾涓鑸殑鍏辨?鍗曞氨"鍟嗗搧鐨勫彉鍖?浠ヤ究鍙婃椂閫氱煡璁㈡埛"榪欎竴鐐?鏄緢澶氱綉涓婂晢搴楀叡鏈夌殑妯″紡,榪欎竴妯″紡綾諱技Observer patern.
鍏蜂綋鐨勮,濡傛灉緗戜笂鍟嗗簵涓晢鍝佸湪鍚嶇О 浠鋒牸絳夋柟闈㈡湁鍙樺寲,濡傛灉緋葷粺鑳借嚜鍔ㄩ氱煡浼氬憳,灝嗘槸緗戜笂鍟嗗簵鍖哄埆浼犵粺鍟嗗簵鐨勪竴澶х壒鑹?榪欏氨闇瑕佸湪鍟嗗搧product涓姞鍏bserver榪欐牱瑙掕壊,浠ヤ究product緇嗚妭鍙戠敓鍙樺寲鏃?Observer鑳借嚜鍔ㄨ瀵熷埌榪欑鍙樺寲,騫惰兘榪涜鍙婃椂鐨剈pdate鎴杗otify鍔ㄤ綔.
Java鐨凙PI榪樹負涓烘垜浠彁渚涚幇鎴愮殑Observer鎺ュ彛Java.util.Observer.鎴戜滑鍙鐩存帴浣跨敤瀹冨氨鍙互.
鎴戜滑蹇呴』extends Java.util.Observer鎵嶈兘鐪熸浣跨敤瀹?
1.鎻愪緵Add/Delete observer鐨勬柟娉?
2.鎻愪緵閫氱煡(notisfy) 鎵鏈塷bserver鐨勬柟娉?
//浜у搧綾?鍙緵Jsp鐩存帴浣跨敤UseBean璋冪敤 璇ョ被涓昏鎵ц浜у搧鏁版嵁搴撴彃鍏?鏇存柊 銆銆private String name; 銆銆public String getName(){ return name;} 銆銆}銆銆銆 銆銆public float getPrice(){ return price;} 銆銆}
} |
鎴戜滑娉ㄦ剰鍒?鍦╬roduct綾諱腑 鐨剆etXXX鏂規硶涓?鎴戜滑璁劇疆浜?notify(閫氱煡)鏂規硶, 褰揓sp琛ㄥ崟璋冪敤setXXX(濡備綍璋冪敤瑙佹垜鐨?a target="_blank">鍙﹀涓綃囨枃绔?/font>),瀹為檯涓婂氨瑙﹀彂浜唍otisfyObservers鏂規硶,榪欏皢閫氱煡鐩稿簲瑙傚療鑰呭簲璇ラ噰鍙栬鍔ㄤ簡.
涓嬮潰鐪嬬湅榪欎簺瑙傚療鑰呯殑浠g爜,浠栦滑絀剁珶閲囧彇浜嗕粈涔堣鍔?
//瑙傚療鑰匩ameObserver涓昏鐢ㄦ潵瀵逛駭鍝佸悕縐?name)榪涜瑙傚療鐨?br />public class NameObserver implements Observer{ 銆銆private String name=null; 銆銆public void update(Observable obj,Object arg){ 銆銆銆銆 name=(String)arg; 銆銆銆銆} 銆銆}
}
//瑙傚療鑰匬riceObserver涓昏鐢ㄦ潵瀵逛駭鍝佷環鏍?price)榪涜瑙傚療鐨?br />public class PriceObserver implements Observer{ 銆銆private float price=0; 銆銆public void update(Observable obj,Object arg){ 銆銆銆銆 price=((Float)arg).floatValue(); 銆銆銆銆} 銆銆}
} |
Jsp涓垜浠彲浠ユ潵姝e紡鎵ц榪欐瑙傚療鑰呯▼搴?
<jsp:useBean id="product" scope="session" class="Product" />
<jsp:useBean id="nameobs" scope="session" class="NameObserver" />
<jsp:useBean id="priceobs" scope="session" class="PriceObserver" />
<%
}else{
%> 銆銆//request.getRequestURI()鏄駭鐢熸湰jsp鐨勭▼搴忓悕,灝辨槸鑷繁璋冪敤鑷繁 銆銆<input type=hidden name="save" value="1"> 銆銆</form>
<%
%>
聽
聽
聽 |
鎵ц鏀?span lang="EN-US">Jsp紼嬪簭,浼氬嚭鐜頒竴涓〃鍗曞綍鍏ョ晫闈? 闇瑕佽緭鍏ヤ駭鍝佸悕縐?浜у搧浠鋒牸, 鐐規寜Submit鍚?榪樻槸鎵ц璇sp鐨?br />if (request.getParameter("save")!=null)涔嬮棿鐨勪唬鐮?
鐢變簬榪欓噷浣跨敤浜嗘暟鎹甹avabeans鐨勮嚜鍔ㄨ祴鍊兼蹇?瀹為檯紼嬪簭鑷姩鎵ц浜唖etName setPrice璇彞.浣犱細鍦ㄦ湇鍔″櫒鎺у埗鍙頒腑鍙戠幇涓嬮潰淇℃伅::
NameObserver :name changet to ?????(Jsp琛ㄥ崟涓緭鍏ョ殑浜у搧鍚嶇О)
PriceObserver :price changet to ???(Jsp琛ㄥ崟涓緭鍏ョ殑浜у搧浠鋒牸);
榪欒鏄庤瀵熻呭凡緇忓湪琛屽姩浜?span lang="EN-US">.!!
鍚屾椂浣犱細鍦ㄦ墽琛宩sp鐨勬祻瑙堝櫒绔緱鍒頒俊鎭?
浜у搧鏁版嵁鍙樺姩 淇濆瓨! 騫跺凡緇忚嚜鍔ㄩ氱煡瀹㈡埛
涓婃枃鐢變簬浣跨敤jsp姒傚康,闅愬惈寰堝鑷姩鍔ㄤ綔,鐜板皢璋冪敤瑙傚療鑰呯殑Java浠g爜鍐欏涓?
聽
public class Test { 銆銆public static void main(String args[]){
Product product=new Product();
//鍔犲叆瑙傚療鑰?br />product.addObserver(nameobs);
product.setName("姍樺瓙綰簡"); 銆銆}
}
聽
聽
聽 |
浣犱細鍦ㄥ彂鐜頒笅闈俊鎭?span lang="EN-US">::
NameObserver :name changet to 姍樺瓙綰簡
PriceObserver :price changet to 9.22
榪欒鏄庤瀵熻呭湪琛屽姩浜?span lang="EN-US">.!!
鏉ユ簮 錛?http://book.javanb.com/java-design-patterns/Observer.html
Composite姣旇緝瀹規槗鐞嗚В錛屾兂鍒癈omposite灝卞簲璇ユ兂鍒版爲褰㈢粨鏋勫浘銆傜粍鍚堜綋鍐呰繖浜涘璞¢兘鏈夊叡鍚屾帴鍙?褰撶粍鍚堜綋涓涓璞$殑鏂規硶琚皟鐢ㄦ墽琛屾椂錛孋omposite灝嗛亶鍘?Iterator)鏁翠釜鏍戝艦緇撴瀯,瀵繪壘鍚屾牱鍖呭惈榪欎釜鏂規硶鐨勫璞″茍瀹炵幇璋冪敤鎵ц銆傚彲浠ョ敤鐗典竴鍔ㄧ櫨鏉ュ艦瀹廣?o:p>
鎵浠?span lang="EN-US">Composite妯″紡浣跨敤鍒癐terator妯″紡錛屽拰Chain of Responsibility妯″紡綾諱技銆?o:p>
Composite濂藉
:
1.浣垮鎴風璋冪敤綆鍗曪紝瀹㈡埛绔彲浠ヤ竴鑷寸殑浣跨敤緇勫悎緇撴瀯鎴栧叾涓崟涓璞★紝鐢ㄦ埛灝變笉蹇呭叧緋昏嚜宸卞鐞嗙殑鏄崟涓璞¤繕鏄暣涓粍鍚堢粨鏋勶紝榪欏氨綆鍖栦簡瀹㈡埛绔唬鐮併?br />2.鏇村鏄撳湪緇勫悎浣撳唴鍔犲叆瀵硅薄閮ㄤ歡. 瀹㈡埛绔笉蹇呭洜涓哄姞鍏ヤ簡鏂扮殑瀵硅薄閮ㄤ歡鑰屾洿鏀逛唬鐮併?o:p>
濡備綍浣跨敤Composite?
棣栧厛瀹氫箟涓涓帴鍙f垨鎶借薄綾伙紝榪欐槸璁捐妯″紡閫氱敤鏂瑰紡浜嗭紝鍏朵粬璁捐妯″紡瀵規帴鍙e唴閮ㄥ畾涔夐檺鍒朵笉澶氾紝Composite鍗存湁涓瀹氾紝閭e氨鏄鍦ㄦ帴鍙e唴閮ㄥ畾涔変竴涓敤浜庤闂拰綆$悊Composite緇勫悎浣撶殑瀵硅薄浠紙鎴栫О閮ㄤ歡Component錛?
涓嬮潰鐨勪唬鐮佹槸浠ユ娊璞$被瀹氫箟錛屼竴鑸敖閲忕敤鎺ュ彛interface,
public abstract class Equipment |
鎶借薄綾?span lang="EN-US">Equipment灝辨槸Component瀹氫箟錛屼唬琛ㄧ潃緇勫悎浣撶被鐨勫璞′滑,Equipment涓畾涔夊嚑涓叡鍚岀殑鏂規硶銆?o:p>
public class Disk extends Equipment |
Disk鏄粍鍚堜綋鍐呯殑涓涓璞★紝鎴栫О涓涓儴浠訛紝榪欎釜閮ㄤ歡鏄釜鍗曠嫭鍏冪礌( Primitive)銆?br />榪樻湁涓縐嶅彲鑳芥槸錛屼竴涓儴浠朵篃鏄竴涓粍鍚堜綋錛屽氨鏄榪欎釜閮ㄤ歡涓嬮潰榪樻湁'鍎垮瓙'錛岃繖鏄爲褰㈢粨鏋勪腑閫氬父鐨勬儏鍐碉紝搴旇姣旇緝瀹規槗鐞嗚В銆傜幇鍦ㄦ垜浠厛瑕佸畾涔夎繖涓粍鍚堜綋錛?o:p>
abstract class CompositeEquipment extends Equipment 銆銆//娉ㄦ剰榪欓噷錛岃繖閲屽氨鎻愪緵鐢ㄤ簬璁塊棶鑷繁緇勫悎浣撳唴鐨勯儴浠舵柟娉曘?br />銆銆//涓婇潰dIsk 涔嬫墍浠ユ病鏈夛紝鏄洜涓篋isk鏄釜鍗曠嫭(Primitive)鐨勫厓绱? |
涓婇潰CompositeEquipment緇ф壙浜咵quipment,鍚屾椂涓鴻嚜宸遍噷闈㈢殑瀵硅薄浠彁渚涗簡澶栭儴璁塊棶鐨勬柟娉?閲嶈澆浜咺terator,Iterator鏄疛ava鐨凜ollection鐨勪竴涓帴鍙o紝鏄疘terator妯″紡鐨勫疄鐜?
鎴戜滑鍐嶇湅鐪?span lang="EN-US">CompositeEquipment鐨勪袱涓叿浣撶被:鐩樼洅Chassis鍜岀瀛怌abinet錛岀瀛愰噷闈㈠彲浠ユ斁寰堝涓滆タ錛屽搴曟澘錛岀數婧愮洅錛岀‖鐩樼洅絳夛紱鐩樼洅閲岄潰鍙互鏀句竴浜涘皬璁懼錛屽紜洏 杞┍絳夈傛棤鐤戣繖涓や釜閮芥槸灞炰簬緇勫悎浣撴ц川鐨勩?o:p>
public class Chassis extends CompositeEquipment
public class Cabinet extends CompositeEquipment |
鑷蟲鎴戜滑瀹屾垚浜嗘暣涓?span lang="EN-US">Composite妯″紡鐨勬灦鏋勩?o:p>
鎴戜滑鍙互鐪嬬湅瀹㈡埛绔皟鐢?span lang="EN-US">Composote浠g爜:
Cabinet cabinet=new Cabinet("Tower");
Chassis chassis=new Chassis("PC Chassis");
//灝哖C Chassis瑁呭埌Tower涓?(灝嗙洏鐩掕鍒扮瀛愰噷)
cabinet.add(chassis);
//灝嗕竴涓?0GB鐨勭‖鐩樿鍒?PC Chassis (灝嗙‖鐩樿鍒扮洏鐩掗噷)
chassis.add(new Disk("10 GB"));
//璋冪敤 netPrice()鏂規硶;
System.out.println("netPrice="+cabinet.netPrice());
System.out.println("discountPrice="+cabinet.discountPrice());
涓婇潰璋冪敤鐨勬柟娉?span lang="EN-US">netPrice()鎴杁iscountPrice()錛屽疄闄呬笂Composite浣跨敤Iterator閬嶅巻浜嗘暣涓爲褰㈢粨鏋?瀵繪壘鍚屾牱鍖呭惈榪欎釜鏂規硶鐨勫璞″茍瀹炵幇璋冪敤鎵ц.
Composite鏄釜寰堝閥濡欎綋鐜版櫤鎱х殑妯″紡錛屽湪瀹為檯搴旂敤涓紝濡傛灉紕板埌鏍戝艦緇撴瀯錛屾垜浠氨鍙互灝濊瘯鏄惁鍙互浣跨敤榪欎釜妯″紡銆?o:p>
浠ヨ鍧涗負渚嬶紝涓涓増(forum)涓湁寰堝甯栧瓙(message),榪欎簺甯栧瓙鏈夊師濮嬭創錛屾湁瀵瑰師濮嬭創鐨勫洖搴旇創錛屾槸涓吀鍨嬬殑鏍戝艦緇撴瀯錛岄偅涔堝綋鐒跺彲浠ヤ嬌鐢–omposite妯″紡錛岄偅涔堟垜浠繘鍏ive涓湅鐪嬶紝鏄浣曞疄鐜扮殑.
Jive瑙e墫
鍦↗ive涓?ForumThread鏄疐orumMessages鐨勫鍣╟ontainer(緇勫悎浣?.涔熷氨鏄錛孎orumThread綾諱技鎴戜滑涓婁緥涓殑 CompositeEquipment.瀹冨拰messages鐨勫叧緋誨鍥撅細
[thread]
銆銆 |- [message]
銆銆 |- [message]
銆銆 銆銆 |- [message]
銆銆 銆銆 |- [message]
銆銆 銆銆 銆銆 |- [message]
鎴戜滑鍦?span lang="EN-US">ForumThread鐪嬪埌濡備笅浠g爜錛?o:p>
public interface ForumThread { 銆銆 public void deleteMessage(ForumMessage message) 銆銆
} |
綾諱技CompositeEquipment, 鎻愪緵鐢ㄤ簬璁塊棶鑷繁緇勫悎浣撳唴鐨勯儴浠舵柟娉? 澧炲姞 鍒犻櫎 閬嶅巻.
緇撳悎鎴戠殑鍏朵粬妯″紡涓Jive鐨勫垎鏋愶紝鎴戜滑宸茬粡鍩烘湰澶т綋鐞嗚В浜咼ive璁哄潧浣撶郴鐨勬鏋訛紝濡傛灉浣犱箣鍓嶄笉鐞嗚В璁捐妯″紡錛岃岀洿鎺ュ幓鐪婮ive婧愪唬鐮侊紝浣犺偗瀹氭棤娉曠湅鎳傘?br />
:)
鏉ユ簮錛?http://book.javanb.com/java-design-patterns/Composite.html
Path-Specific relationships
The PathProxy solution applies in any situation where an entity can appear as an association of another entity type, but only for a specific path. I refer to such relationships as path specific. E is a child of D, but only for the path A-->B-->C-->D-->E.On another path, D might have no children (A-->B-->Q-->D) or might have a different child or children(A-->B-->X-->D-->Z)
As an example, imagine a development team consisting of a project manager named Johnie and two developers, Robert and Mukunda. On project A, Johnie leads Robert and on Project B, Johnie leads Mukunda. This is a somewhat contrived example, but not an uncommon scenario in the world of corporate structures. In the real world, you might have the efficiency of the same process in different business locations, or the actions taken in response to the same event by different teams
When to use PathProxy?
You have many entities whose interrelationships are complex and require knowledge of other relationships. Creating explicit objects to represent these types of聽 relationships becomes burdensome. This is expecially true if the objects must be persisted, creating a proliferation of database tables.
Consider PathProxy if , Your system design calls for a number of casses whose sole or primary function is to model the relationships between other objects.
Using PathProxy is more complicated than using聽 simple objects to represent relationships, so consider your particular situation. If you have few relationships to store and they are not too complicated, PathProxy may not be the right choice. On the other hand, once you reach a certain level of relationship complexity, using PathProxy will greatly simplify your overall system design. Beijng able to reuse the same machnaism over and over again is also a huge time-saver.
鏉ヨ嚜 : http://www.javaworld.com/javaworld/jw-07-2008/jw-07-pathproxy.html?page=1