杩欎釜鏃跺€欙紝瀵逛簬瀹㈡埛绔潵璇达紝涓嶅啀闇€瑕佸崟鐙寫閫?CPU鍘傚晢銆佷富鏉垮巶鍟嗐€佺‖鐩樺巶鍟嗙瓑锛岀洿鎺ラ€夋嫨涓€瀹跺搧鐗屽伐鍘傦紝鍝佺墝宸ュ巶浼氳礋璐g敓浜ф墍鏈夌殑涓滆タ锛岃€屼笖鑳戒繚璇佽偗瀹氭槸鍏煎鍙敤鐨勩€?/span>
public static void main (String[] args) { // 绗竴姝ュ氨瑕侀€夊畾涓€涓€滃ぇ鍘傗€?/span> ComputerFactory cf = new AmdFactory(); // 浠庤繖涓ぇ鍘傞€?nbsp;CPU CPU cpu = cf.makeCPU(); // 浠庤繖涓ぇ鍘傞€犱富鏉?/span> MainBoard board = cf.makeMainBoard(); // 浠庤繖涓ぇ鍘傞€犵‖鐩?/span> HardDisk hardDisk = cf.makeHardDisk(); // 灏嗗悓涓€涓巶瀛愬嚭鏉ョ殑 CPU銆佷富鏉裤€佺‖鐩樼粍瑁呭湪涓€璧?/span> Computer result = new Computer(cpu, board, hardDisk); }
褰撶劧锛屾娊璞″伐鍘傜殑闂涔熸槸鏄捐€屾槗瑙佺殑锛屾瘮濡傛垜浠鍔犱釜鏄剧ず鍣紝灏遍渶瑕佷慨鏀规墍鏈夌殑宸ュ巶锛岀粰鎵€鏈夌殑宸ュ巶閮藉姞涓婂埗閫犳樉绀哄櫒鐨勬柟娉曘€傝繖鏈夌偣杩濆弽浜?span class="mq-295">瀵逛慨鏀瑰叧闂紝瀵规墿灞曞紑鏀?/strong>杩欎釜璁捐鍘熷垯銆?/span>
鍗曚緥妯″紡
鍗曚緥妯″紡鐢ㄥ緱鏈€澶氾紝閿欏緱鏈€澶氥€?/span>
public class Singleton { // 棣栧厛锛屽皢 new Singleton() 鍫垫 private Singleton () {}; // 鍒涘缓绉佹湁闈欐€佸疄渚嬶紝鎰忓懗鐫€杩欎釜绫荤涓€娆′娇鐢ㄧ殑鏃跺€欏氨浼氳繘琛屽垱寤?/span> private static Singleton instance = new Singleton(); public static Singleton getInstance () { return instance; } // 鐬庡啓涓€涓潤鎬佹柟娉曘€傝繖閲屾兂璇寸殑鏄紝濡傛灉鎴戜滑鍙槸瑕佽皟鐢?Singleton.getDate(...)锛?/span> // 鏈潵鏄笉鎯宠鐢熸垚 Singleton 瀹炰緥鐨勶紝涓嶈繃娌″姙娉曪紝宸茬粡鐢熸垚浜?/span> public static Date getDate (String mode) {return new Date();} }
寰堝浜洪兘鑳借鍑洪タ姹夋ā寮忕殑缂虹偣锛屽彲鏄垜瑙夊緱鐢熶骇杩囩▼涓紝寰堝皯纰板埌杩欑鎯呭喌锛氫綘瀹氫箟浜嗕竴涓崟渚嬬殑绫伙紝涓嶉渶瑕佸叾瀹炰緥锛屽彲鏄綘鍗存妸涓€涓垨鍑犱釜浣犱細鐢ㄥ埌鐨勯潤鎬佹柟娉曞鍒拌繖涓被涓€?/span>
public class Singleton { // 棣栧厛锛屼篃鏄厛鍫垫 new Singleton() 杩欐潯璺?/span> private Singleton () {} // 鍜岄タ姹夋ā寮忕浉姣旓紝杩欒竟涓嶉渶瑕佸厛瀹炰緥鍖栧嚭鏉ワ紝娉ㄦ剰杩欓噷鐨?nbsp;volatile锛屽畠鏄繀椤荤殑 private static volatile Singleton instance = null ; public static Singleton getInstance () { if (instance == null ) { // 鍔犻攣 synchronized (Singleton.class) { // 杩欎竴娆″垽鏂篃鏄繀椤荤殑锛屼笉鐒朵細鏈夊苟鍙戦棶棰?/span> if (instance == null ) { instance = new Singleton(); } } } return instance; } }
鍙岄噸妫€鏌ワ紝鎸囩殑鏄袱娆℃鏌?instance 鏄惁涓?null銆?/span>
volatile 鍦ㄨ繖閲屾槸闇€瑕佺殑锛屽笇鏈涜兘寮曡捣璇昏€呯殑鍏虫敞銆?/span>
寰堝浜轰笉鐭ラ亾鎬庝箞鍐欙紝鐩存帴灏卞湪 getInstance() 鏂规硶绛惧悕涓婂姞涓?synchronized锛岃繖灏变笉澶氳浜嗭紝鎬ц兘澶樊銆?/span>
public class Singleton3 { private Singleton3 () {} // 涓昏鏄娇鐢ㄤ簡 宓屽绫诲彲浠ヨ闂閮ㄧ被鐨勯潤鎬佸睘鎬у拰闈欐€佹柟娉?nbsp;鐨勭壒鎬?/span> private static class Holder { private static Singleton3 instance = new Singleton3(); } public static Singleton3 getInstance () { return Holder.instance; } }
娉ㄦ剰锛屽緢澶氫汉閮戒細鎶婅繖涓?span class="mq-447">宓屽绫?/strong>璇存垚鏄?span class="mq-448">闈欐€佸唴閮ㄧ被锛屼弗鏍煎湴璇达紝鍐呴儴绫诲拰宓屽绫绘槸涓嶄竴鏍风殑锛屽畠浠兘璁块棶鐨勫閮ㄧ被鏉冮檺涔熸槸涓嶄竴鏍风殑銆?/span>
鏈€鍚庯紝鎴戜滑璇翠竴涓嬫灇涓撅紝鏋氫妇寰堢壒娈婏紝瀹冨湪绫诲姞杞界殑鏃跺€欎細鍒濆鍖栭噷闈㈢殑鎵€鏈夌殑瀹炰緥锛岃€屼笖 JVM 淇濊瘉浜嗗畠浠笉浼氬啀琚疄渚嬪寲锛屾墍浠ュ畠澶╃敓灏辨槸鍗曚緥鐨勩€?/span>
铏界劧鎴戜滑骞虫椂寰堝皯鐪嬪埌鐢ㄦ灇涓炬潵瀹炵幇鍗曚緥锛屼絾鏄湪 RxJava 鐨勬簮鐮佷腑锛屾湁寰堝鍦版柟閮界敤浜嗘灇涓炬潵瀹炵幇鍗曚緥銆?/span>
寤洪€犺€呮ā寮?/span>
缁忓父纰拌鐨?XxxBuilder 鐨勭被锛岄€氬父閮芥槸寤洪€犺€呮ā寮忕殑浜х墿銆傚缓閫犺€呮ā寮忓叾瀹炴湁寰堝鐨勫彉绉嶏紝浣嗘槸瀵逛簬瀹㈡埛绔潵璇达紝鎴戜滑鐨勪娇鐢ㄩ€氬父閮芥槸涓€涓ā寮忕殑锛?/span>
Food food = new FoodBuilder().a().b().c().build(); Food food = Food.builder().a().b().c().build();
濂楄矾灏辨槸鍏?new 涓€涓?Builder锛岀劧鍚庡彲浠ラ摼寮忓湴璋冪敤涓€鍫嗘柟娉曪紝鏈€鍚庡啀璋冪敤涓€娆?build() 鏂规硶锛屾垜浠渶瑕佺殑瀵硅薄灏辨湁浜嗐€?/span>
鏉ヤ竴涓腑瑙勪腑鐭╃殑寤洪€犺€呮ā寮忥細
class User { // 涓嬮潰鏄€滀竴鍫嗏€濈殑灞炴€?/span> private String name; private String password; private String nickName; private int age; // 鏋勯€犳柟娉曠鏈夊寲锛屼笉鐒跺鎴风灏变細鐩存帴璋冪敤鏋勯€犳柟娉曚簡 private User (String name, String password, String nickName, int age) { this .name = name; this .password = password; this .nickName = nickName; this .age = age; } // 闈欐€佹柟娉曪紝鐢ㄤ簬鐢熸垚涓€涓?nbsp;Builder锛岃繖涓笉涓€瀹氳鏈夛紝涓嶈繃鍐欒繖涓柟娉曟槸涓€涓緢濂界殑涔犳儻锛?/span> // 鏈変簺浠g爜瑕佹眰鍒汉鍐?nbsp;new User.UserBuilder().a()...build() 鐪嬩笂鍘诲氨娌¢偅涔堝ソ public static UserBuilder builder () { return new UserBuilder(); } public static class UserBuilder { // 涓嬮潰鏄拰 User 涓€妯′竴鏍风殑涓€鍫嗗睘鎬?/span> private String name; private String password; private String nickName; private int age; private UserBuilder () { } // 閾惧紡璋冪敤璁剧疆鍚勪釜灞炴€у€硷紝杩斿洖 this锛屽嵆 UserBuilder public UserBuilder name (String name) { this .name = name; return this ; } public UserBuilder password (String password) { this .password = password; return this ; } public UserBuilder nickName (String nickName) { this .nickName = nickName; return this ; } public UserBuilder age (int age) { this .age = age; return this ; } // build() 鏂规硶璐熻矗灏?UserBuilder 涓缃ソ鐨勫睘鎬р€滃鍒垛€濆埌 User 涓€?/span> // 褰撶劧锛屽彲浠ュ湪 鈥滃鍒垛€?nbsp;涔嬪墠鍋氱偣妫€楠?/span> public User build () { if (name == null || password == null ) { throw new RuntimeException("鐢ㄦ埛鍚嶅拰瀵嗙爜蹇呭~" ); } if (age <= 0 || age >= 150 ) { throw new RuntimeException("骞撮緞涓嶅悎娉? ); } // 杩樺彲浠ュ仛璧嬩簣鈥濋粯璁ゅ€尖€滅殑鍔熻兘 if (nickName == null ) { nickName = name; } return new User(name, password, nickName, age); } } }
鏍稿績鏄細鍏堟妸鎵€鏈夌殑灞炴€ч兘璁剧疆缁?Builder锛岀劧鍚?build() 鏂规硶鐨勬椂鍊欙紝灏嗚繖浜涘睘鎬?span class="mq-634">澶嶅埗缁欏疄闄呬骇鐢熺殑瀵硅薄銆?/span>
鐪嬬湅瀹㈡埛绔殑璋冪敤锛?/span>
public class APP { public static void main (String[] args) { User d = User.builder() .name("foo" ) .password("pAss12345" ) .age(25 ) .build(); } }
璇村疄璇濓紝寤洪€犺€呮ā寮忕殑閾惧紡鍐欐硶寰堝惛寮曚汉锛屼絾鏄紝澶氬啓浜嗗緢澶氣€滄棤鐢ㄢ€濈殑 builder 鐨勪唬鐮侊紝鎰熻杩欎釜妯″紡娌′粈涔堢敤銆備笉杩囷紝褰撳睘鎬у緢澶氾紝鑰屼笖鏈変簺蹇呭~锛屾湁浜涢€夊~鐨勬椂鍊欙紝杩欎釜妯″紡浼氫娇浠g爜娓呮櫚寰堝銆傛垜浠彲浠ュ湪 Builder 鐨勬瀯閫犳柟娉?/strong>涓己鍒惰璋冪敤鑰呮彁渚涘繀濉瓧娈碉紝杩樻湁锛屽湪 build() 鏂规硶涓牎楠屽悇涓弬鏁版瘮鍦?User 鐨勬瀯閫犳柟娉曚腑鏍¢獙锛屼唬鐮佽浼橀泤涓€浜涖€?/span>
棰樺璇濓紝寮虹儓寤鸿璇昏€呬娇鐢?lombok锛岀敤浜?lombok 浠ュ悗锛屼笂闈㈢殑涓€澶у爢浠g爜浼氬彉鎴愬涓嬭繖鏍?
@Builder class User { private String name; private String password; private String nickName; private int age; }
鎬庝箞鏍凤紝鐪佷笅鏉ョ殑鏃堕棿鏄笉鏄張鍙互骞茬偣鍒殑浜嗐€?/span>
褰撶劧锛屽鏋滀綘鍙槸鎯宠閾惧紡鍐欐硶锛屼笉鎯宠寤洪€犺€呮ā寮忥紝鏈変釜寰堢畝鍗曠殑鍔炴硶锛孶ser 鐨?getter 鏂规硶涓嶅彉锛屾墍鏈夌殑 setter 鏂规硶閮借鍏?nbsp;return this 灏卞彲浠ヤ簡锛岀劧鍚庡氨鍙互鍍忎笅闈㈣繖鏍疯皟鐢細
User user = new User().setName("" ).setPassword("" ).setAge(20 );
寰堝浜烘槸杩欎箞鐢ㄧ殑锛屼絾鏄瑪鑰呰寰楀叾瀹炶繖绉嶅啓娉曢潪甯稿湴涓嶄紭闆咃紝涓嶆槸寰堟帹鑽愪娇鐢ㄣ€?/span>
鍘熷瀷妯″紡
杩欐槸鎴戣璇寸殑鍒涘缓鍨嬫ā寮忕殑鏈€鍚庝竴涓璁℃ā寮忎簡銆?/span>
鍘熷瀷妯″紡寰堢畝鍗曪細鏈変竴涓師鍨?span class="mq-708">瀹炰緥锛屽熀浜庤繖涓師鍨嬪疄渚嬩骇鐢熸柊鐨勫疄渚嬶紝涔熷氨鏄€滃厠闅嗏€濅簡銆?/span>
Object 绫讳腑鏈変竴涓?clone() 鏂规硶锛屽畠鐢ㄤ簬鐢熸垚涓€涓柊鐨勫璞★紝褰撶劧锛屽鏋滄垜浠璋冪敤杩欎釜鏂规硶锛宩ava 瑕佹眰鎴戜滑鐨勭被蹇呴』鍏?span class="mq-711">瀹炵幇 Cloneable 鎺ュ彛锛屾鎺ュ彛娌℃湁瀹氫箟浠讳綍鏂规硶锛屼絾鏄笉杩欎箞鍋氱殑璇濓紝鍦?clone() 鐨勬椂鍊欙紝浼氭姏鍑?CloneNotSupportedException 寮傚父銆?/span>
protected native Object clone () throws CloneNotSupportedException ;
java 鐨勫厠闅嗘槸娴呭厠闅嗭紝纰板埌瀵硅薄寮曠敤鐨勬椂鍊欙紝鍏嬮殕鍑烘潵鐨勫璞″拰鍘熷璞′腑鐨勫紩鐢ㄥ皢鎸囧悜鍚屼竴涓璞°€傞€氬父瀹炵幇娣卞厠闅嗙殑鏂规硶鏄皢瀵硅薄杩涜搴忓垪鍖栵紝鐒跺悗鍐嶈繘琛屽弽搴忓垪鍖栥€?/span>
鍘熷瀷妯″紡浜嗚В鍒拌繖閲屾垜瑙夊緱灏卞浜嗭紝鍚勭鍙樼潃娉曞瓙璇磋繖绉嶄唬鐮佹垨閭g浠g爜鏄師鍨嬫ā寮忥紝娌′粈涔堟剰涔夈€?/span>
鍒涘缓鍨嬫ā寮忔€荤粨
鍒涘缓鍨嬫ā寮忔€讳綋涓婃瘮杈冪畝鍗曪紝瀹冧滑鐨勪綔鐢ㄥ氨鏄负浜嗕骇鐢熷疄渚嬪璞★紝绠楁槸鍚勭宸ヤ綔鐨勭涓€姝ヤ簡锛屽洜涓烘垜浠啓鐨勬槸闈㈠悜瀵硅薄鐨勪唬鐮侊紝鎵€浠ユ垜浠涓€姝ュ綋鐒舵槸闇€瑕佸垱寤轰竴涓璞′簡銆?/span>
绠€鍗曞伐鍘傛ā寮忔渶绠€鍗曪紱宸ュ巶妯″紡鍦ㄧ畝鍗曞伐鍘傛ā寮忕殑鍩虹涓婂鍔犱簡閫夋嫨宸ュ巶鐨勭淮搴︼紝闇€瑕佺涓€姝ラ€夋嫨鍚堥€傜殑宸ュ巶锛涙娊璞″伐鍘傛ā寮忔湁浜у搧鏃忕殑姒傚康锛屽鏋滃悇涓骇鍝佹槸瀛樺湪鍏煎鎬ч棶棰樼殑锛屽氨瑕佺敤鎶借薄宸ュ巶妯″紡銆傚崟渚嬫ā寮忓氨涓嶈浜嗭紝涓轰簡淇濊瘉鍏ㄥ眬浣跨敤鐨勬槸鍚屼竴瀵硅薄锛屼竴鏂归潰鏄畨鍏ㄦ€ц€冭檻锛屼竴鏂归潰鏄负浜嗚妭鐪佽祫婧愶紱寤洪€犺€呮ā寮忎笓闂ㄥ浠樺睘鎬у緢澶氱殑閭g绫伙紝涓轰簡璁╀唬鐮佹洿浼樼編锛涘師鍨嬫ā寮忕敤寰楁渶灏戯紝浜嗚В鍜?Object 绫讳腑鐨?clone() 鏂规硶鐩稿叧鐨勭煡璇嗗嵆鍙€?/span>
缁撴瀯鍨嬫ā寮?/strong>
鍓嶉潰鍒涘缓鍨嬫ā寮忎粙缁嶄簡鍒涘缓瀵硅薄鐨勪竴浜涜璁℃ā寮忥紝杩欒妭浠嬬粛鐨勭粨鏋勫瀷妯″紡鏃ㄥ湪閫氳繃鏀瑰彉浠g爜缁撴瀯鏉ヨ揪鍒拌В鑰︾殑鐩殑锛屼娇寰楁垜浠殑浠g爜瀹规槗缁存姢鍜屾墿灞曘€?/span>
浠g悊妯″紡
绗竴涓浠嬬粛鐨勪唬鐞嗘ā寮忔槸鏈€甯镐娇鐢ㄧ殑妯″紡涔嬩竴浜嗭紝鐢ㄤ竴涓唬鐞嗘潵闅愯棌鍏蜂綋瀹炵幇绫荤殑瀹炵幇缁嗚妭锛岄€氬父杩樼敤浜庡湪鐪熷疄鐨勫疄鐜扮殑鍓嶅悗娣诲姞涓€閮ㄥ垎閫昏緫銆?/span>
鏃㈢劧璇存槸浠g悊锛岄偅灏辫瀵瑰鎴风闅愯棌鐪熷疄瀹炵幇锛岀敱浠g悊鏉ヨ礋璐e鎴风鐨勬墍鏈夎姹傘€傚綋鐒讹紝浠g悊鍙槸涓唬鐞嗭紝瀹冧笉浼氬畬鎴愬疄闄呯殑涓氬姟閫昏緫锛岃€屾槸涓€灞傜毊鑰屽凡锛屼絾鏄浜庡鎴风鏉ヨ锛屽畠蹇呴』琛ㄧ幇寰楀氨鏄鎴风闇€瑕佺殑鐪熷疄瀹炵幇銆?/span>
鐞嗚В浠g悊杩欎釜璇嶏紝杩欎釜妯″紡鍏跺疄灏辩畝鍗曚簡銆?/span>
public interface FoodService { Food makeChicken () ; Food makeNoodle () ; }public class FoodServiceImpl implements FoodService { public Food makeChicken () { Food f = new Chicken() f.setChicken("1kg" ); f.setSpicy("1g" ); f.setSalt("3g" ); return f; } public Food makeNoodle () { Food f = new Noodle(); f.setNoodle("500g" ); f.setSalt("5g" ); return f; } }// 浠g悊瑕佽〃鐜板緱鈥滃氨鍍忔槸鈥濈湡瀹炲疄鐜扮被锛屾墍浠ラ渶瑕佸疄鐜?nbsp;FoodService public class FoodServiceProxy implements FoodService { // 鍐呴儴涓€瀹氳鏈変竴涓湡瀹炵殑瀹炵幇绫伙紝褰撶劧涔熷彲浠ラ€氳繃鏋勯€犳柟娉曟敞鍏?/span> private FoodService foodService = new FoodServiceImpl(); public Food makeChicken () { System.out.println("鎴戜滑椹笂瑕佸紑濮嬪埗浣滈浮鑲変簡" ); // 濡傛灉鎴戜滑瀹氫箟杩欏彞涓烘牳蹇冧唬鐮佺殑璇濓紝閭d箞锛屾牳蹇冧唬鐮佹槸鐪熷疄瀹炵幇绫诲仛鐨勶紝 // 浠g悊鍙槸鍦ㄦ牳蹇冧唬鐮佸墠鍚庡仛浜涒€滄棤瓒宠交閲嶁€濈殑浜嬫儏 Food food = foodService.makeChicken(); System.out.println("楦¤倝鍒朵綔瀹屾垚鍟︼紝鍔犵偣鑳℃绮? ); // 澧炲己 food.addCondiment("pepper" ); return food; } public Food makeNoodle () { System.out.println("鍑嗗鍒朵綔鎷夐潰~" ); Food food = foodService.makeNoodle(); System.out.println("鍒朵綔瀹屾垚鍟? ) return food; } }
瀹㈡埛绔皟鐢紝娉ㄦ剰锛屾垜浠鐢ㄤ唬鐞嗘潵瀹炰緥鍖栨帴鍙o細
// 杩欓噷鐢ㄤ唬鐞嗙被鏉ュ疄渚嬪寲 FoodService foodService = new FoodServiceProxy(); foodService.makeChicken();
proxy
鎴戜滑鍙戠幇娌℃湁锛屼唬鐞嗘ā寮忚鐧戒簡灏辨槸鍋?nbsp;鈥滄柟娉曞寘瑁呪€?/strong> 鎴栧仛 鈥滄柟娉曞寮衡€?/strong>銆傚湪闈㈠悜鍒囬潰缂栫▼涓紝鍏跺疄灏辨槸鍔ㄦ€佷唬鐞嗙殑杩囩▼銆傛瘮濡?Spring 涓紝鎴戜滑鑷繁涓嶅畾涔変唬鐞嗙被锛屼絾鏄?Spring 浼氬府鎴戜滑鍔ㄦ€佹潵瀹氫箟浠g悊锛岀劧鍚庢妸鎴戜滑瀹氫箟鍦?@Before銆丂After銆丂Around 涓殑浠g爜閫昏緫鍔ㄦ€佹坊鍔犲埌浠g悊涓€?/span>
璇村埌鍔ㄦ€佷唬鐞嗭紝鍙堝彲浠ュ睍寮€璇达紝Spring 涓疄鐜板姩鎬佷唬鐞嗘湁涓ょ锛屼竴绉嶆槸濡傛灉鎴戜滑鐨勭被瀹氫箟浜嗘帴鍙o紝濡?UserService 鎺ュ彛鍜?UserServiceImpl 瀹炵幇锛岄偅涔堥噰鐢?JDK 鐨勫姩鎬佷唬鐞嗭紝鎰熷叴瓒g殑璇昏€呭彲浠ュ幓鐪嬬湅 java.lang.reflect.Proxy 绫荤殑婧愮爜锛涘彟涓€绉嶆槸鎴戜滑鑷繁娌℃湁瀹氫箟鎺ュ彛鐨勶紝Spring 浼氶噰鐢?CGLIB 杩涜鍔ㄦ€佷唬鐞嗭紝瀹冩槸涓€涓?jar 鍖咃紝鎬ц兘杩樹笉閿欍€?/span>
閫傞厤鍣ㄦā寮?/span>
璇村畬浠g悊妯″紡锛岃閫傞厤鍣ㄦā寮忥紝鏄洜涓哄畠浠緢鐩镐技锛岃繖閲屽彲浠ュ仛涓瘮杈冦€?/span>
閫傞厤鍣ㄦā寮忓仛鐨勫氨鏄紝鏈変竴涓帴鍙i渶瑕佸疄鐜帮紝浣嗘槸鎴戜滑鐜版垚鐨勫璞¢兘涓嶆弧瓒筹紝闇€瑕佸姞涓€灞傞€傞厤鍣ㄦ潵杩涜閫傞厤銆?/span>
閫傞厤鍣ㄦā寮忔€讳綋鏉ヨ鍒嗕笁绉嶏細榛樿閫傞厤鍣ㄦā寮忋€佸璞¢€傞厤鍣ㄦā寮忋€佺被閫傞厤鍣ㄦā寮忋€傚厛涓嶆€ョ潃鍒嗘竻妤氳繖鍑犱釜锛屽厛鐪嬬湅渚嬪瓙鍐嶈銆?/span>
榛樿閫傞厤鍣ㄦā寮?/span>
棣栧厛锛屾垜浠厛鐪嬬湅鏈€绠€鍗曠殑閫傞厤鍣ㄦā寮?span class="mq-891">榛樿閫傞厤鍣ㄦā寮?Default Adapter)鏄€庝箞鏍风殑銆?/span>
鎴戜滑鐢?Appache commons-io 鍖呬腑鐨?FileAlterationListener 鍋氫緥瀛愶紝姝ゆ帴鍙e畾涔変簡寰堝鐨勬柟娉曪紝鐢ㄤ簬瀵规枃浠舵垨鏂囦欢澶硅繘琛岀洃鎺э紝涓€鏃﹀彂鐢熶簡瀵瑰簲鐨勬搷浣滐紝灏变細瑙﹀彂鐩稿簲鐨勬柟娉曘€?/span>
public interface FileAlterationListener { void onStart (final FileAlterationObserver observer) ; void onDirectoryCreate (final File directory) ; void onDirectoryChange (final File directory) ; void onDirectoryDelete (final File directory) ; void onFileCreate (final File file) ; void onFileChange (final File file) ; void onFileDelete (final File file) ; void onStop (final FileAlterationObserver observer) ; }
姝ゆ帴鍙g殑涓€澶ч棶棰樻槸鎶借薄鏂规硶澶浜嗭紝濡傛灉鎴戜滑瑕佺敤杩欎釜鎺ュ彛锛屾剰鍛崇潃鎴戜滑瑕佸疄鐜版瘡涓€涓娊璞℃柟娉曪紝濡傛灉鎴戜滑鍙槸鎯宠鐩戞帶鏂囦欢澶逛腑鐨?span class="mq-952">鏂囦欢鍒涘缓鍜?span class="mq-953">鏂囦欢鍒犻櫎浜嬩欢锛屽彲鏄垜浠繕鏄笉寰椾笉瀹炵幇鎵€鏈夌殑鏂规硶锛屽緢鏄庢樉锛岃繖涓嶆槸鎴戜滑鎯宠鐨勩€?/span>
鎵€浠ワ紝鎴戜滑闇€瑕佷笅闈㈢殑涓€涓?span class="mq-956">閫傞厤鍣?/strong>锛屽畠鐢ㄤ簬瀹炵幇涓婇潰鐨勬帴鍙o紝浣嗘槸鎵€鏈夌殑鏂规硶閮芥槸绌烘柟娉?/strong>锛岃繖鏍凤紝鎴戜滑灏卞彲浠ヨ浆鑰屽畾涔夎嚜宸辩殑绫绘潵缁ф壙涓嬮潰杩欎釜绫诲嵆鍙€?/span>
public class FileAlterationListenerAdaptor implements FileAlterationListener { public void onStart (final FileAlterationObserver observer) { } public void onDirectoryCreate (final File directory) { } public void onDirectoryChange (final File directory) { } public void onDirectoryDelete (final File directory) { } public void onFileCreate (final File file) { } public void onFileChange (final File file) { } public void onFileDelete (final File file) { } public void onStop (final FileAlterationObserver observer) { } }
姣斿鎴戜滑鍙互瀹氫箟浠ヤ笅绫伙紝鎴戜滑浠呬粎闇€瑕佸疄鐜版垜浠兂瀹炵幇鐨勬柟娉曞氨鍙互浜嗭細
public class FileMonitor extends FileAlterationListenerAdaptor { public void onFileCreate (final File file) { // 鏂囦欢鍒涘缓 doSomething(); } public void onFileDelete (final File file) { // 鏂囦欢鍒犻櫎 doSomething(); } }
褰撶劧锛屼笂闈㈣鐨勫彧鏄€傞厤鍣ㄦā寮忕殑鍏朵腑涓€绉嶏紝涔熸槸鏈€绠€鍗曠殑涓€绉嶏紝鏃犻渶澶氳█銆備笅闈紝鍐嶄粙缁?span class="mq-1077">鈥滄缁熺殑鈥?/strong>閫傞厤鍣ㄦā寮忋€?/span>
瀵硅薄閫傞厤鍣ㄦā寮?/span>
鏉ョ湅涓€涓€奌ead First 璁捐妯″紡銆嬩腑鐨勪竴涓緥瀛愶紝鎴戠◢寰慨鏀逛簡涓€涓嬶紝鐪嬬湅鎬庝箞灏嗛浮閫傞厤鎴愰腑锛岃繖鏍烽浮涔熻兘褰撻腑鏉ョ敤銆傚洜涓猴紝鐜板湪楦繖涓帴鍙o紝鎴戜滑娌℃湁鍚堥€傜殑瀹炵幇绫诲彲浠ョ敤锛屾墍浠ラ渶瑕侀€傞厤鍣ㄣ€?/span>
public interface Duck { public void quack () ; // 楦殑鍛卞懕鍙?/span> public void fly () ; // 椋?/span> }public interface Cock { public void gobble () ; // 楦$殑鍜曞挄鍙?/span> public void fly () ; // 椋?/span> }public class WildCock implements Cock { public void gobble () { System.out.println("鍜曞挄鍙? ); } public void fly () { System.out.println("楦′篃浼氶鍝? ); } }
楦帴鍙f湁 fly() 鍜?quare() 涓や釜鏂规硶锛岄浮 Cock 濡傛灉瑕佸啋鍏呴腑锛宖ly() 鏂规硶鏄幇鎴愮殑锛屼絾鏄浮涓嶄細楦殑鍛卞懕鍙紝娌℃湁 quack() 鏂规硶銆傝繖涓椂鍊欏氨闇€瑕侀€傞厤浜嗭細
// 姣棤鐤戦棶锛岄鍏堬紝杩欎釜閫傞厤鍣ㄨ偗瀹氶渶瑕?nbsp;implements Duck锛岃繖鏍锋墠鑳藉綋鍋氶腑鏉ョ敤 public class CockAdapter implements Duck { Cock cock; // 鏋勯€犳柟娉曚腑闇€瑕佷竴涓浮鐨勫疄渚嬶紝姝ょ被灏辨槸灏嗚繖鍙浮閫傞厤鎴愰腑鏉ョ敤 public CockAdapter (Cock cock) { this .cock = cock; } // 瀹炵幇楦殑鍛卞懕鍙柟娉?/span> @Override public void quack () { // 鍐呴儴鍏跺疄鏄竴鍙浮鐨勫挄鍜曞彨 cock.gobble(); } @Override public void fly () { cock.fly(); } }
瀹㈡埛绔皟鐢ㄥ緢绠€鍗曚簡锛?/span>
public static void main (String[] args) { // 鏈変竴鍙噹楦?/span> Cock wildCock = new WildCock(); // 鎴愬姛灏嗛噹楦¢€傞厤鎴愰腑 Duck duck = new CockAdapter(wildCock); ... }
鍒拌繖閲岋紝澶у涔熷氨鐭ラ亾浜嗛€傞厤鍣ㄦā寮忔槸鎬庝箞鍥炰簨浜嗐€傛棤闈炴槸鎴戜滑闇€瑕佷竴鍙腑锛屼絾鏄垜浠彧鏈変竴鍙浮锛岃繖涓椂鍊欏氨闇€瑕佸畾涔変竴涓€傞厤鍣紝鐢辫繖涓€傞厤鍣ㄦ潵鍏呭綋楦紝浣嗘槸閫傞厤鍣ㄩ噷闈㈢殑鏂规硶杩樻槸鐢遍浮鏉ュ疄鐜扮殑銆?/span>
鎴戜滑鐢ㄤ竴涓浘鏉ョ畝鍗曡鏄庝笅锛?/span>
涓婂浘搴旇杩樻槸寰堝鏄撶悊瑙g殑锛屾垜灏变笉鍋氭洿澶氱殑瑙i噴浜嗐€備笅闈紝鎴戜滑鐪嬬湅绫婚€傞厤妯″紡鎬庝箞鏍风殑銆?/span>
绫婚€傞厤鍣ㄦā寮?/span>
adapter-1
鐪嬪埌杩欎釜鍥撅紝澶у搴旇寰堝鏄撶悊瑙g殑鍚э紝閫氳繃缁ф壙鐨勬柟娉曪紝閫傞厤鍣ㄨ嚜鍔ㄨ幏寰椾簡鎵€闇€瑕佺殑澶ч儴鍒嗘柟娉曘€傝繖涓椂鍊欙紝瀹㈡埛绔娇鐢ㄦ洿鍔犵畝鍗曪紝鐩存帴
Target t = new SomeAdapter();
灏卞彲浠ヤ簡銆?/span>
閫傞厤鍣ㄦā寮忔€荤粨
绫婚€傞厤灞炰簬闈欐€佸疄鐜帮紝瀵硅薄閫傞厤灞炰簬缁勫悎鐨勫姩鎬佸疄鐜帮紝瀵硅薄閫傞厤闇€瑕佸瀹炰緥鍖栦竴涓璞°€?/span>
鎬讳綋鏉ヨ锛屽璞¢€傞厤鐢ㄥ緱姣旇緝澶氥€?/span>
閫傞厤鍣ㄦā寮忓拰浠g悊妯″紡鐨勫紓鍚?/span>
姣旇緝杩欎袱绉嶆ā寮忥紝鍏跺疄鏄瘮杈冨璞¢€傞厤鍣ㄦā寮忓拰浠g悊妯″紡锛屽湪浠g爜缁撴瀯涓婏紝瀹冧滑寰堢浉浼硷紝閮介渶瑕佷竴涓叿浣撶殑瀹炵幇绫荤殑瀹炰緥銆備絾鏄畠浠殑鐩殑涓嶄竴鏍凤紝浠g悊妯″紡鍋氱殑鏄寮哄師鏂规硶鐨勬椿锛涢€傞厤鍣ㄥ仛鐨勬槸閫傞厤鐨勬椿锛屼负鐨勬槸鎻愪緵鈥滄妸楦″寘瑁呮垚楦紝鐒跺悗褰撳仛楦潵浣跨敤鈥濓紝鑰岄浮鍜岄腑瀹冧滑涔嬮棿鍘熸湰娌℃湁缁ф壙鍏崇郴銆?/span>
妗ユ妯″紡
鐞嗚В妗ユ妯″紡锛屽叾瀹炲氨鏄悊瑙d唬鐮佹娊璞″拰瑙h€︺€?/span>
鎴戜滑棣栧厛闇€瑕佷竴涓ˉ姊侊紝瀹冩槸涓€涓帴鍙o紝瀹氫箟鎻愪緵鐨勬帴鍙f柟娉曘€?/span>
public interface DrawAPI { public void draw (int radius, int x, int y) ; }
鐒跺悗鏄竴绯诲垪瀹炵幇绫伙細
public class RedPen implements DrawAPI { @Override public void draw (int radius, int x, int y) { System.out.println("鐢ㄧ孩鑹茬瑪鐢诲浘锛宺adius:" + radius + ", x:" + x + ", y:" + y); } }public class GreenPen implements DrawAPI { @Override public void draw (int radius, int x, int y) { System.out.println("鐢ㄧ豢鑹茬瑪鐢诲浘锛宺adius:" + radius + ", x:" + x + ", y:" + y); } }public class BluePen implements DrawAPI { @Override public void draw (int radius, int x, int y) { System.out.println("鐢ㄨ摑鑹茬瑪鐢诲浘锛宺adius:" + radius + ", x:" + x + ", y:" + y); } }
瀹氫箟涓€涓娊璞$被锛屾绫荤殑瀹炵幇绫婚兘闇€瑕佷娇鐢?DrawAPI锛?/span>
public abstract class Shape { protected DrawAPI drawAPI; protected Shape (DrawAPI drawAPI) { this .drawAPI = drawAPI; } public abstract void draw () ; }
瀹氫箟鎶借薄绫荤殑瀛愮被锛?/span>
// 鍦嗗舰 public class Circle extends Shape { private int radius; public Circle (int radius, DrawAPI drawAPI) { super (drawAPI); this .radius = radius; } public void draw () { drawAPI.draw(radius, 0 , 0 ); } }// 闀挎柟褰?/span>public class Rectangle extends Shape { private int x; private int y; public Rectangle (int x, int y, DrawAPI drawAPI) { super (drawAPI); this .x = x; this .y = y; } public void draw () { drawAPI.draw(0 , x, y); } }
鏈€鍚庯紝鎴戜滑鏉ョ湅瀹㈡埛绔紨绀猴細
public static void main (String[] args) { Shape greenCircle = new Circle(10 , new GreenPen()); Shape redRectangle = new Rectangle(4 , 8 , new RedPen()); greenCircle.draw(); redRectangle.draw(); }
鍙兘澶у鐪嬩笂闈竴姝ユ杩樹笉鏄壒鍒竻鏅帮紝鎴戞妸鎵€鏈夌殑涓滆タ鏁村悎鍒颁竴寮犲浘涓婏細
杩欏洖澶у搴旇灏辩煡閬撴娊璞″湪鍝噷锛屾€庝箞瑙h€︿簡鍚с€傛ˉ姊佹ā寮忕殑浼樼偣涔熸槸鏄捐€屾槗瑙佺殑锛屽氨鏄潪甯稿鏄撹繘琛屾墿灞曘€?/span>
鏈妭寮曠敤浜嗚繖閲岀殑渚嬪瓙锛屽苟瀵瑰叾杩涜浜嗕慨鏀广€?/span>
瑁呴グ妯″紡
瑕佹妸瑁呴グ妯″紡璇存竻妤氭槑鐧斤紝涓嶆槸浠跺鏄撶殑浜嬫儏銆備篃璁歌鑰呯煡閬?nbsp;Java IO 涓殑鍑犱釜绫绘槸鍏稿瀷鐨勮楗版ā寮忕殑搴旂敤锛屼絾鏄鑰呬笉涓€瀹氭竻妤氬叾涓殑鍏崇郴锛屼篃璁哥湅瀹屽氨蹇樹簡锛屽笇鏈涚湅瀹岃繖鑺傚悗锛岃鑰呭彲浠ュ鍏舵湁鏇存繁鐨勬劅鎮熴€?/span>
棣栧厛锛屾垜浠厛鐪嬩竴涓畝鍗曠殑鍥撅紝鐪嬭繖涓浘鐨勬椂鍊欙紝浜嗚В涓嬪眰娆$粨鏋勫氨鍙互浜嗭細
鎴戜滑鏉ヨ璇磋楗版ā寮忕殑鍑哄彂鐐癸紝浠庡浘涓彲浠ョ湅鍒帮紝鎺ュ彛
Component
鍏跺疄宸茬粡鏈変簡
ConcreteComponentA
鍜?nbsp;
ConcreteComponentB
涓や釜瀹炵幇绫讳簡锛屼絾鏄紝濡傛灉鎴戜滑瑕?span class="mq-1522">澧炲己杩欎袱涓疄鐜扮被鐨勮瘽锛屾垜浠氨鍙互閲囩敤瑁呴グ妯″紡锛岀敤鍏蜂綋鐨勮楗板櫒鏉?span class="mq-1523">瑁呴グ瀹炵幇绫伙紝浠ヨ揪鍒板寮虹殑鐩殑銆?/span>
浠庡悕瀛楁潵绠€鍗曡В閲婁笅瑁呴グ鍣ㄣ€傛棦鐒惰鏄楗帮紝閭d箞寰€寰€灏辨槸娣诲姞灏忓姛鑳?/strong>杩欑锛岃€屼笖锛屾垜浠婊¤冻鍙互娣诲姞澶氫釜灏忓姛鑳姐€傛渶绠€鍗曠殑锛屼唬鐞嗘ā寮忓氨鍙互瀹炵幇鍔熻兘鐨勫寮猴紝浣嗘槸浠g悊涓嶅鏄撳疄鐜板涓姛鑳界殑澧炲己锛屽綋鐒朵綘鍙互璇寸敤浠g悊鍖呰浠g悊鐨勫灞傚寘瑁呮柟寮忥紝浣嗘槸閭f牱鐨勮瘽浠g爜灏卞鏉備簡銆?/span>
棣栧厛鏄庣櫧涓€浜涚畝鍗曠殑姒傚康锛屼粠鍥句腑鎴戜滑鐪嬪埌锛屾墍鏈夌殑鍏蜂綋瑁呴グ鑰呬滑 ConcreteDecorator* 閮藉彲浠ヤ綔涓?Component 鏉ヤ娇鐢紝鍥犱负瀹冧滑閮藉疄鐜颁簡 Component 涓殑鎵€鏈夋帴鍙c€傚畠浠拰 Component 瀹炵幇绫?ConcreteComponent* 鐨勫尯鍒槸锛屽畠浠彧鏄楗拌€咃紝璧?span class="mq-1531">瑁呴グ浣滅敤锛屼篃灏辨槸鍗充娇瀹冧滑鐪嬩笂鍘荤墰閫艰桨杞帮紝浣嗘槸瀹冧滑閮藉彧鏄湪鍏蜂綋鐨勫疄鐜颁腑鍔犱簡灞傜毊鏉ヨ楗?/strong>鑰屽凡銆?/span>
娉ㄦ剰杩欐璇濅腑娣锋潅鍦ㄥ悇涓悕璇嶄腑鐨?Component 鍜?Decorator锛屽埆鎼炴贩浜嗐€?/span>
涓嬮潰鏉ョ湅鐪嬩竴涓緥瀛愶紝鍏堟妸瑁呴グ妯″紡寮勬竻妤氾紝鐒跺悗鍐嶄粙缁嶄笅 java io 涓殑瑁呴グ妯″紡鐨勫簲鐢ㄣ€?/span>
鏈€杩戝ぇ琛椾笂娴佽璧锋潵浜嗏€滃揩涔愭煚妾€濓紝鎴戜滑鎶婂揩涔愭煚妾殑楗枡鍒嗕负涓夌被锛氱孩鑼躲€佺豢鑼躲€佸挅鍟★紝鍦ㄨ繖涓夊ぇ绫荤殑鍩虹涓婏紝鍙堝鍔犱簡璁稿鐨勫彛鍛筹紝浠€涔堥噾妗旀煚妾孩鑼躲€侀噾妗旀煚妾弽鐝犵豢鑼躲€佽姃鏋滅孩鑼躲€佽姃鏋滅豢鑼躲€佽姃鏋滅弽鐝犵孩鑼躲€佺儰鐝嶇彔绾㈣尪銆佺儰鐝嶇彔鑺掓灉缁胯尪銆佹ぐ棣欒儦鑺藉挅鍟°€佺劍绯栧彲鍙挅鍟$瓑绛夛紝姣忓搴楅兘鏈夊緢闀跨殑鑿滃崟锛屼絾鏄粩缁嗙湅涓嬶紝鍏跺疄鍘熸枡涔熸病鍑犳牱锛屼絾鏄彲浠ユ惌閰嶅嚭寰堝缁勫悎锛屽鏋滈【瀹㈤渶瑕侊紝寰堝娌″嚭鐜板湪鑿滃崟涓殑楗枡浠栦滑涔熸槸鍙互鍋氱殑銆?/span>
鍦ㄨ繖涓緥瀛愪腑锛岀孩鑼躲€佺豢鑼躲€佸挅鍟℃槸鏈€鍩虹鐨勯ギ鏂欙紝鍏朵粬鐨勫儚閲戞鏌犳銆佽姃鏋溿€佺弽鐝犮€佹ぐ鏋溿€佺劍绯栫瓑閮藉睘浜庤楗扮敤鐨勩€傚綋鐒讹紝鍦ㄥ紑鍙戜腑锛屾垜浠‘瀹炲彲浠ュ儚闂ㄥ簵涓€鏍凤紝寮€鍙戣繖浜涚被锛歀emonBlackTea銆丩emonGreenTea銆丮angoBlackTea銆丮angoLemonGreenTea鈥︹€︿絾鏄紝寰堝揩鎴戜滑灏卞彂鐜帮紝杩欐牱瀛愬共鑲畾鏄笉琛岀殑锛岃繖浼氬鑷存垜浠渶瑕佺粍鍚堝嚭鎵€鏈夌殑鍙兘锛岃€屼笖濡傛灉瀹汉闇€瑕佸湪绾㈣尪涓姞鍙屼唤鏌犳鎬庝箞鍔烇紵涓変唤鏌犳鎬庝箞鍔烇紵
public abstract class Beverage { // 杩斿洖鎻忚堪 public abstract String getDescription () ; // 杩斿洖浠锋牸 public abstract double cost () ; }
鐒跺悗鏄笁涓熀纭€楗枡瀹炵幇绫伙紝绾㈣尪銆佺豢鑼跺拰鍜栧暋锛?/span>
public class BlackTea extends Beverage { public String getDescription () { return "绾㈣尪" ; } public double cost () { return 10 ; } }public class GreenTea extends Beverage { public String getDescription () { return "缁胯尪" ; } public double cost () { return 11 ; } } ...// 鍜栧暋鐪佺暐
瀹氫箟璋冩枡锛屼篃灏辨槸瑁呴グ鑰呯殑鍩虹被锛屾绫诲繀椤荤户鎵胯嚜 Beverage锛?/span>
// 璋冩枡 public abstract class Condiment extends Beverage { }
鐒跺悗鎴戜滑鏉ュ畾涔夋煚妾€佽姃鏋滅瓑鍏蜂綋鐨勮皟鏂欙紝瀹冧滑灞炰簬瑁呴グ鑰咃紝姣棤鐤戦棶锛岃繖浜涜皟鏂欒偗瀹氶兘闇€瑕佺户鎵胯皟鏂?Condiment 绫伙細
public class Lemon extends Condiment { private Beverage bevarage; // 杩欓噷寰堝叧閿紝闇€瑕佷紶鍏ュ叿浣撶殑楗枡锛屽闇€瑕佷紶鍏ユ病鏈夎瑁呴グ鐨勭孩鑼舵垨缁胯尪锛?/span> // 褰撶劧涔熷彲浠ヤ紶鍏ュ凡缁忚楗板ソ鐨勮姃鏋滅豢鑼讹紝杩欐牱鍙互鍋氳姃鏋滄煚妾豢鑼?/span> public Lemon (Beverage bevarage) { this .bevarage = bevarage; } public String getDescription () { // 瑁呴グ return bevarage.getDescription() + ", 鍔犳煚妾? ; } public double cost () { // 瑁呴グ return beverage.cost() + 2 ; // 鍔犳煚妾渶瑕?nbsp;2 鍏?/span> } }public class Mango extends Condiment { private Beverage bevarage; public Mango (Beverage bevarage) { this .bevarage = bevarage; } public String getDescription () { return bevarage.getDescription() + ", 鍔犺姃鏋? ; } public double cost () { return beverage.cost() + 3 ; // 鍔犺姃鏋滈渶瑕?nbsp;3 鍏?/span> } } ...// 缁欐瘡涓€绉嶈皟鏂欓兘鍔犱竴涓被
鐪嬪鎴风璋冪敤锛?/span>
public static void main (String[] args) { // 棣栧厛锛屾垜浠渶瑕佷竴涓熀纭€楗枡锛岀孩鑼躲€佺豢鑼舵垨鍜栧暋 Beverage beverage = new GreenTea(); // 寮€濮嬭楗?/span> beverage = new Lemon(beverage); // 鍏堝姞涓€浠芥煚妾?/span> beverage = new Mongo(beverage); // 鍐嶅姞涓€浠借姃鏋?/span> System.out.println(beverage.getDescription() + " 浠锋牸锛氾骏" + beverage.cost()); //"缁胯尪, 鍔犳煚妾? 鍔犺姃鏋?nbsp;浠锋牸锛氾骏16" }
濡傛灉鎴戜滑闇€瑕?nbsp;鑺掓灉-鐝嶇彔-鍙屼唤鏌犳-绾㈣尪锛?/span>
Beverage beverage = new Mongo(new Pearl(new Lemon(new Lemon(new BlackTea()))));
鏄笉鏄緢鍙樻€侊紵
鐪嬬湅涓嬪浘鍙兘浼氭竻鏅颁竴浜涳細
鍒拌繖閲岋紝澶у搴旇宸茬粡娓呮瑁呴グ妯″紡浜嗗惂銆?/span>
涓嬮潰锛屾垜浠啀鏉ヨ璇?java IO 涓殑瑁呴グ妯″紡銆傜湅涓嬪浘 InputStream 娲剧敓鍑烘潵鐨勯儴鍒嗙被锛?/span>
鎴戜滑鐭ラ亾 InputStream 浠h〃浜嗚緭鍏ユ祦锛屽叿浣撶殑杈撳叆鏉ユ簮鍙互鏄枃浠讹紙FileInputStream锛夈€佺閬擄紙PipedInputStream锛夈€佹暟缁勶紙ByteArrayInputStream锛夌瓑锛岃繖浜涘氨鍍忓墠闈㈠ザ鑼剁殑渚嬪瓙涓殑绾㈣尪銆佺豢鑼讹紝灞炰簬鍩虹杈撳叆娴併€?/span>
FilterInputStream 鎵挎帴浜嗚楗版ā寮忕殑鍏抽敭鑺傜偣锛屽畠鐨勫疄鐜扮被鏄竴绯诲垪瑁呴グ鍣紝姣斿 BufferedInputStream 浠h〃鐢ㄧ紦鍐叉潵瑁呴グ锛屼篃灏变娇寰楄緭鍏ユ祦鍏锋湁浜嗙紦鍐茬殑鍔熻兘锛孡ineNumberInputStream 浠h〃鐢ㄨ鍙锋潵瑁呴グ锛屽湪鎿嶄綔鐨勬椂鍊欏氨鍙互鍙栧緱琛屽彿浜嗭紝DataInputStream 鐨勮楗帮紝浣垮緱鎴戜滑鍙互浠庤緭鍏ユ祦杞崲涓?java 涓殑鍩烘湰绫诲瀷鍊笺€?/span>
褰撶劧锛屽湪 java IO 涓紝濡傛灉鎴戜滑浣跨敤瑁呴グ鍣ㄧ殑璇濓紝灏变笉澶€傚悎闈㈠悜鎺ュ彛缂栫▼浜嗭紝濡傦細
InputStream inputStream = new LineNumberInputStream(new BufferedInputStream(new FileInputStream("" )));
杩欐牱鐨勭粨鏋滄槸锛孖nputStream 杩樻槸涓嶅叿鏈夎鍙栬鍙风殑鍔熻兘锛屽洜涓鸿鍙栬鍙风殑鏂规硶瀹氫箟鍦?LineNumberInputStream 绫讳腑銆?/span>
鎴戜滑搴旇鍍忎笅闈㈣繖鏍蜂娇鐢細
DataInputStream is = new DataInputStream( new BufferedInputStream( new FileInputStream("" )));
鎵€浠ヨ鍢涳紝瑕佹壘鍒扮函鐨勪弗鏍肩鍚堣璁℃ā寮忕殑浠g爜杩樻槸姣旇緝闅剧殑銆?/span>
闂ㄩ潰妯″紡
闂ㄩ潰妯″紡锛堜篃鍙瑙傛ā寮忥紝Facade Pattern锛夊湪璁稿婧愮爜涓湁浣跨敤锛屾瘮濡?slf4j 灏卞彲浠ョ悊瑙d负鏄棬闈㈡ā寮忕殑搴旂敤銆傝繖鏄竴涓畝鍗曠殑璁捐妯″紡锛屾垜浠洿鎺ヤ笂浠g爜鍐嶈鍚с€?/span>
public interface Shape { void draw () ; }
瀹氫箟鍑犱釜瀹炵幇绫伙細
public class Circle implements Shape { @Override public void draw () { System.out.println("Circle::draw()" ); } }public class Rectangle implements Shape { @Override public void draw () { System.out.println("Rectangle::draw()" ); } }
瀹㈡埛绔皟鐢細
public static void main (String[] args) { // 鐢讳竴涓渾褰?/span> Shape circle = new Circle(); circle.draw(); // 鐢讳竴涓暱鏂瑰舰 Shape rectangle = new Rectangle(); rectangle.draw(); }
浠ヤ笂鏄垜浠父鍐欑殑浠g爜锛屾垜浠渶瑕佺敾鍦嗗氨瑕佸厛瀹炰緥鍖栧渾锛岀敾闀挎柟褰㈠氨闇€瑕佸厛瀹炰緥鍖栦竴涓暱鏂瑰舰锛岀劧鍚庡啀璋冪敤鐩稿簲鐨?draw() 鏂规硶銆?/span>
涓嬮潰锛屾垜浠湅鐪嬫€庝箞鐢ㄩ棬闈㈡ā寮忔潵璁╁鎴风璋冪敤鏇村姞鍙嬪ソ涓€浜涖€?/span>
public class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; public ShapeMaker () { circle = new Circle(); rectangle = new Rectangle(); square = new Square(); } /** * 涓嬮潰瀹氫箟涓€鍫嗘柟娉曪紝鍏蜂綋搴旇璋冪敤浠€涔堟柟娉曪紝鐢辫繖涓棬闈㈡潵鍐冲畾 */ public void drawCircle () { circle.draw(); } public void drawRectangle () { rectangle.draw(); } public void drawSquare () { square.draw(); } }
鐪嬬湅鐜板湪瀹㈡埛绔€庝箞璋冪敤锛?/span>
public static void main (String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); // 瀹㈡埛绔皟鐢ㄧ幇鍦ㄦ洿鍔犳竻鏅颁簡 shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); }
闂ㄩ潰妯″紡鐨勪紭鐐规樉鑰屾槗瑙侊紝瀹㈡埛绔笉鍐嶉渶瑕佸叧娉ㄥ疄渚嬪寲鏃跺簲璇ヤ娇鐢ㄥ摢涓疄鐜扮被锛岀洿鎺ヨ皟鐢ㄩ棬闈㈡彁渚涚殑鏂规硶灏卞彲浠ヤ簡锛屽洜涓洪棬闈㈢被鎻愪緵鐨勬柟娉曠殑鏂规硶鍚嶅浜庡鎴风鏉ヨ宸茬粡寰堝弸濂戒簡銆?/span>
缁勫悎妯″紡
缁勫悎妯″紡鐢ㄤ簬琛ㄧず鍏锋湁灞傛缁撴瀯鐨勬暟鎹紝浣垮緱鎴戜滑瀵瑰崟涓璞″拰缁勫悎瀵硅薄鐨勮闂叿鏈変竴鑷存€с€?/span>
鐩存帴鐪嬩竴涓緥瀛愬惂锛屾瘡涓憳宸ラ兘鏈夊鍚嶃€侀儴闂ㄣ€佽柂姘磋繖浜涘睘鎬э紝鍚屾椂杩樻湁涓嬪睘鍛樺伐闆嗗悎锛堣櫧鐒跺彲鑳介泦鍚堜负绌猴級锛岃€屼笅灞炲憳宸ュ拰鑷繁鐨勭粨鏋勬槸涓€鏍风殑锛屼篃鏈夊鍚嶃€侀儴闂ㄨ繖浜涘睘鎬э紝鍚屾椂涔熸湁浠栦滑鐨勪笅灞炲憳宸ラ泦鍚堛€?/span>
public class Employee { private String name; private String dept; private int salary; private List<Employee> subordinates; // 涓嬪睘 public Employee (String name,String dept, int sal) { this .name = name; this .dept = dept; this .salary = sal; subordinates = new ArrayList<Employee>(); } public void add (Employee e) { subordinates.add(e); } public void remove (Employee e) { subordinates.remove(e); } public List<Employee> getSubordinates () { return subordinates; } public String toString () { return ("Employee :[ Name : " + name + ", dept : " + dept + ", salary :" + salary+" ]" ); } }
閫氬父锛岃繖绉嶇被闇€瑕佸畾涔?add(node)銆乺emove(node)銆乬etChildren() 杩欎簺鏂规硶銆?/span>
杩欒鐨勫叾瀹炲氨鏄粍鍚堟ā寮忥紝杩欑绠€鍗曠殑妯″紡鎴戝氨涓嶅仛杩囧浠嬬粛浜嗭紝鐩镐俊鍚勪綅璇昏€呬篃涓嶅枩娆㈢湅鎴戝啓搴熻瘽銆?/span>
浜厓妯″紡
鑻辨枃鏄?Flyweight Pattern锛屼笉鐭ラ亾鏄皝鏈€鍏堢炕璇戠殑杩欎釜璇嶏紝鎰熻杩欑炕璇戠湡鐨勪笉濂界悊瑙o紝鎴戜滑璇曠潃寮鸿鍏宠仈璧锋潵鍚с€侳lyweight 鏄交閲忕骇鐨勬剰鎬濓紝浜厓鍒嗗紑鏉ヨ灏辨槸 鍏变韩 鍏冨櫒浠讹紝涔熷氨鏄鐢ㄥ凡缁忕敓鎴愮殑瀵硅薄锛岃繖绉嶅仛娉曞綋鐒朵篃灏辨槸杞婚噺绾х殑浜嗐€?/span>
澶嶇敤瀵硅薄鏈€绠€鍗曠殑鏂瑰紡鏄紝鐢ㄤ竴涓?HashMap 鏉ュ瓨鏀炬瘡娆℃柊鐢熸垚鐨勫璞°€傛瘡娆¢渶瑕佷竴涓璞$殑鏃跺€欙紝鍏堝埌 HashMap 涓湅鐪嬫湁娌℃湁锛屽鏋滄病鏈夛紝鍐嶇敓鎴愭柊鐨勫璞★紝鐒跺悗灏嗚繖涓璞℃斁鍏?HashMap 涓€?/span>
杩欑绠€鍗曠殑浠g爜鎴戝氨涓嶆紨绀轰簡銆?/span>
缁撴瀯鍨嬫ā寮忔€荤粨
鍓嶉潰锛屾垜浠浜嗕唬鐞嗘ā寮忋€侀€傞厤鍣ㄦā寮忋€佹ˉ姊佹ā寮忋€佽楗版ā寮忋€侀棬闈㈡ā寮忋€佺粍鍚堟ā寮忓拰浜厓妯″紡銆傝鑰呮槸鍚﹀彲浠ュ垎鍒妸杩欏嚑涓ā寮忚娓呮浜嗗憿锛熷湪璇村埌杩欎簺妯″紡鐨勬椂鍊欙紝蹇冧腑鏄惁鏈変竴涓竻鏅扮殑鍥炬垨澶勭悊娴佺▼鍦ㄨ剳娴烽噷鍛紵
浠g悊妯″紡鏄仛鏂规硶澧炲己鐨勶紝閫傞厤鍣ㄦā寮忔槸鎶婇浮鍖呰鎴愰腑杩欑鐢ㄦ潵閫傞厤鎺ュ彛鐨勶紝妗ユ妯″紡鍋氬埌浜嗗緢濂界殑瑙h€︼紝瑁呴グ妯″紡浠庡悕瀛椾笂灏辩湅寰楀嚭鏉ワ紝閫傚悎浜庤楗扮被鎴栬€呰鏄寮虹被鐨勫満鏅紝闂ㄩ潰妯″紡鐨勪紭鐐规槸瀹㈡埛绔笉闇€瑕佸叧蹇冨疄渚嬪寲杩囩▼锛屽彧瑕佽皟鐢ㄩ渶瑕佺殑鏂规硶鍗冲彲锛岀粍鍚堟ā寮忕敤浜庢弿杩板叿鏈夊眰娆$粨鏋勭殑鏁版嵁锛屼韩鍏冩ā寮忔槸涓轰簡鍦ㄧ壒瀹氱殑鍦烘櫙涓紦瀛樺凡缁忓垱寤虹殑瀵硅薄锛岀敤浜庢彁楂樻€ц兘銆?/span>
琛屼负鍨嬫ā寮?/span>
琛屼负鍨嬫ā寮忓叧娉ㄧ殑鏄悇涓被涔嬮棿鐨勭浉浜掍綔鐢紝灏嗚亴璐e垝鍒嗘竻妤氾紝浣垮緱鎴戜滑鐨勪唬鐮佹洿鍔犲湴娓呮櫚銆?/span>
绛栫暐妯″紡
绛栫暐妯″紡澶父鐢ㄤ簡锛屾墍浠ユ妸瀹冩斁鍒版渶鍓嶉潰杩涜浠嬬粛銆傚畠姣旇緝绠€鍗曪紝鎴戝氨涓嶅簾璇濓紝鐩存帴鐢ㄤ唬鐮佽浜嬪惂銆?/span>
涓嬮潰璁捐鐨勫満鏅槸锛屾垜浠渶瑕佺敾涓€涓浘褰紝鍙€夌殑绛栫暐灏辨槸鐢ㄧ孩鑹茬瑪鏉ョ敾锛岃繕鏄豢鑹茬瑪鏉ョ敾锛屾垨鑰呰摑鑹茬瑪鏉ョ敾銆?/span>
棣栧厛锛屽厛瀹氫箟涓€涓瓥鐣ユ帴鍙o細
public interface Strategy { public void draw (int radius, int x, int y) ; }
鐒跺悗鎴戜滑瀹氫箟鍏蜂綋鐨勫嚑涓瓥鐣ワ細
public class RedPen implements Strategy { @Override public void draw (int radius, int x, int y) { System.out.println("鐢ㄧ孩鑹茬瑪鐢诲浘锛宺adius:" + radius + ", x:" + x + ", y:" + y); } }public class GreenPen implements Strategy { @Override public void draw (int radius, int x, int y) { System.out.println("鐢ㄧ豢鑹茬瑪鐢诲浘锛宺adius:" + radius + ", x:" + x + ", y:" + y); } }public class BluePen implements Strategy { @Override public void draw (int radius, int x, int y) { System.out.println("鐢ㄨ摑鑹茬瑪鐢诲浘锛宺adius:" + radius + ", x:" + x + ", y:" + y); } }
浣跨敤绛栫暐鐨勭被锛?/p>
public class Context { private Strategy strategy; public Context (Strategy strategy) { this .strategy = strategy; } public int executeDraw (int radius, int x, int y) { return strategy.draw(radius, x, y); } }
瀹㈡埛绔紨绀猴細
public static void main (String[] args) { Context context = new Context(new BluePen()); // 浣跨敤缁胯壊绗旀潵鐢?/span> context.executeDraw(10 , 0 , 0 ); }
鏀惧埌涓€寮犲浘涓婏紝璁╁ぇ瀹剁湅寰楁竻鏅颁簺锛?/span>
杩欎釜鏃跺€欙紝澶у鏈夋病鏈夎仈鎯冲埌缁撴瀯鍨嬫ā寮忎腑鐨勬ˉ姊佹ā寮忥紝瀹冧滑鍏跺疄闈炲父鐩镐技锛屾垜鎶婃ˉ姊佹ā寮忕殑鍥炬嬁杩囨潵澶у瀵规瘮涓嬶細
瑕佹垜璇寸殑璇濓紝瀹冧滑闈炲父鐩镐技锛屾ˉ姊佹ā寮忓湪宸︿晶鍔犱簡涓€灞傛娊璞¤€屽凡銆傛ˉ姊佹ā寮忕殑鑰﹀悎鏇翠綆锛岀粨鏋勬洿澶嶆潅涓€浜涖€?/span>
瑙傚療鑰呮ā寮?/span>
瑙傚療鑰呮ā寮忓浜庢垜浠潵璇达紝鐪熸槸鍐嶇畝鍗曚笉杩囦簡銆傛棤澶栦箮涓や釜鎿嶄綔锛岃瀵熻€呰闃呰嚜宸卞叧蹇冪殑涓婚鍜屼富棰樻湁鏁版嵁鍙樺寲鍚庨€氱煡瑙傚療鑰呬滑銆?/span>
棣栧厛锛岄渶瑕佸畾涔変富棰橈紝姣忎釜涓婚闇€瑕佹寔鏈夎瀵熻€呭垪琛ㄧ殑寮曠敤锛岀敤浜庡湪鏁版嵁鍙樻洿鐨勬椂鍊欓€氱煡鍚勪釜瑙傚療鑰咃細
public class Subject { private List<Observer> observers = new ArrayList<Observer>(); private int state; public int getState () { return state; } public void setState (int state) { this .state = state; // 鏁版嵁宸插彉鏇达紝閫氱煡瑙傚療鑰呬滑 notifyAllObservers(); } // 娉ㄥ唽瑙傚療鑰?/span> public void attach (Observer observer) { observers.add(observer); } // 閫氱煡瑙傚療鑰呬滑 public void notifyAllObservers () { for (Observer observer : observers) { observer.update(); } } }
瀹氫箟瑙傚療鑰呮帴鍙o細
public abstract class Observer { protected Subject subject; public abstract void update () ; }
鍏跺疄濡傛灉鍙湁涓€涓瀵熻€呯被鐨勮瘽锛屾帴鍙i兘涓嶇敤瀹氫箟浜嗭紝涓嶈繃锛岄€氬父鍦烘櫙涓嬶紝鏃㈢劧鐢ㄥ埌浜嗚瀵熻€呮ā寮忥紝鎴戜滑灏辨槸甯屾湜涓€涓簨浠跺嚭鏉ヤ簡锛屼細鏈夊涓笉鍚岀殑绫婚渶瑕佸鐞嗙浉搴旂殑淇℃伅銆傛瘮濡傦紝璁㈠崟淇敼鎴愬姛浜嬩欢锛屾垜浠笇鏈涘彂鐭俊鐨勭被寰楀埌閫氱煡銆佸彂閭欢鐨勭被寰楀埌閫氱煡銆佸鐞嗙墿娴佷俊鎭殑绫诲緱鍒伴€氱煡绛夈€?/span>
鎴戜滑鏉ュ畾涔夊叿浣撶殑鍑犱釜瑙傚療鑰呯被锛?/span>
public class BinaryObserver extends Observer { // 鍦ㄦ瀯閫犳柟娉曚腑杩涜璁㈤槄涓婚 public BinaryObserver (Subject subject) { this .subject = subject; // 閫氬父鍦ㄦ瀯閫犳柟娉曚腑灏?nbsp;this 鍙戝竷鍑哄幓鐨勬搷浣滀竴瀹氳灏忓績 this .subject.attach(this ); } // 璇ユ柟娉曠敱涓婚绫诲湪鏁版嵁鍙樻洿鐨勬椂鍊欒繘琛岃皟鐢?/span> @Override public void update () { String result = Integer.toBinaryString(subject.getState()); System.out.println("璁㈤槄鐨勬暟鎹彂鐢熷彉鍖栵紝鏂扮殑鏁版嵁澶勭悊涓轰簩杩涘埗鍊间负锛? + result); } }public class HexaObserver extends Observer { public HexaObserver (Subject subject) { this .subject = subject; this .subject.attach(this ); } @Override public void update () { String result = Integer.toHexString(subject.getState()).toUpperCase(); System.out.println("璁㈤槄鐨勬暟鎹彂鐢熷彉鍖栵紝鏂扮殑鏁版嵁澶勭悊涓哄崄鍏繘鍒跺€间负锛? + result); } }
瀹㈡埛绔娇鐢ㄤ篃闈炲父绠€鍗曪細
public static void main (String[] args) { // 鍏堝畾涔変竴涓富棰?/span> Subject subject1 = new Subject(); // 瀹氫箟瑙傚療鑰?/span> new BinaryObserver(subject1); new HexaObserver(subject1); // 妯℃嫙鏁版嵁鍙樻洿锛岃繖涓椂鍊欙紝瑙傚療鑰呬滑鐨?nbsp;update 鏂规硶灏嗕細琚皟鐢?/span> subject.setState(11 ); }
output:
璁㈤槄鐨勬暟鎹彂鐢熷彉鍖栵紝鏂扮殑鏁版嵁澶勭悊涓轰簩杩涘埗鍊间负锛?011 璁㈤槄鐨勬暟鎹彂鐢熷彉鍖栵紝鏂扮殑鏁版嵁澶勭悊涓哄崄鍏繘鍒跺€间负锛欱
褰撶劧锛宩dk 涔熸彁渚涗簡鐩镐技鐨勬敮鎸侊紝鍏蜂綋鐨勫ぇ瀹跺彲浠ュ弬鑰?java.util.Observable 鍜?java.util.Observer 杩欎袱涓被銆?/span>
瀹為檯鐢熶骇杩囩▼涓紝瑙傚療鑰呮ā寮忓線寰€鐢ㄦ秷鎭腑闂翠欢鏉ュ疄鐜帮紝濡傛灉瑕佸疄鐜板崟鏈鸿瀵熻€呮ā寮忥紝绗旇€呭缓璁鑰呬娇鐢?Guava 涓殑 EventBus锛屽畠鏈夊悓姝ュ疄鐜颁篃鏈夊紓姝ュ疄鐜帮紝鏈枃涓昏浠嬬粛璁捐妯″紡锛屽氨涓嶅睍寮€璇翠簡銆?/span>
杩樻湁锛屽嵆浣挎槸涓婇潰鐨勮繖涓唬鐮侊紝涔熶細鏈夊緢澶氬彉绉嶏紝澶у鍙璁颁綇鏍稿績鐨勯儴鍒嗭紝閭e氨鏄竴瀹氭湁涓€涓湴鏂瑰瓨鏀句簡鎵€鏈夌殑瑙傚療鑰咃紝鐒跺悗鍦ㄤ簨浠跺彂鐢熺殑鏃跺€欙紝閬嶅巻瑙傚療鑰咃紝璋冪敤瀹冧滑鐨勫洖璋冨嚱鏁般€?/span>
璐d换閾炬ā寮?/span>
璐d换閾鹃€氬父闇€瑕佸厛寤虹珛涓€涓崟鍚戦摼琛紝鐒跺悗璋冪敤鏂瑰彧闇€瑕佽皟鐢ㄥご閮ㄨ妭鐐瑰氨鍙互浜嗭紝鍚庨潰浼氳嚜鍔ㄦ祦杞笅鍘汇€傛瘮濡傛祦绋嬪鎵瑰氨鏄竴涓緢濂界殑渚嬪瓙锛屽彧瑕佺粓绔敤鎴锋彁浜ょ敵璇凤紝鏍规嵁鐢宠鐨勫唴瀹逛俊鎭紝鑷姩寤虹珛涓€鏉¤矗浠婚摼锛岀劧鍚庡氨鍙互寮€濮嬫祦杞簡銆?/span>
鏈夎繖涔堜竴涓満鏅紝鐢ㄦ埛鍙傚姞涓€涓椿鍔ㄥ彲浠ラ鍙栧鍝侊紝浣嗘槸娲诲姩闇€瑕佽繘琛屽緢澶氱殑瑙勫垯鏍¢獙鐒跺悗鎵嶈兘鏀捐锛屾瘮濡傞鍏堥渶瑕佹牎楠岀敤鎴锋槸鍚︽槸鏂扮敤鎴枫€佷粖鏃ュ弬涓庝汉鏁版槸鍚︽湁闄愰銆佸叏鍦哄弬涓庝汉鏁版槸鍚︽湁闄愰绛夌瓑銆傝瀹氱殑瑙勫垯閮介€氳繃鍚庯紝鎵嶈兘璁╃敤鎴烽璧板鍝併€?/span>
濡傛灉浜у搧缁欎綘杩欎釜闇€姹傜殑璇濓紝鎴戞兂澶ч儴鍒嗕汉涓€寮€濮嬭偗瀹氭兂鐨勫氨鏄紝鐢ㄤ竴涓?List 鏉ュ瓨鏀炬墍鏈夌殑瑙勫垯锛岀劧鍚?foreach 鎵ц涓€涓嬫瘡涓鍒欏氨濂戒簡銆備笉杩囷紝璇昏€呬篃鍏堝埆鎬ワ紝鐪嬬湅璐d换閾炬ā寮忓拰鎴戜滑璇寸殑杩欎釜鏈変粈涔堜笉涓€鏍凤紵
棣栧厛锛屾垜浠瀹氫箟娴佺▼涓婅妭鐐圭殑鍩虹被锛?/span>
public abstract class RuleHandler { // 鍚庣户鑺傜偣 protected RuleHandler successor; public abstract void apply (Context context) ; public void setSuccessor (RuleHandler successor) { this .successor = successor; } public RuleHandler getSuccessor () { return successor; } }
鎺ヤ笅鏉ワ紝鎴戜滑闇€瑕佸畾涔夊叿浣撶殑姣忎釜鑺傜偣浜嗐€?/span>
鏍¢獙鐢ㄦ埛鏄惁鏄柊鐢ㄦ埛锛?/span>
public class NewUserRuleHandler extends RuleHandler { public void apply (Context context) { if (context.isNewUser()) { // 濡傛灉鏈夊悗缁ц妭鐐圭殑璇濓紝浼犻€掍笅鍘?/span> if (this .getSuccessor() != null ) { this .getSuccessor().apply(context); } } else { throw new RuntimeException("璇ユ椿鍔ㄤ粎闄愭柊鐢ㄦ埛鍙備笌" ); } } }
鏍¢獙鐢ㄦ埛鎵€鍦ㄥ湴鍖烘槸鍚﹀彲浠ュ弬涓庯細
public class LocationRuleHandler extends RuleHandler { public void apply (Context context) { boolean allowed = activityService.isSupportedLocation(context.getLocation); if (allowed) { if (this .getSuccessor() != null ) { this .getSuccessor().apply(context); } } else { throw new RuntimeException("闈炲父鎶辨瓑锛屾偍鎵€鍦ㄧ殑鍦板尯鏃犳硶鍙備笌鏈娲诲姩" ); } } }
鏍¢獙濂栧搧鏄惁宸查瀹岋細
public class LimitRuleHandler extends RuleHandler { public void apply (Context context) { int remainedTimes = activityService.queryRemainedTimes(context); // 鏌ヨ鍓╀綑濂栧搧 if (remainedTimes > 0 ) { if (this .getSuccessor() != null ) { this .getSuccessor().apply(userInfo); } } else { throw new RuntimeException("鎮ㄦ潵寰楀お鏅氫簡锛屽鍝佽棰嗗畬浜? ); } } }
瀹㈡埛绔細
public static void main (String[] args) { RuleHandler newUserHandler = new NewUserRuleHandler(); RuleHandler locationHandler = new LocationRuleHandler(); RuleHandler limitHandler = new LimitRuleHandler(); // 鍋囪鏈娲诲姩浠呮牎楠屽湴鍖哄拰濂栧搧鏁伴噺锛屼笉鏍¢獙鏂拌€佺敤鎴?/span> locationHandler.setSuccessor(limitHandler); locationHandler.apply(context); }
浠g爜鍏跺疄寰堢畝鍗曪紝灏辨槸鍏堝畾涔夊ソ涓€涓摼琛紝鐒跺悗鍦ㄩ€氳繃浠绘剰涓€鑺傜偣鍚庯紝濡傛灉姝よ妭鐐规湁鍚庣户鑺傜偣锛岄偅涔堜紶閫掍笅鍘汇€?/span>
鑷充簬瀹冨拰鎴戜滑鍓嶉潰璇寸殑鐢ㄤ竴涓?List 瀛樻斁闇€瑕佹墽琛岀殑瑙勫垯鐨勫仛娉曟湁浠€涔堝紓鍚岋紝鐣欑粰璇昏€呰嚜宸辩悽纾ㄥ惂銆?/span>
妯℃澘鏂规硶妯″紡
鍦ㄥ惈鏈夌户鎵跨粨鏋勭殑浠g爜涓紝妯℃澘鏂规硶妯″紡鏄潪甯稿父鐢ㄧ殑銆?/span>
public abstract class AbstractTemplate { // 杩欏氨鏄ā鏉挎柟娉?/span> public void templateMethod () { init(); apply(); // 杩欎釜鏄噸鐐?/span> end(); // 鍙互浣滀负閽╁瓙鏂规硶 } protected void init () { System.out.println("init 鎶借薄灞傚凡缁忓疄鐜帮紝瀛愮被涔熷彲浠ラ€夋嫨瑕嗗啓" ); } // 鐣欑粰瀛愮被瀹炵幇 protected abstract void apply () ; protected void end () { } }
妯℃澘鏂规硶涓皟鐢ㄤ簡 3 涓柟娉曪紝鍏朵腑 apply() 鏄娊璞℃柟娉曪紝瀛愮被蹇呴』瀹炵幇瀹冿紝鍏跺疄妯℃澘鏂规硶涓湁鍑犱釜鎶借薄鏂规硶瀹屽叏鏄嚜鐢辩殑锛屾垜浠篃鍙互灏嗕笁涓柟娉曢兘璁剧疆涓烘娊璞℃柟娉曪紝璁╁瓙绫绘潵瀹炵幇銆備篃灏辨槸璇达紝妯℃澘鏂规硶鍙礋璐e畾涔夌涓€姝ュ簲璇ヨ鍋氫粈涔堬紝绗簩姝ュ簲璇ュ仛浠€涔堬紝绗笁姝ュ簲璇ュ仛浠€涔堬紝鑷充簬鎬庝箞鍋氾紝鐢卞瓙绫绘潵瀹炵幇銆?/span>
public class ConcreteTemplate extends AbstractTemplate { public void apply () { System.out.println("瀛愮被瀹炵幇鎶借薄鏂规硶 apply" ); } public void end () { System.out.println("鎴戜滑鍙互鎶?nbsp;method3 褰撳仛閽╁瓙鏂规硶鏉ヤ娇鐢紝闇€瑕佺殑鏃跺€欒鍐欏氨鍙互浜? ); } }
瀹㈡埛绔皟鐢ㄦ紨绀猴細
public static void main (String[] args) { AbstractTemplate t = new ConcreteTemplate(); // 璋冪敤妯℃澘鏂规硶 t.templateMethod(); }
浠g爜鍏跺疄寰堢畝鍗曪紝鍩烘湰涓婄湅鍒板氨鎳備簡锛屽叧閿槸瑕佸浼氱敤鍒拌嚜宸辩殑浠g爜涓€?/span>
鐘舵€佹ā寮?/span>
搴熻瘽鎴戝氨涓嶈浜嗭紝鎴戜滑璇翠竴涓畝鍗曠殑渚嬪瓙銆傚晢鍝佸簱瀛樹腑蹇冩湁涓渶鍩烘湰鐨勯渶姹傛槸鍑忓簱瀛樺拰琛ュ簱瀛橈紝鎴戜滑鐪嬬湅鎬庝箞鐢ㄧ姸鎬佹ā寮忔潵鍐欍€?/span>
鏍稿績鍦ㄤ簬锛屾垜浠殑鍏虫敞鐐逛笉鍐嶆槸 Context 鏄杩涜鍝鎿嶄綔锛岃€屾槸鍏虫敞鍦ㄨ繖涓?Context 浼氭湁鍝簺鎿嶄綔銆?/span>
public interface State { public void doAction (Context context) ; }
瀹氫箟鍑忓簱瀛樼殑鐘舵€侊細
public class DeductState implements State { public void doAction (Context context) { System.out.println("鍟嗗搧鍗栧嚭锛屽噯澶囧噺搴撳瓨" ); context.setState(this ); //... 鎵ц鍑忓簱瀛樼殑鍏蜂綋鎿嶄綔 } public String toString () { return "Deduct State" ; } }
瀹氫箟琛ュ簱瀛樼姸鎬侊細
public class RevertState implements State { public void doAction (Context context) { System.out.println("缁欐鍟嗗搧琛ュ簱瀛? ); context.setState(this ); //... 鎵ц鍔犲簱瀛樼殑鍏蜂綋鎿嶄綔 } public String toString () { return "Revert State" ; } }
鍓嶉潰鐢ㄥ埌浜?context.setState(this)锛屾垜浠潵鐪嬬湅鎬庝箞瀹氫箟 Context 绫伙細
public class Context { private State state; private String name; public Context (String name) { this .name = name; } public void setState (State state) { this .state = state; } public void getState () { return this .state; } }
鎴戜滑鏉ョ湅涓嬪鎴风璋冪敤锛屽ぇ瀹跺氨涓€娓呬簩妤氫簡锛?/span>
public static void main (String[] args) { // 鎴戜滑闇€瑕佹搷浣滅殑鏄?nbsp;iPhone X Context context = new Context("iPhone X" ); // 鐪嬬湅鎬庝箞杩涜琛ュ簱瀛樻搷浣?/span> State revertState = new RevertState(); revertState.doAction(context); // 鍚屾牱鐨勶紝鍑忓簱瀛樻搷浣滀篃闈炲父绠€鍗?/span> State deductState = new DeductState(); deductState.doAction(context); // 濡傛灉闇€瑕佹垜浠彲浠ヨ幏鍙栧綋鍓嶇殑鐘舵€?/span> // context.getState().toString(); }
璇昏€呭彲鑳戒細鍙戠幇锛屽湪涓婇潰杩欎釜渚嬪瓙涓紝濡傛灉鎴戜滑涓嶅叧蹇冨綋鍓?context 澶勪簬浠€涔堢姸鎬侊紝閭d箞 Context 灏卞彲浠ヤ笉鐢ㄧ淮鎶?state 灞炴€т簡锛岄偅鏍蜂唬鐮佷細绠€鍗曞緢澶氥€?/span>
涓嶈繃锛屽晢鍝佸簱瀛樿繖涓緥瀛愭瘯绔熷彧鏄釜渚嬶紝鎴戜滑杩樻湁寰堝瀹炰緥鏄渶瑕佺煡閬撳綋鍓?context 澶勪簬浠€涔堢姸鎬佺殑銆?/span>
琛屼负鍨嬫ā寮忔€荤粨
琛屼负鍨嬫ā寮忛儴鍒嗕粙缁嶄簡绛栫暐妯″紡銆佽瀵熻€呮ā寮忋€佽矗浠婚摼妯″紡銆佹ā鏉挎柟娉曟ā寮忓拰鐘舵€佹ā寮忥紝鍏跺疄锛岀粡鍏哥殑琛屼负鍨嬫ā寮忚繕鍖呮嫭澶囧繕褰曟ā寮忋€佸懡浠ゆā寮忕瓑锛屼絾鏄畠浠殑浣跨敤鍦烘櫙姣旇緝鏈夐檺锛岃€屼笖鏈枃绡囧箙涔熸尯澶т簡锛屾垜灏变笉杩涜浠嬬粛浜嗐€?/span>
鎬荤粨
瀛︿範璁捐妯″紡鐨勭洰鐨勬槸涓轰簡璁╂垜浠殑浠g爜鏇村姞鐨勪紭闆呫€佹槗缁存姢銆佹槗鎵╁睍銆傝繖娆℃暣鐞嗚繖绡囨枃绔狅紝璁╂垜閲嶆柊瀹¤浜嗕竴涓嬪悇涓璁℃ā寮忥紝瀵规垜鑷繁鑰岃█鏀惰幏杩樻槸鎸哄ぇ鐨勩€傛垜鎯筹紝鏂囩珷鐨勬渶澶ф敹鐩婅€呬竴鑸兘鏄綔鑰呮湰浜猴紝涓轰簡鍐欎竴绡囨枃绔狅紝闇€瑕佸珐鍥鸿嚜宸辩殑鐭ヨ瘑锛岄渶瑕佸鎵惧悇绉嶈祫鏂欙紝鑰屼笖锛岃嚜宸卞啓杩囩殑鎵嶆渶瀹规槗璁颁綇锛屼篃绠楁槸鎴戠粰璇昏€呯殑寤鸿鍚с€?/span>
Java鍚庣浜ゆ祦缇?/strong>宸叉垚绔?/strong>
鎺?/span>
鑽?/span>
闃?/span>
璇?nbsp;
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章
谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js
片段和活动之间的核心区别是啥?哪些代码可以写成片段?
VSCode自定义代码片段——.vue文件的模板
VSCode自定义代码片段6——CSS选择器
VSCode自定义代码片段——声明函数
VSCode自定义代码片段8——声明函数