涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?

Posted Java瀛︿範涔嬮亾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?相关的知识,希望对你有一定的参考价值。

鏉ユ簮: javadoop.com/post/design-pattern

搴?/span>

涓€鐩存兂鍐欎竴绡囦粙缁嶈璁℃ā寮忕殑鏂囩珷锛岃璇昏€呭彲浠ュ緢蹇湅瀹岋紝鑰屼笖涓€鐪嬪氨鎳傦紝鐪嬫噦灏变細鐢紝鍚屾椂涓嶄細灏嗗悇涓ā寮忔悶娣枫€傝嚜璁や负鏈枃杩樻槸鍐欏緱涓嶉敊鐨勷煒傪煒傪煒傦紝鑺变簡涓嶅皯蹇冩€濇潵鍐欒繖鏂囩珷鍜屽仛鍥撅紝鍔涙眰璁╄鑰呯湡鐨勮兘鐪嬬潃绠€鍗曞悓鏃舵湁鎵€鏀惰幏銆?/p>

璁捐妯″紡鏄澶у瀹為檯宸ヤ綔涓啓鐨勫悇绉嶄唬鐮佽繘琛岄珮灞傛鎶借薄鐨勬€荤粨锛屽叾涓渶鍑哄悕鐨勫綋灞?Gang of Four (GoF) 鐨勫垎绫讳簡锛屼粬浠皢璁捐妯″紡鍒嗙被涓?23 绉嶇粡鍏哥殑妯″紡锛屾牴鎹敤閫旀垜浠張鍙互鍒嗕负涓夊ぇ绫伙紝鍒嗗埆涓哄垱寤哄瀷妯″紡銆佺粨鏋勫瀷妯″紡鍜岃涓哄瀷妯″紡銆?/p>

鏈変竴浜涢噸瑕佺殑璁捐鍘熷垯鍦ㄥ紑绡囧拰澶у鍒嗕韩涓嬶紝杩欎簺鍘熷垯灏嗚疮閫氬叏鏂囷細

  1. 闈㈠悜鎺ュ彛缂栫▼锛岃€屼笉鏄潰鍚戝疄鐜般€傝繖涓緢閲嶈锛屼篃鏄紭闆呯殑銆佸彲鎵╁睍鐨勪唬鐮佺殑绗竴姝ワ紝杩欏氨涓嶉渶瑕佸璇翠簡鍚с€?
  2. 鑱岃矗鍗曚竴鍘熷垯銆傛瘡涓被閮藉簲璇ュ彧鏈変竴涓崟涓€鐨勫姛鑳斤紝骞朵笖璇ュ姛鑳藉簲璇ョ敱杩欎釜绫诲畬鍏ㄥ皝瑁呰捣鏉ャ€?
  3. 瀵逛慨鏀瑰叧闂紝瀵规墿灞曞紑鏀俱€傚淇敼鍏抽棴鏄锛屾垜浠緵杈涜嫤鑻﹀姞鐝啓鍑烘潵鐨勪唬鐮侊紝璇ュ疄鐜扮殑鍔熻兘鍜岃淇鐨?bug 閮藉畬鎴愪簡锛屽埆浜哄彲涓嶈兘璇存敼灏辨敼锛涘鎵╁睍寮€鏀惧氨姣旇緝濂界悊瑙d簡锛屼篃灏辨槸璇村湪鎴戜滑鍐欏ソ鐨勪唬鐮佸熀纭€涓婏紝寰堝鏄撳疄鐜版墿灞曘€?

銆屽垱寤哄瀷妯″紡姣旇緝绠€鍗曪紝浣嗘槸浼氭瘮杈冩病鏈夋剰鎬濓紝缁撴瀯鍨嬪拰琛屼负鍨嬫瘮杈冩湁鎰忔€濄€傘€?/strong>

鍒涘缓鍨嬫ā寮?/span>

鍒涘缓鍨嬫ā寮忕殑浣滅敤灏辨槸鍒涘缓瀵硅薄锛岃鍒板垱寤轰竴涓璞★紝鏈€鐔熸倝鐨勫氨鏄?new 涓€涓璞★紝鐒跺悗 set 鐩稿叧灞炴€с€備絾鏄紝鍦ㄥ緢澶氬満鏅笅锛屾垜浠渶瑕佺粰瀹㈡埛绔彁渚涙洿鍔犲弸濂界殑鍒涘缓瀵硅薄鐨勬柟寮忥紝灏ゅ叾鏄偅绉嶆垜浠畾涔変簡绫伙紝浣嗘槸闇€瑕佹彁渚涚粰鍏朵粬寮€鍙戣€呯敤鐨勬椂鍊欍€?/p>

绠€鍗曞伐鍘傛ā寮?/span>

鍜屽悕瀛椾竴鏍风畝鍗曪紝闈炲父绠€鍗曪紝鐩存帴涓婁唬鐮佸惂锛?/p>

public class FoodFactory {

    public static Food makeFood(String name) {
        if (name.equals("noodle")) {
            Food noodle = new LanZhouNoodle();
            noodle.addSpicy("more");
            return noodle;
        } else if (name.equals("chicken")) {
            Food chicken = new HuangMenChicken();
            chicken.addCondiment("potato");
            return chicken;
        } else {
            return null;
        }
    }
}

鍏朵腑锛孡anZhouNoodle 鍜?HuangMenChicken 閮界户鎵胯嚜 Food銆?/span>

绠€鍗曞湴璇达紝绠€鍗曞伐鍘傛ā寮忛€氬父灏辨槸杩欐牱锛屼竴涓伐鍘傜被 XxxFactory锛岄噷闈㈡湁涓€涓潤鎬佹柟娉曪紝鏍规嵁鎴戜滑涓嶅悓鐨勫弬鏁帮紝杩斿洖涓嶅悓鐨勬淳鐢熻嚜鍚屼竴涓埗绫伙紙鎴栧疄鐜板悓涓€鎺ュ彛锛夌殑瀹炰緥瀵硅薄銆?/p>

鉂?/span>

鎴戜滑寮鸿皟銆岃亴璐e崟涓€銆?/strong>鍘熷垯锛屼竴涓被鍙彁渚涗竴绉嶅姛鑳斤紝FoodFactory 鐨勫姛鑳藉氨鏄彧瑕佽礋璐g敓浜у悇绉?Food銆?/p> 鉂?/span>

宸ュ巶妯″紡

绠€鍗曞伐鍘傛ā寮忓緢绠€鍗曪紝濡傛灉瀹冭兘婊¤冻鎴戜滑鐨勯渶瑕侊紝鎴戣寰楀氨涓嶈鎶樿吘浜嗐€備箣鎵€浠ラ渶瑕佸紩鍏ュ伐鍘傛ā寮忥紝鏄洜涓烘垜浠線寰€闇€瑕佷娇鐢ㄤ袱涓垨涓や釜浠ヤ笂鐨勫伐鍘傘€?/p>

public interface FoodFactory {
    Food makeFood(String name);
}
public class ChineseFoodFactory implements FoodFactory {

    @Override
    public Food makeFood(String name) {
        if (name.equals("A")) {
            return new ChineseFoodA();
        } else if (name.equals("B")) {
            return new ChineseFoodB();
        } else {
            return null;
        }
    }
}
public class AmericanFoodFactory implements FoodFactory {

    @Override
    public Food makeFood(String name) {
        if (name.equals("A")) {
            return new AmericanFoodA();
        } else if (name.equals("B")) {
            return new AmericanFoodB();
        } else {
            return null;
        }
    }
}

鍏朵腑锛孋hineseFoodA銆丆hineseFoodB銆丄mericanFoodA銆丄mericanFoodB 閮芥淳鐢熻嚜 Food銆?/p>

瀹㈡埛绔皟鐢細

public class APP {
    public static void main(String[] args) {
        // 鍏堥€夋嫨涓€涓叿浣撶殑宸ュ巶
        FoodFactory factory = new ChineseFoodFactory();
        // 鐢辩涓€姝ョ殑宸ュ巶浜х敓鍏蜂綋鐨勫璞★紝涓嶅悓鐨勫伐鍘傞€犲嚭涓嶄竴鏍风殑瀵硅薄
        Food food = factory.makeFood("A");
    }
}

铏界劧閮芥槸璋冪敤 makeFood("A")  鍒朵綔 A 绫婚鐗╋紝浣嗘槸锛屼笉鍚岀殑宸ュ巶鐢熶骇鍑烘潵鐨勫畬鍏ㄤ笉涓€鏍枫€?/p>

绗竴姝ワ紝鎴戜滑闇€瑕侀€夊彇鍚堥€傜殑宸ュ巶锛岀劧鍚庣浜屾鍩烘湰涓婂拰绠€鍗曞伐鍘備竴鏍枫€?/p>

銆屾牳蹇冨湪浜庯紝鎴戜滑闇€瑕佸湪绗竴姝ラ€夊ソ鎴戜滑闇€瑕佺殑宸ュ巶銆?/strong>銆傛瘮濡傦紝鎴戜滑鏈?LogFactory 鎺ュ彛锛屽疄鐜扮被鏈?FileLogFactory 鍜?KafkaLogFactory锛屽垎鍒搴斿皢鏃ュ織鍐欏叆鏂囦欢鍜屽啓鍏?Kafka 涓紝鏄剧劧锛屾垜浠鎴风绗竴姝ュ氨闇€瑕佸喅瀹氬埌搴曡瀹炰緥鍖?FileLogFactory 杩樻槸 KafkaLogFactory锛岃繖灏嗗喅瀹氫箣鍚庣殑鎵€鏈夌殑鎿嶄綔銆?/p>

铏界劧绠€鍗曪紝涓嶈繃鎴戜篃鎶婃墍鏈夌殑鏋勪欢閮界敾鍒颁竴寮犲浘涓婏紝杩欐牱璇昏€呯湅鐫€姣旇緝娓呮櫚锛?/p>

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
factory-1

鎶借薄宸ュ巶妯″紡

褰撴秹鍙婂埌銆屼骇鍝佹棌銆?/strong>鐨勬椂鍊欙紝灏遍渶瑕佸紩鍏ユ娊璞″伐鍘傛ā寮忎簡銆?/p>

涓€涓粡鍏哥殑渚嬪瓙鏄€犱竴鍙扮數鑴戙€傛垜浠厛涓嶅紩鍏ユ娊璞″伐鍘傛ā寮忥紝鐪嬬湅鎬庝箞瀹炵幇銆?/p>

鍥犱负鐢佃剳鏄敱璁稿鐨勬瀯浠剁粍鎴愮殑锛屾垜浠皢 CPU 鍜屼富鏉胯繘琛屾娊璞★紝鐒跺悗 CPU 鐢?CPUFactory 鐢熶骇锛屼富鏉跨敱 MainBoardFactory 鐢熶骇锛岀劧鍚庯紝鎴戜滑鍐嶅皢 CPU 鍜屼富鏉挎惌閰嶈捣鏉ョ粍鍚堝湪涓€璧凤紝濡備笅鍥撅細

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
factory-1

杩欎釜鏃跺€欑殑瀹㈡埛绔皟鐢ㄦ槸杩欐牱鐨勶細

// 寰楀埌 Intel 鐨?nbsp;CPU
CPUFactory cpuFactory = new IntelCPUFactory();
CPU cpu = intelCPUFactory.makeCPU();

// 寰楀埌 AMD 鐨勪富鏉?br>MainBoardFactory mainBoardFactory = new AmdMainBoardFactory();
MainBoard mainBoard = mainBoardFactory.make();

// 缁勮 CPU 鍜屼富鏉?br>Computer computer = new Computer(cpu, mainBoard);

鍗曠嫭鐪?CPU 宸ュ巶鍜屼富鏉垮伐鍘傦紝瀹冧滑鍒嗗埆鏄墠闈㈡垜浠鐨?span class="mq-122">銆屽伐鍘傛ā寮忋€?/strong>銆傝繖绉嶆柟寮忎篃瀹规槗鎵╁睍锛屽洜涓鸿缁欑數鑴戝姞纭洏鐨勮瘽锛屽彧闇€瑕佸姞涓€涓?HardDiskFactory 鍜岀浉搴旂殑瀹炵幇鍗冲彲锛屼笉闇€瑕佷慨鏀圭幇鏈夌殑宸ュ巶銆?/p>

浣嗘槸锛岃繖绉嶆柟寮忔湁涓€涓棶棰橈紝閭e氨鏄鏋?銆孖ntel 瀹朵骇鐨?CPU 鍜?AMD 浜х殑涓绘澘涓嶈兘鍏煎浣跨敤銆?/strong>锛岄偅涔堣繖浠g爜灏卞鏄撳嚭閿欙紝鍥犱负瀹㈡埛绔苟涓嶇煡閬撳畠浠笉鍏煎锛屼篃灏变細閿欒鍦板嚭鐜伴殢鎰忕粍鍚堛€?/p>

涓嬮潰灏辨槸鎴戜滑瑕佽鐨?span class="mq-126">銆屼骇鍝佹棌銆?/strong>鐨勬蹇碉紝瀹冧唬琛ㄤ簡缁勬垚鏌愪釜浜у搧鐨勪竴绯诲垪闄勪欢鐨勯泦鍚堬細

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
abstract-factory-2

褰撴秹鍙婂埌杩欑浜у搧鏃忕殑闂鐨勬椂鍊欙紝灏遍渶瑕佹娊璞″伐鍘傛ā寮忔潵鏀寔浜嗐€傛垜浠笉鍐嶅畾涔?CPU 宸ュ巶銆佷富鏉垮伐鍘傘€佺‖鐩樺伐鍘傘€佹樉绀哄睆宸ュ巶绛夌瓑锛屾垜浠洿鎺ュ畾涔夌數鑴戝伐鍘傦紝姣忎釜鐢佃剳宸ュ巶璐熻矗鐢熶骇鎵€鏈夌殑璁惧锛岃繖鏍疯兘淇濊瘉鑲畾涓嶅瓨鍦ㄥ吋瀹归棶棰樸€?/p>

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
abstract-factory-3

杩欎釜鏃跺€欙紝瀵逛簬瀹㈡埛绔潵璇达紝涓嶅啀闇€瑕佸崟鐙寫閫?CPU鍘傚晢銆佷富鏉垮巶鍟嗐€佺‖鐩樺巶鍟嗙瓑锛岀洿鎺ラ€夋嫨涓€瀹跺搧鐗屽伐鍘傦紝鍝佺墝宸ュ巶浼氳礋璐g敓浜ф墍鏈夌殑涓滆タ锛岃€屼笖鑳戒繚璇佽偗瀹氭槸鍏煎鍙敤鐨勩€?/p>

public static void main(String[] args) {
    // 绗竴姝ュ氨瑕侀€夊畾涓€涓€滃ぇ鍘傗€?br>    ComputerFactory cf = new AmdFactory();
    // 浠庤繖涓ぇ鍘傞€?nbsp;CPU
    CPU cpu = cf.makeCPU();
    // 浠庤繖涓ぇ鍘傞€犱富鏉?br>    MainBoard board = cf.makeMainBoard();
      // 浠庤繖涓ぇ鍘傞€犵‖鐩?br>      HardDisk hardDisk = cf.makeHardDisk();

    // 灏嗗悓涓€涓巶瀛愬嚭鏉ョ殑 CPU銆佷富鏉裤€佺‖鐩樼粍瑁呭湪涓€璧?br>    Computer result = new Computer(cpu, board, hardDisk);
}

褰撶劧锛屾娊璞″伐鍘傜殑闂涔熸槸鏄捐€屾槗瑙佺殑锛屾瘮濡傛垜浠鍔犱釜鏄剧ず鍣紝灏遍渶瑕佷慨鏀规墍鏈夌殑宸ュ巶锛岀粰鎵€鏈夌殑宸ュ巶閮藉姞涓婂埗閫犳樉绀哄櫒鐨勬柟娉曘€傝繖鏈夌偣杩濆弽浜?span class="mq-141">銆屽淇敼鍏抽棴锛屽鎵╁睍寮€鏀俱€?/strong>杩欎釜璁捐鍘熷垯銆?/p>

鍗曚緥妯″紡

鍗曚緥妯″紡鐢ㄥ緱鏈€澶氾紝閿欏緱鏈€澶氥€?/p>

楗挎眽妯″紡鏈€绠€鍗曪細

public class Singleton {
    // 棣栧厛锛屽皢 new Singleton() 鍫垫
    private Singleton() {};
    // 鍒涘缓绉佹湁闈欐€佸疄渚嬶紝鎰忓懗鐫€杩欎釜绫荤涓€娆′娇鐢ㄧ殑鏃跺€欏氨浼氳繘琛屽垱寤?br>    private static Singleton instance = new Singleton();

    public static Singleton getInstance() {
        return instance;
    }
    // 鐬庡啓涓€涓潤鎬佹柟娉曘€傝繖閲屾兂璇寸殑鏄紝濡傛灉鎴戜滑鍙槸瑕佽皟鐢?Singleton.getDate(...)锛?br>    // 鏈潵鏄笉鎯宠鐢熸垚 Singleton 瀹炰緥鐨勶紝涓嶈繃娌″姙娉曪紝宸茬粡鐢熸垚浜?br>    public static Date getDate(String mode) {return new Date();}
}
鉂?/span>

寰堝浜洪兘鑳借鍑洪タ姹夋ā寮忕殑缂虹偣锛屽彲鏄垜瑙夊緱鐢熶骇杩囩▼涓紝寰堝皯纰板埌杩欑鎯呭喌锛氫綘瀹氫箟浜嗕竴涓崟渚嬬殑绫伙紝涓嶉渶瑕佸叾瀹炰緥锛屽彲鏄綘鍗存妸涓€涓垨鍑犱釜浣犱細鐢ㄥ埌鐨勯潤鎬佹柟娉曞鍒拌繖涓被涓€?/p> 鉂?/span>

楗辨眽妯″紡鏈€瀹规槗鍑洪敊锛?/p>

public class Singleton {
    // 棣栧厛锛屼篃鏄厛鍫垫 new Singleton() 杩欐潯璺?br>    private Singleton() {}
    // 鍜岄タ姹夋ā寮忕浉姣旓紝杩欒竟涓嶉渶瑕佸厛瀹炰緥鍖栧嚭鏉ワ紝娉ㄦ剰杩欓噷鐨?nbsp;volatile锛屽畠鏄繀椤荤殑
    private static volatile Singleton instance = null;

    public static Singleton getInstance() {
        if (instance == null) {
            // 鍔犻攣
            synchronized (Singleton.class) {
                // 杩欎竴娆″垽鏂篃鏄繀椤荤殑锛屼笉鐒朵細鏈夊苟鍙戦棶棰?br>                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
鉂?/span>

鍙岄噸妫€鏌ワ紝鎸囩殑鏄袱娆℃鏌?instance 鏄惁涓?null銆?/p>

volatile 鍦ㄨ繖閲屾槸闇€瑕佺殑锛屽笇鏈涜兘寮曡捣璇昏€呯殑鍏虫敞銆?/p>

寰堝浜轰笉鐭ラ亾鎬庝箞鍐欙紝鐩存帴灏卞湪 getInstance() 鏂规硶绛惧悕涓婂姞涓?synchronized锛岃繖灏变笉澶氳浜嗭紝鎬ц兘澶樊銆?/p> 鉂?/span>

宓屽绫绘渶缁忓吀锛屼互鍚庡ぇ瀹跺氨鐢ㄥ畠鍚э細

public class Singleton3 {

    private Singleton3() {}
    // 涓昏鏄娇鐢ㄤ簡 宓屽绫诲彲浠ヨ闂閮ㄧ被鐨勯潤鎬佸睘鎬у拰闈欐€佹柟娉?nbsp;鐨勭壒鎬?br>    private static class Holder {
        private static Singleton3 instance = new Singleton3();
    }
    public static Singleton3 getInstance() {
        return Holder.instance;
    }
}
鉂?/span>

娉ㄦ剰锛屽緢澶氫汉閮戒細鎶婅繖涓?span class="mq-191">銆屽祵濂楃被銆?/strong>璇存垚鏄?span class="mq-192">銆岄潤鎬佸唴閮ㄧ被銆?/strong>锛屼弗鏍煎湴璇达紝鍐呴儴绫诲拰宓屽绫绘槸涓嶄竴鏍风殑锛屽畠浠兘璁块棶鐨勫閮ㄧ被鏉冮檺涔熸槸涓嶄竴鏍风殑銆?/p> 鉂?/span>

鏈€鍚庯紝鎴戜滑璇翠竴涓嬫灇涓撅紝鏋氫妇寰堢壒娈婏紝瀹冨湪绫诲姞杞界殑鏃跺€欎細鍒濆鍖栭噷闈㈢殑鎵€鏈夌殑瀹炰緥锛岃€屼笖 JVM 淇濊瘉浜嗗畠浠笉浼氬啀琚疄渚嬪寲锛屾墍浠ュ畠澶╃敓灏辨槸鍗曚緥鐨勩€?/p>

铏界劧鎴戜滑骞虫椂寰堝皯鐪嬪埌鐢ㄦ灇涓炬潵瀹炵幇鍗曚緥锛屼絾鏄湪 RxJava 鐨勬簮鐮佷腑锛屾湁寰堝鍦版柟閮界敤浜嗘灇涓炬潵瀹炵幇鍗曚緥銆?/p>

寤洪€犺€呮ā寮?/span>

缁忓父纰拌鐨?XxxBuilder 鐨勭被锛岄€氬父閮芥槸寤洪€犺€呮ā寮忕殑浜х墿銆傚缓閫犺€呮ā寮忓叾瀹炴湁寰堝鐨勫彉绉嶏紝浣嗘槸瀵逛簬瀹㈡埛绔潵璇达紝鎴戜滑鐨勪娇鐢ㄩ€氬父閮芥槸涓€涓ā寮忕殑锛?/p>

Food food = new FoodBuilder().a().b().c().build();
Food food = Food.builder().a().b().c().build();

濂楄矾灏辨槸鍏?new 涓€涓?Builder锛岀劧鍚庡彲浠ラ摼寮忓湴璋冪敤涓€鍫嗘柟娉曪紝鏈€鍚庡啀璋冪敤涓€娆?build() 鏂规硶锛屾垜浠渶瑕佺殑瀵硅薄灏辨湁浜嗐€?/p>

鏉ヤ竴涓腑瑙勪腑鐭╃殑寤洪€犺€呮ā寮忥細

class User {
    // 涓嬮潰鏄€滀竴鍫嗏€濈殑灞炴€?br>    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锛岃繖涓笉涓€瀹氳鏈夛紝涓嶈繃鍐欒繖涓柟娉曟槸涓€涓緢濂界殑涔犳儻锛?br>    // 鏈変簺浠g爜瑕佹眰鍒汉鍐?nbsp;new User.UserBuilder().a()...build() 鐪嬩笂鍘诲氨娌¢偅涔堝ソ
    public static UserBuilder builder() {
        return new UserBuilder();
    }

    public static class UserBuilder {
        // 涓嬮潰鏄拰 User 涓€妯′竴鏍风殑涓€鍫嗗睘鎬?br>        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 涓€?br>        // 褰撶劧锛屽彲浠ュ湪 鈥滃鍒垛€?nbsp;涔嬪墠鍋氱偣妫€楠?br>        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-228">銆屽鍒躲€?/strong>缁欏疄闄呬骇鐢熺殑瀵硅薄銆?/p>

鐪嬬湅瀹㈡埛绔殑璋冪敤锛?/p>

public class APP {
    public static void main(String[] args) {
        User d = User.builder()
                .name("foo")
                .password("pAss12345")
                .age(25)
                .build();
    }
}

璇村疄璇濓紝寤洪€犺€呮ā寮忕殑銆岄摼寮忋€?/strong>鍐欐硶寰堝惛寮曚汉锛屼絾鏄紝澶氬啓浜嗗緢澶氣€滄棤鐢ㄢ€濈殑 builder 鐨勪唬鐮侊紝鎰熻杩欎釜妯″紡娌′粈涔堢敤銆備笉杩囷紝褰撳睘鎬у緢澶氾紝鑰屼笖鏈変簺蹇呭~锛屾湁浜涢€夊~鐨勬椂鍊欙紝杩欎釜妯″紡浼氫娇浠g爜娓呮櫚寰堝銆傛垜浠彲浠ュ湪 銆孊uilder 鐨勬瀯閫犳柟娉曘€?/strong>涓己鍒惰璋冪敤鑰呮彁渚涘繀濉瓧娈碉紝杩樻湁锛屽湪 build() 鏂规硶涓牎楠屽悇涓弬鏁版瘮鍦?User 鐨勬瀯閫犳柟娉曚腑鏍¢獙锛屼唬鐮佽浼橀泤涓€浜涖€?/p>

鉂?/span>

棰樺璇濓紝寮虹儓寤鸿璇昏€呬娇鐢?lombok锛岀敤浜?lombok 浠ュ悗锛屼笂闈㈢殑涓€澶у爢浠g爜浼氬彉鎴愬涓嬭繖鏍?

鉂?/span>
@Builder
class User {
    private String  name;
    private String password;
    private String nickName;
    private int age;
}
鉂?/span>

鎬庝箞鏍凤紝鐪佷笅鏉ョ殑鏃堕棿鏄笉鏄張鍙互骞茬偣鍒殑浜嗐€?/p> 鉂?/span>

褰撶劧锛屽鏋滀綘鍙槸鎯宠閾惧紡鍐欐硶锛屼笉鎯宠寤洪€犺€呮ā寮忥紝鏈変釜寰堢畝鍗曠殑鍔炴硶锛孶ser 鐨?getter 鏂规硶涓嶅彉锛屾墍鏈夌殑 setter 鏂规硶閮借鍏?銆宺eturn this銆?/strong> 灏卞彲浠ヤ簡锛岀劧鍚庡氨鍙互鍍忎笅闈㈣繖鏍疯皟鐢細

User user = new User().setName("").setPassword("").setAge(20);
鉂?/span>

寰堝浜烘槸杩欎箞鐢ㄧ殑锛屼絾鏄瑪鑰呰寰楀叾瀹炶繖绉嶅啓娉曢潪甯稿湴涓嶄紭闆咃紝涓嶆槸寰堟帹鑽愪娇鐢ㄣ€?/p> 鉂?/span>

鍘熷瀷妯″紡

杩欐槸鎴戣璇寸殑鍒涘缓鍨嬫ā寮忕殑鏈€鍚庝竴涓璁℃ā寮忎簡銆?/p>

鍘熷瀷妯″紡寰堢畝鍗曪細鏈変竴涓師鍨?span class="mq-267">銆屽疄渚嬨€?/strong>锛屽熀浜庤繖涓師鍨嬪疄渚嬩骇鐢熸柊鐨勫疄渚嬶紝涔熷氨鏄€滃厠闅嗏€濅簡銆?/p>

Object 绫讳腑鏈変竴涓?clone() 鏂规硶锛屽畠鐢ㄤ簬鐢熸垚涓€涓柊鐨勫璞★紝褰撶劧锛屽鏋滄垜浠璋冪敤杩欎釜鏂规硶锛宩ava 瑕佹眰鎴戜滑鐨勭被蹇呴』鍏?span class="mq-269">銆屽疄鐜?Cloneable 鎺ュ彛銆?/strong>锛屾鎺ュ彛娌℃湁瀹氫箟浠讳綍鏂规硶锛屼絾鏄笉杩欎箞鍋氱殑璇濓紝鍦?clone() 鐨勬椂鍊欙紝浼氭姏鍑?CloneNotSupportedException 寮傚父銆?/p>

protected native Object clone() throws CloneNotSupportedException;
鉂?/span>

java 鐨勫厠闅嗘槸娴呭厠闅嗭紝纰板埌瀵硅薄寮曠敤鐨勬椂鍊欙紝鍏嬮殕鍑烘潵鐨勫璞″拰鍘熷璞′腑鐨勫紩鐢ㄥ皢鎸囧悜鍚屼竴涓璞°€傞€氬父瀹炵幇娣卞厠闅嗙殑鏂规硶鏄皢瀵硅薄杩涜搴忓垪鍖栵紝鐒跺悗鍐嶈繘琛屽弽搴忓垪鍖栥€?/p> 鉂?/span>

鍘熷瀷妯″紡浜嗚В鍒拌繖閲屾垜瑙夊緱灏卞浜嗭紝鍚勭鍙樼潃娉曞瓙璇磋繖绉嶄唬鐮佹垨閭g浠g爜鏄師鍨嬫ā寮忥紝娌′粈涔堟剰涔夈€?/p>

鍒涘缓鍨嬫ā寮忔€荤粨

鍒涘缓鍨嬫ā寮忔€讳綋涓婃瘮杈冪畝鍗曪紝瀹冧滑鐨勪綔鐢ㄥ氨鏄负浜嗕骇鐢熷疄渚嬪璞★紝绠楁槸鍚勭宸ヤ綔鐨勭涓€姝ヤ簡锛屽洜涓烘垜浠啓鐨勬槸銆岄潰鍚戝璞°€?/strong>鐨勪唬鐮侊紝鎵€浠ユ垜浠涓€姝ュ綋鐒舵槸闇€瑕佸垱寤轰竴涓璞′簡銆?/p>

绠€鍗曞伐鍘傛ā寮忔渶绠€鍗曪紱宸ュ巶妯″紡鍦ㄧ畝鍗曞伐鍘傛ā寮忕殑鍩虹涓婂鍔犱簡閫夋嫨宸ュ巶鐨勭淮搴︼紝闇€瑕佺涓€姝ラ€夋嫨鍚堥€傜殑宸ュ巶锛涙娊璞″伐鍘傛ā寮忔湁浜у搧鏃忕殑姒傚康锛屽鏋滃悇涓骇鍝佹槸瀛樺湪鍏煎鎬ч棶棰樼殑锛屽氨瑕佺敤鎶借薄宸ュ巶妯″紡銆傚崟渚嬫ā寮忓氨涓嶈浜嗭紝涓轰簡淇濊瘉鍏ㄥ眬浣跨敤鐨勬槸鍚屼竴瀵硅薄锛屼竴鏂归潰鏄畨鍏ㄦ€ц€冭檻锛屼竴鏂归潰鏄负浜嗚妭鐪佽祫婧愶紱寤洪€犺€呮ā寮忎笓闂ㄥ浠樺睘鎬у緢澶氱殑閭g绫伙紝涓轰簡璁╀唬鐮佹洿浼樼編锛涘師鍨嬫ā寮忕敤寰楁渶灏戯紝浜嗚В鍜?Object 绫讳腑鐨?clone() 鏂规硶鐩稿叧鐨勭煡璇嗗嵆鍙€?/p>

缁撴瀯鍨嬫ā寮?/span>

鍓嶉潰鍒涘缓鍨嬫ā寮忎粙缁嶄簡鍒涘缓瀵硅薄鐨勪竴浜涜璁℃ā寮忥紝杩欒妭浠嬬粛鐨勭粨鏋勫瀷妯″紡鏃ㄥ湪閫氳繃鏀瑰彉浠g爜缁撴瀯鏉ヨ揪鍒拌В鑰︾殑鐩殑锛屼娇寰楁垜浠殑浠g爜瀹规槗缁存姢鍜屾墿灞曘€?/p>

浠g悊妯″紡

绗竴涓浠嬬粛鐨勪唬鐞嗘ā寮忔槸鏈€甯镐娇鐢ㄧ殑妯″紡涔嬩竴浜嗭紝鐢ㄤ竴涓唬鐞嗘潵闅愯棌鍏蜂綋瀹炵幇绫荤殑瀹炵幇缁嗚妭锛岄€氬父杩樼敤浜庡湪鐪熷疄鐨勫疄鐜扮殑鍓嶅悗娣诲姞涓€閮ㄥ垎閫昏緫銆?/p>

鏃㈢劧璇存槸銆屼唬鐞嗐€?/strong>锛岄偅灏辫瀵瑰鎴风闅愯棌鐪熷疄瀹炵幇锛岀敱浠g悊鏉ヨ礋璐e鎴风鐨勬墍鏈夎姹傘€傚綋鐒讹紝浠g悊鍙槸涓唬鐞嗭紝瀹冧笉浼氬畬鎴愬疄闄呯殑涓氬姟閫昏緫锛岃€屾槸涓€灞傜毊鑰屽凡锛屼絾鏄浜庡鎴风鏉ヨ锛屽畠蹇呴』琛ㄧ幇寰楀氨鏄鎴风闇€瑕佺殑鐪熷疄瀹炵幇銆?/p>

鉂?/span>

鐞嗚В銆屼唬鐞嗐€?/strong>杩欎釜璇嶏紝杩欎釜妯″紡鍏跺疄灏辩畝鍗曚簡銆?/p> 鉂?/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 {

    // 鍐呴儴涓€瀹氳鏈変竴涓湡瀹炵殑瀹炵幇绫伙紝褰撶劧涔熷彲浠ラ€氳繃鏋勯€犳柟娉曟敞鍏?br>    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();
涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
proxy

鎴戜滑鍙戠幇娌℃湁锛屼唬鐞嗘ā寮忚鐧戒簡灏辨槸鍋?銆屸€滄柟娉曞寘瑁呪€濄€?/strong> 鎴栧仛 銆屸€滄柟娉曞寮衡€濄€?/strong>銆傚湪闈㈠悜鍒囬潰缂栫▼涓紝鍏跺疄灏辨槸鍔ㄦ€佷唬鐞嗙殑杩囩▼銆傛瘮濡?Spring 涓紝鎴戜滑鑷繁涓嶅畾涔変唬鐞嗙被锛屼絾鏄?Spring 浼氬府鎴戜滑鍔ㄦ€佹潵瀹氫箟浠g悊锛岀劧鍚庢妸鎴戜滑瀹氫箟鍦?@Before銆丂After銆丂Around 涓殑浠g爜閫昏緫鍔ㄦ€佹坊鍔犲埌浠g悊涓€?/p>

閫傞厤鍣ㄦā寮?/span>

璇村畬浠g悊妯″紡锛岃閫傞厤鍣ㄦā寮忥紝鏄洜涓哄畠浠緢鐩镐技锛岃繖閲屽彲浠ュ仛涓瘮杈冦€?/p>

閫傞厤鍣ㄦā寮忓仛鐨勫氨鏄紝鏈変竴涓帴鍙i渶瑕佸疄鐜帮紝浣嗘槸鎴戜滑鐜版垚鐨勫璞¢兘涓嶆弧瓒筹紝闇€瑕佸姞涓€灞傞€傞厤鍣ㄦ潵杩涜閫傞厤銆?/p>

閫傞厤鍣ㄦā寮忔€讳綋鏉ヨ鍒嗕笁绉嶏細榛樿閫傞厤鍣ㄦā寮忋€佸璞¢€傞厤鍣ㄦā寮忋€佺被閫傞厤鍣ㄦā寮忋€傚厛涓嶆€ョ潃鍒嗘竻妤氳繖鍑犱釜锛屽厛鐪嬬湅渚嬪瓙鍐嶈銆?/p>

榛樿閫傞厤鍣ㄦā寮?/span>

棣栧厛锛屾垜浠厛鐪嬬湅鏈€绠€鍗曠殑閫傞厤鍣ㄦā寮?*榛樿閫傞厤鍣ㄦā寮?Default Adapter)**鏄€庝箞鏍风殑銆?/p>

鎴戜滑鐢?Appache commons-io 鍖呬腑鐨?FileAlterationListener 鍋氫緥瀛愶紝姝ゆ帴鍙e畾涔変簡寰堝鐨勬柟娉曪紝鐢ㄤ簬瀵规枃浠舵垨鏂囦欢澶硅繘琛岀洃鎺э紝涓€鏃﹀彂鐢熶簡瀵瑰簲鐨勬搷浣滐紝灏变細瑙﹀彂鐩稿簲鐨勬柟娉曘€?/p>

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-360">銆屾枃浠跺垱寤恒€?/strong>鍜?span class="mq-361">銆屾枃浠跺垹闄ゃ€?/strong>浜嬩欢锛屽彲鏄垜浠繕鏄笉寰椾笉瀹炵幇鎵€鏈夌殑鏂规硶锛屽緢鏄庢樉锛岃繖涓嶆槸鎴戜滑鎯宠鐨勩€?/p>

鎵€浠ワ紝鎴戜滑闇€瑕佷笅闈㈢殑涓€涓?span class="mq-363">銆岄€傞厤鍣ㄣ€?/strong>锛屽畠鐢ㄤ簬瀹炵幇涓婇潰鐨勬帴鍙o紝浣嗘槸銆屾墍鏈夌殑鏂规硶閮芥槸绌烘柟娉曘€?/strong>锛岃繖鏍凤紝鎴戜滑灏卞彲浠ヨ浆鑰屽畾涔夎嚜宸辩殑绫绘潵缁ф壙涓嬮潰杩欎釜绫诲嵆鍙€?/p>

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();
    }
}

褰撶劧锛屼笂闈㈣鐨勫彧鏄€傞厤鍣ㄦā寮忕殑鍏朵腑涓€绉嶏紝涔熸槸鏈€绠€鍗曠殑涓€绉嶏紝鏃犻渶澶氳█銆備笅闈紝鍐嶄粙缁?*鈥滄缁熺殑鈥?*閫傞厤鍣ㄦā寮忋€?/p>

瀵硅薄閫傞厤鍣ㄦā寮?/span>

鏉ョ湅涓€涓€奌ead First 璁捐妯″紡銆嬩腑鐨勪竴涓緥瀛愶紝鎴戠◢寰慨鏀逛簡涓€涓嬶紝鐪嬬湅鎬庝箞灏嗛浮閫傞厤鎴愰腑锛岃繖鏍烽浮涔熻兘褰撻腑鏉ョ敤銆傚洜涓猴紝鐜板湪楦繖涓帴鍙o紝鎴戜滑娌℃湁鍚堥€傜殑瀹炵幇绫诲彲浠ョ敤锛屾墍浠ラ渶瑕侀€傞厤鍣ㄣ€?/p>

public interface Duck {
    public void quack(); // 楦殑鍛卞懕鍙?br>    public void fly(); // 椋?br>}

public interface Cock {
    public void gobble(); // 楦$殑鍜曞挄鍙?br>    public void fly(); // 椋?br>}

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;
    }

    // 瀹炵幇楦殑鍛卞懕鍙柟娉?br>    @Override
      public void quack() {
        // 鍐呴儴鍏跺疄鏄竴鍙浮鐨勫挄鍜曞彨
        cock.gobble();
    }

      @Override
      public void fly() {
        cock.fly();
    }
}

瀹㈡埛绔皟鐢ㄥ緢绠€鍗曚簡锛?/p>

public static void main(String[] args) {
    // 鏈変竴鍙噹楦?br>      Cock wildCock = new WildCock();
      // 鎴愬姛灏嗛噹楦¢€傞厤鎴愰腑
      Duck duck = new CockAdapter(wildCock);
      ...
}

鍒拌繖閲岋紝澶у涔熷氨鐭ラ亾浜嗛€傞厤鍣ㄦā寮忔槸鎬庝箞鍥炰簨浜嗐€傛棤闈炴槸鎴戜滑闇€瑕佷竴鍙腑锛屼絾鏄垜浠彧鏈変竴鍙浮锛岃繖涓椂鍊欏氨闇€瑕佸畾涔変竴涓€傞厤鍣紝鐢辫繖涓€傞厤鍣ㄦ潵鍏呭綋楦紝浣嗘槸閫傞厤鍣ㄩ噷闈㈢殑鏂规硶杩樻槸鐢遍浮鏉ュ疄鐜扮殑銆?/p>

鎴戜滑鐢ㄤ竴涓浘鏉ョ畝鍗曡鏄庝笅锛?/p>

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
adapter-1

涓婂浘搴旇杩樻槸寰堝鏄撶悊瑙g殑锛屾垜灏变笉鍋氭洿澶氱殑瑙i噴浜嗐€備笅闈紝鎴戜滑鐪嬬湅绫婚€傞厤妯″紡鎬庝箞鏍风殑銆?/p>

绫婚€傞厤鍣ㄦā寮?/span>

搴熻瘽灏戣锛岀洿鎺ヤ笂鍥撅細

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
adapter-1

鐪嬪埌杩欎釜鍥撅紝澶у搴旇寰堝鏄撶悊瑙g殑鍚э紝閫氳繃缁ф壙鐨勬柟娉曪紝閫傞厤鍣ㄨ嚜鍔ㄨ幏寰椾簡鎵€闇€瑕佺殑澶ч儴鍒嗘柟娉曘€傝繖涓椂鍊欙紝瀹㈡埛绔娇鐢ㄦ洿鍔犵畝鍗曪紝鐩存帴 Target t = new SomeAdapter(); 灏卞彲浠ヤ簡銆?/p>

閫傞厤鍣ㄦā寮忔€荤粨

  1. 绫婚€傞厤鍜屽璞¢€傞厤鐨勫紓鍚?
    鉂?/span>

    涓€涓噰鐢ㄧ户鎵匡紝涓€涓噰鐢ㄧ粍鍚堬紱

    绫婚€傞厤灞炰簬闈欐€佸疄鐜帮紝瀵硅薄閫傞厤灞炰簬缁勫悎鐨勫姩鎬佸疄鐜帮紝瀵硅薄閫傞厤闇€瑕佸瀹炰緥鍖栦竴涓璞°€?/p>

    鎬讳綋鏉ヨ锛屽璞¢€傞厤鐢ㄥ緱姣旇緝澶氥€?/p> 鉂?/span>

  2. 閫傞厤鍣ㄦā寮忓拰浠g悊妯″紡鐨勫紓鍚?姣旇緝杩欎袱绉嶆ā寮忥紝鍏跺疄鏄瘮杈冨璞¢€傞厤鍣ㄦā寮忓拰浠g悊妯″紡锛屽湪浠g爜缁撴瀯涓婏紝瀹冧滑寰堢浉浼硷紝閮介渶瑕佷竴涓叿浣撶殑瀹炵幇绫荤殑瀹炰緥銆備絾鏄畠浠殑鐩殑涓嶄竴鏍凤紝浠g悊妯″紡鍋氱殑鏄寮哄師鏂规硶鐨勬椿锛涢€傞厤鍣ㄥ仛鐨勬槸閫傞厤鐨勬椿锛屼负鐨勬槸鎻愪緵鈥滄妸楦″寘瑁呮垚楦紝鐒跺悗褰撳仛楦潵浣跨敤鈥濓紝鑰岄浮鍜岄腑瀹冧滑涔嬮棿鍘熸湰娌℃湁缁ф壙鍏崇郴銆? 涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?

妗ユ妯″紡

鐞嗚В妗ユ妯″紡锛屽叾瀹炲氨鏄悊瑙d唬鐮佹娊璞″拰瑙h€︺€?/p>

鎴戜滑棣栧厛闇€瑕佷竴涓ˉ姊侊紝瀹冩槸涓€涓帴鍙o紝瀹氫箟鎻愪緵鐨勬帴鍙f柟娉曘€?/p>

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锛?/p>

public abstract class Shape {
    protected DrawAPI drawAPI;
    protected Shape(DrawAPI drawAPI) {
        this.drawAPI = drawAPI;
    }
    public abstract void draw();
}

瀹氫箟鎶借薄绫荤殑瀛愮被锛?/p>

// 鍦嗗舰
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);
    }
}
// 闀挎柟褰?br>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();
}

鍙兘澶у鐪嬩笂闈竴姝ユ杩樹笉鏄壒鍒竻鏅帮紝鎴戞妸鎵€鏈夌殑涓滆タ鏁村悎鍒颁竴寮犲浘涓婏細

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
bridge-1

杩欏洖澶у搴旇灏辩煡閬撴娊璞″湪鍝噷锛屾€庝箞瑙h€︿簡鍚с€傛ˉ姊佹ā寮忕殑浼樼偣涔熸槸鏄捐€屾槗瑙佺殑锛屽氨鏄潪甯稿鏄撹繘琛屾墿灞曘€?/p>

鉂?/span>

鏈妭寮曠敤浜嗚繖閲岀殑渚嬪瓙锛屽苟瀵瑰叾杩涜浜嗕慨鏀广€?/p> 鉂?/span>

瑁呴グ妯″紡

瑕佹妸瑁呴グ妯″紡璇存竻妤氭槑鐧斤紝涓嶆槸浠跺鏄撶殑浜嬫儏銆備篃璁歌鑰呯煡閬?銆孞ava IO銆?/strong> 涓殑鍑犱釜绫绘槸鍏稿瀷鐨勮楗版ā寮忕殑搴旂敤锛屼絾鏄鑰呬笉涓€瀹氭竻妤氬叾涓殑鍏崇郴锛屼篃璁哥湅瀹屽氨蹇樹簡锛屽笇鏈涚湅瀹岃繖鑺傚悗锛岃鑰呭彲浠ュ鍏舵湁鏇存繁鐨勬劅鎮熴€?/p>

棣栧厛锛屾垜浠厛鐪嬩竴涓畝鍗曠殑鍥撅紝鐪嬭繖涓浘鐨勬椂鍊欙紝浜嗚В涓嬪眰娆$粨鏋勫氨鍙互浜嗭細

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
decorator-1

鎴戜滑鏉ヨ璇磋楗版ā寮忕殑鍑哄彂鐐癸紝浠庡浘涓彲浠ョ湅鍒帮紝鎺ュ彛 Component 鍏跺疄宸茬粡鏈変簡 ConcreteComponentA 鍜?ConcreteComponentB 涓や釜瀹炵幇绫讳簡锛屼絾鏄紝濡傛灉鎴戜滑瑕?span class="mq-499">銆屽寮恒€?/strong>杩欎袱涓疄鐜扮被鐨勮瘽锛屾垜浠氨鍙互閲囩敤瑁呴グ妯″紡锛岀敤鍏蜂綋鐨勮楗板櫒鏉?span class="mq-500">銆岃楗般€?/strong>瀹炵幇绫伙紝浠ヨ揪鍒板寮虹殑鐩殑銆?/p>

鉂?/span>

浠庡悕瀛楁潵绠€鍗曡В閲婁笅瑁呴グ鍣ㄣ€傛棦鐒惰鏄楗帮紝閭d箞寰€寰€灏辨槸銆屾坊鍔犲皬鍔熻兘銆?/strong>杩欑锛岃€屼笖锛屾垜浠婊¤冻鍙互娣诲姞澶氫釜灏忓姛鑳姐€傛渶绠€鍗曠殑锛屼唬鐞嗘ā寮忓氨鍙互瀹炵幇鍔熻兘鐨勫寮猴紝浣嗘槸浠g悊涓嶅鏄撳疄鐜板涓姛鑳界殑澧炲己锛屽綋鐒朵綘鍙互璇寸敤浠g悊鍖呰浠g悊鐨勫灞傚寘瑁呮柟寮忥紝浣嗘槸閭f牱鐨勮瘽浠g爜灏卞鏉備簡銆?/p> 鉂?/span>

棣栧厛鏄庣櫧涓€浜涚畝鍗曠殑姒傚康锛屼粠鍥句腑鎴戜滑鐪嬪埌锛屾墍鏈夌殑鍏蜂綋瑁呴グ鑰呬滑 銆孋oncreteDecorator銆?/strong>* 閮藉彲浠ヤ綔涓?Component 鏉ヤ娇鐢紝鍥犱负瀹冧滑閮藉疄鐜颁簡 Component 涓殑鎵€鏈夋帴鍙c€傚畠浠拰 Component 瀹炵幇绫?ConcreteComponent* 鐨勫尯鍒槸锛屽畠浠彧鏄楗拌€咃紝璧?span class="mq-508">銆岃楗般€?/strong>浣滅敤锛屼篃灏辨槸鍗充娇瀹冧滑鐪嬩笂鍘荤墰閫艰桨杞帮紝浣嗘槸瀹冧滑閮藉彧鏄湪鍏蜂綋鐨勫疄鐜颁腑銆屽姞浜嗗眰鐨潵瑁呴グ銆?/strong>鑰屽凡銆?/p>

鉂?/span>

娉ㄦ剰杩欐璇濅腑娣锋潅鍦ㄥ悇涓悕璇嶄腑鐨?Component 鍜?Decorator锛屽埆鎼炴贩浜嗐€?/p> 鉂?/span>

涓嬮潰鏉ョ湅鐪嬩竴涓緥瀛愶紝鍏堟妸瑁呴グ妯″紡寮勬竻妤氾紝鐒跺悗鍐嶄粙缁嶄笅 java io 涓殑瑁呴グ妯″紡鐨勫簲鐢ㄣ€?/p>

鏈€杩戝ぇ琛椾笂娴佽璧锋潵浜嗏€滃揩涔愭煚妾€濓紝鎴戜滑鎶婂揩涔愭煚妾殑楗枡鍒嗕负涓夌被锛氱孩鑼躲€佺豢鑼躲€佸挅鍟★紝鍦ㄨ繖涓夊ぇ绫荤殑鍩虹涓婏紝鍙堝鍔犱簡璁稿鐨勫彛鍛筹紝浠€涔堥噾妗旀煚妾孩鑼躲€侀噾妗旀煚妾弽鐝犵豢鑼躲€佽姃鏋滅孩鑼躲€佽姃鏋滅豢鑼躲€佽姃鏋滅弽鐝犵孩鑼躲€佺儰鐝嶇彔绾㈣尪銆佺儰鐝嶇彔鑺掓灉缁胯尪銆佹ぐ棣欒儦鑺藉挅鍟°€佺劍绯栧彲鍙挅鍟$瓑绛夛紝姣忓搴楅兘鏈夊緢闀跨殑鑿滃崟锛屼絾鏄粩缁嗙湅涓嬶紝鍏跺疄鍘熸枡涔熸病鍑犳牱锛屼絾鏄彲浠ユ惌閰嶅嚭寰堝缁勫悎锛屽鏋滈【瀹㈤渶瑕侊紝寰堝娌″嚭鐜板湪鑿滃崟涓殑楗枡浠栦滑涔熸槸鍙互鍋氱殑銆?/p>

鍦ㄨ繖涓緥瀛愪腑锛岀孩鑼躲€佺豢鑼躲€佸挅鍟℃槸鏈€鍩虹鐨勯ギ鏂欙紝鍏朵粬鐨勫儚閲戞鏌犳銆佽姃鏋溿€佺弽鐝犮€佹ぐ鏋溿€佺劍绯栫瓑閮藉睘浜庤楗扮敤鐨勩€傚綋鐒讹紝鍦ㄥ紑鍙戜腑锛屾垜浠‘瀹炲彲浠ュ儚闂ㄥ簵涓€鏍凤紝寮€鍙戣繖浜涚被锛歀emonBlackTea銆丩emonGreenTea銆丮angoBlackTea銆丮angoLemonGreenTea......浣嗘槸锛屽緢蹇垜浠氨鍙戠幇锛岃繖鏍峰瓙骞茶偗瀹氭槸涓嶈鐨勶紝杩欎細瀵艰嚧鎴戜滑闇€瑕佺粍鍚堝嚭鎵€鏈夌殑鍙兘锛岃€屼笖濡傛灉瀹汉闇€瑕佸湪绾㈣尪涓姞鍙屼唤鏌犳鎬庝箞鍔烇紵涓変唤鏌犳鎬庝箞鍔烇紵

涓嶈搴熻瘽浜嗭紝涓婁唬鐮併€?/p>

棣栧厛锛屽畾涔夐ギ鏂欐娊璞″熀绫伙細

public abstract class Beverage {
      // 杩斿洖鎻忚堪
      public abstract String getDescription();
      // 杩斿洖浠锋牸
      public abstract double cost();
}

鐒跺悗鏄笁涓熀纭€楗枡瀹炵幇绫伙紝绾㈣尪銆佺豢鑼跺拰鍜栧暋锛?/p>

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锛?/p>

// 璋冩枡
public abstract class Condiment extends Beverage {

}

鐒跺悗鎴戜滑鏉ュ畾涔夋煚妾€佽姃鏋滅瓑鍏蜂綋鐨勮皟鏂欙紝瀹冧滑灞炰簬瑁呴グ鑰咃紝姣棤鐤戦棶锛岃繖浜涜皟鏂欒偗瀹氶兘闇€瑕佺户鎵胯皟鏂?Condiment 绫伙細

public class Lemon extends Condiment {
    private Beverage bevarage;
    // 杩欓噷寰堝叧閿紝闇€瑕佷紶鍏ュ叿浣撶殑楗枡锛屽闇€瑕佷紶鍏ユ病鏈夎瑁呴グ鐨勭孩鑼舵垨缁胯尪锛?br>    // 褰撶劧涔熷彲浠ヤ紶鍏ュ凡缁忚楗板ソ鐨勮姃鏋滅豢鑼讹紝杩欐牱鍙互鍋氳姃鏋滄煚妾豢鑼?br>    public Lemon(Beverage bevarage) {
        this.bevarage = bevarage;
    }
    public String getDescription() {
        // 瑁呴グ
        return bevarage.getDescription() + ", 鍔犳煚妾?;
    }
    public double cost() {
        // 瑁呴グ
        return beverage.cost() + 2; // 鍔犳煚妾渶瑕?nbsp;2 鍏?br>    }
}

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 鍏?br>    }
}
...// 缁欐瘡涓€绉嶈皟鏂欓兘鍔犱竴涓被

鐪嬪鎴风璋冪敤锛?/p>

public static void main(String[] args) {
    // 棣栧厛锛屾垜浠渶瑕佷竴涓熀纭€楗枡锛岀孩鑼躲€佺豢鑼舵垨鍜栧暋
    Beverage beverage = new GreenTea();
    // 寮€濮嬭楗?br>    beverage = new Lemon(beverage); // 鍏堝姞涓€浠芥煚妾?br>    beverage = new Mongo(beverage); // 鍐嶅姞涓€浠借姃鏋?br>
    System.out.println(beverage.getDescription() + " 浠锋牸锛氾骏" + beverage.cost());
    //"缁胯尪, 鍔犳煚妾? 鍔犺姃鏋?nbsp;浠锋牸锛氾骏16"
}

濡傛灉鎴戜滑闇€瑕?銆岃姃鏋?鐝嶇彔-鍙屼唤鏌犳-绾㈣尪銆?/strong>锛?/p>

Beverage beverage = new Mongo(new Pearl(new Lemon(new Lemon(new BlackTea()))));

鏄笉鏄緢鍙樻€侊紵

鐪嬬湅涓嬪浘鍙兘浼氭竻鏅颁竴浜涳細

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
decorator-2

鍒拌繖閲岋紝澶у搴旇宸茬粡娓呮瑁呴グ妯″紡浜嗗惂銆?/p>

涓嬮潰锛屾垜浠啀鏉ヨ璇?java IO 涓殑瑁呴グ妯″紡銆傜湅涓嬪浘 InputStream 娲剧敓鍑烘潵鐨勯儴鍒嗙被锛?/p>

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
decorator-3

鎴戜滑鐭ラ亾 InputStream 浠h〃浜嗚緭鍏ユ祦锛屽叿浣撶殑杈撳叆鏉ユ簮鍙互鏄枃浠讹紙FileInputStream锛夈€佺閬擄紙PipedInputStream锛夈€佹暟缁勶紙ByteArrayInputStream锛夌瓑锛岃繖浜涘氨鍍忓墠闈㈠ザ鑼剁殑渚嬪瓙涓殑绾㈣尪銆佺豢鑼讹紝灞炰簬鍩虹杈撳叆娴併€?/p>

FilterInputStream 鎵挎帴浜嗚楗版ā寮忕殑鍏抽敭鑺傜偣锛屽畠鐨勫疄鐜扮被鏄竴绯诲垪瑁呴グ鍣紝姣斿 BufferedInputStream 浠h〃鐢ㄧ紦鍐叉潵瑁呴グ锛屼篃灏变娇寰楄緭鍏ユ祦鍏锋湁浜嗙紦鍐茬殑鍔熻兘锛孡ineNumberInputStream 浠h〃鐢ㄨ鍙锋潵瑁呴グ锛屽湪鎿嶄綔鐨勬椂鍊欏氨鍙互鍙栧緱琛屽彿浜嗭紝DataInputStream 鐨勮楗帮紝浣垮緱鎴戜滑鍙互浠庤緭鍏ユ祦杞崲涓?java 涓殑鍩烘湰绫诲瀷鍊笺€?/p>

褰撶劧锛屽湪 java IO 涓紝濡傛灉鎴戜滑浣跨敤瑁呴グ鍣ㄧ殑璇濓紝灏变笉澶€傚悎闈㈠悜鎺ュ彛缂栫▼浜嗭紝濡傦細

InputStream inputStream = new LineNumberInputStream(new BufferedInputStream(new FileInputStream("")));

杩欐牱鐨勭粨鏋滄槸锛孖nputStream 杩樻槸涓嶅叿鏈夎鍙栬鍙风殑鍔熻兘锛屽洜涓鸿鍙栬鍙风殑鏂规硶瀹氫箟鍦?LineNumberInputStream 绫讳腑銆?/p>

鎴戜滑搴旇鍍忎笅闈㈣繖鏍蜂娇鐢細

DataInputStream is = new DataInputStream(
                              new BufferedInputStream(
                                  new FileInputStream("")));
鉂?/span>

鎵€浠ヨ鍢涳紝瑕佹壘鍒扮函鐨勪弗鏍肩鍚堣璁℃ā寮忕殑浠g爜杩樻槸姣旇緝闅剧殑銆?/p> 鉂?/span>

闂ㄩ潰妯″紡

闂ㄩ潰妯″紡锛堜篃鍙瑙傛ā寮忥紝Facade Pattern锛夊湪璁稿婧愮爜涓湁浣跨敤锛屾瘮濡?slf4j 灏卞彲浠ョ悊瑙d负鏄棬闈㈡ā寮忕殑搴旂敤銆傝繖鏄竴涓畝鍗曠殑璁捐妯″紡锛屾垜浠洿鎺ヤ笂浠g爜鍐嶈鍚с€?/p>

棣栧厛锛屾垜浠畾涔変竴涓帴鍙o細

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) {
    // 鐢讳竴涓渾褰?br>      Shape circle = new Circle();
      circle.draw();

      // 鐢讳竴涓暱鏂瑰舰
      Shape rectangle = new Rectangle();
      rectangle.draw();
}

浠ヤ笂鏄垜浠父鍐欑殑浠g爜锛屾垜浠渶瑕佺敾鍦嗗氨瑕佸厛瀹炰緥鍖栧渾锛岀敾闀挎柟褰㈠氨闇€瑕佸厛瀹炰緥鍖栦竴涓暱鏂瑰舰锛岀劧鍚庡啀璋冪敤鐩稿簲鐨?draw() 鏂规硶銆?/p>

涓嬮潰锛屾垜浠湅鐪嬫€庝箞鐢ㄩ棬闈㈡ā寮忔潵璁╁鎴风璋冪敤鏇村姞鍙嬪ソ涓€浜涖€?/p>

鎴戜滑鍏堝畾涔変竴涓棬闈細

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();
   }
}

鐪嬬湅鐜板湪瀹㈡埛绔€庝箞璋冪敤锛?/p>

public static void main(String[] args) {
  ShapeMaker shapeMaker = new ShapeMaker();

  // 瀹㈡埛绔皟鐢ㄧ幇鍦ㄦ洿鍔犳竻鏅颁簡
  shapeMaker.drawCircle();
  shapeMaker.drawRectangle();
  shapeMaker.drawSquare();        
}

闂ㄩ潰妯″紡鐨勪紭鐐规樉鑰屾槗瑙侊紝瀹㈡埛绔笉鍐嶉渶瑕佸叧娉ㄥ疄渚嬪寲鏃跺簲璇ヤ娇鐢ㄥ摢涓疄鐜扮被锛岀洿鎺ヨ皟鐢ㄩ棬闈㈡彁渚涚殑鏂规硶灏卞彲浠ヤ簡锛屽洜涓洪棬闈㈢被鎻愪緵鐨勬柟娉曠殑鏂规硶鍚嶅浜庡鎴风鏉ヨ宸茬粡寰堝弸濂戒簡銆?/p>

缁勫悎妯″紡

缁勫悎妯″紡鐢ㄤ簬琛ㄧず鍏锋湁灞傛缁撴瀯鐨勬暟鎹紝浣垮緱鎴戜滑瀵瑰崟涓璞″拰缁勫悎瀵硅薄鐨勮闂叿鏈変竴鑷存€с€?/p>

鐩存帴鐪嬩竴涓緥瀛愬惂锛屾瘡涓憳宸ラ兘鏈夊鍚嶃€侀儴闂ㄣ€佽柂姘磋繖浜涘睘鎬э紝鍚屾椂杩樻湁涓嬪睘鍛樺伐闆嗗悎锛堣櫧鐒跺彲鑳介泦鍚堜负绌猴級锛岃€屼笅灞炲憳宸ュ拰鑷繁鐨勭粨鏋勬槸涓€鏍风殑锛屼篃鏈夊鍚嶃€侀儴闂ㄨ繖浜涘睘鎬э紝鍚屾椂涔熸湁浠栦滑鐨勪笅灞炲憳宸ラ泦鍚堛€?/p>

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() 杩欎簺鏂规硶銆?/p>

杩欒鐨勫叾瀹炲氨鏄粍鍚堟ā寮忥紝杩欑绠€鍗曠殑妯″紡鎴戝氨涓嶅仛杩囧浠嬬粛浜嗭紝鐩镐俊鍚勪綅璇昏€呬篃涓嶅枩娆㈢湅鎴戝啓搴熻瘽銆?/p>

浜厓妯″紡

鑻辨枃鏄?Flyweight Pattern锛屼笉鐭ラ亾鏄皝鏈€鍏堢炕璇戠殑杩欎釜璇嶏紝鎰熻杩欑炕璇戠湡鐨勪笉濂界悊瑙o紝鎴戜滑璇曠潃寮鸿鍏宠仈璧锋潵鍚с€侳lyweight 鏄交閲忕骇鐨勬剰鎬濓紝浜厓鍒嗗紑鏉ヨ灏辨槸 鍏变韩 鍏冨櫒浠讹紝涔熷氨鏄鐢ㄥ凡缁忕敓鎴愮殑瀵硅薄锛岃繖绉嶅仛娉曞綋鐒朵篃灏辨槸杞婚噺绾х殑浜嗐€?/p>

澶嶇敤瀵硅薄鏈€绠€鍗曠殑鏂瑰紡鏄紝鐢ㄤ竴涓?HashMap 鏉ュ瓨鏀炬瘡娆℃柊鐢熸垚鐨勫璞°€傛瘡娆¢渶瑕佷竴涓璞$殑鏃跺€欙紝鍏堝埌 HashMap 涓湅鐪嬫湁娌℃湁锛屽鏋滄病鏈夛紝鍐嶇敓鎴愭柊鐨勫璞★紝鐒跺悗灏嗚繖涓璞℃斁鍏?HashMap 涓€?/p>

杩欑绠€鍗曠殑浠g爜鎴戝氨涓嶆紨绀轰簡銆?/p>

缁撴瀯鍨嬫ā寮忔€荤粨

鍓嶉潰锛屾垜浠浜嗕唬鐞嗘ā寮忋€侀€傞厤鍣ㄦā寮忋€佹ˉ姊佹ā寮忋€佽楗版ā寮忋€侀棬闈㈡ā寮忋€佺粍鍚堟ā寮忓拰浜厓妯″紡銆傝鑰呮槸鍚﹀彲浠ュ垎鍒妸杩欏嚑涓ā寮忚娓呮浜嗗憿锛熷湪璇村埌杩欎簺妯″紡鐨勬椂鍊欙紝蹇冧腑鏄惁鏈変竴涓竻鏅扮殑鍥炬垨澶勭悊娴佺▼鍦ㄨ剳娴烽噷鍛紵

浠g悊妯″紡鏄仛鏂规硶澧炲己鐨勶紝閫傞厤鍣ㄦā寮忔槸鎶婇浮鍖呰鎴愰腑杩欑鐢ㄦ潵閫傞厤鎺ュ彛鐨勶紝妗ユ妯″紡鍋氬埌浜嗗緢濂界殑瑙h€︼紝瑁呴グ妯″紡浠庡悕瀛椾笂灏辩湅寰楀嚭鏉ワ紝閫傚悎浜庤楗扮被鎴栬€呰鏄寮虹被鐨勫満鏅紝闂ㄩ潰妯″紡鐨勪紭鐐规槸瀹㈡埛绔笉闇€瑕佸叧蹇冨疄渚嬪寲杩囩▼锛屽彧瑕佽皟鐢ㄩ渶瑕佺殑鏂规硶鍗冲彲锛岀粍鍚堟ā寮忕敤浜庢弿杩板叿鏈夊眰娆$粨鏋勭殑鏁版嵁锛屼韩鍏冩ā寮忔槸涓轰簡鍦ㄧ壒瀹氱殑鍦烘櫙涓紦瀛樺凡缁忓垱寤虹殑瀵硅薄锛岀敤浜庢彁楂樻€ц兘銆?/p>

琛屼负鍨嬫ā寮?/span>

琛屼负鍨嬫ā寮忓叧娉ㄧ殑鏄悇涓被涔嬮棿鐨勭浉浜掍綔鐢紝灏嗚亴璐e垝鍒嗘竻妤氾紝浣垮緱鎴戜滑鐨勪唬鐮佹洿鍔犲湴娓呮櫚銆?/p>

绛栫暐妯″紡

绛栫暐妯″紡澶父鐢ㄤ簡锛屾墍浠ユ妸瀹冩斁鍒版渶鍓嶉潰杩涜浠嬬粛銆傚畠姣旇緝绠€鍗曪紝鎴戝氨涓嶅簾璇濓紝鐩存帴鐢ㄤ唬鐮佽浜嬪惂銆?/p>

涓嬮潰璁捐鐨勫満鏅槸锛屾垜浠渶瑕佺敾涓€涓浘褰紝鍙€夌殑绛栫暐灏辨槸鐢ㄧ孩鑹茬瑪鏉ョ敾锛岃繕鏄豢鑹茬瑪鏉ョ敾锛屾垨鑰呰摑鑹茬瑪鏉ョ敾銆?/p>

棣栧厛锛屽厛瀹氫箟涓€涓瓥鐣ユ帴鍙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()); // 浣跨敤缁胯壊绗旀潵鐢?br>      context.executeDraw(10, 0, 0);
}

鏀惧埌涓€寮犲浘涓婏紝璁╁ぇ瀹剁湅寰楁竻鏅颁簺锛?/p>

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
strategy-1

杩欎釜鏃跺€欙紝澶у鏈夋病鏈夎仈鎯冲埌缁撴瀯鍨嬫ā寮忎腑鐨勬ˉ姊佹ā寮忥紝瀹冧滑鍏跺疄闈炲父鐩镐技锛屾垜鎶婃ˉ姊佹ā寮忕殑鍥炬嬁杩囨潵澶у瀵规瘮涓嬶細

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?
bridge-1

瑕佹垜璇寸殑璇濓紝瀹冧滑闈炲父鐩镐技锛屾ˉ姊佹ā寮忓湪宸︿晶鍔犱簡涓€灞傛娊璞¤€屽凡銆傛ˉ姊佹ā寮忕殑鑰﹀悎鏇翠綆锛岀粨鏋勬洿澶嶆潅涓€浜涖€?/p>

瑙傚療鑰呮ā寮?/span>

瑙傚療鑰呮ā寮忓浜庢垜浠潵璇达紝鐪熸槸鍐嶇畝鍗曚笉杩囦簡銆傛棤澶栦箮涓や釜鎿嶄綔锛岃瀵熻€呰闃呰嚜宸卞叧蹇冪殑涓婚鍜屼富棰樻湁鏁版嵁鍙樺寲鍚庨€氱煡瑙傚療鑰呬滑銆?/p>

棣栧厛锛岄渶瑕佸畾涔変富棰橈紝姣忎釜涓婚闇€瑕佹寔鏈夎瀵熻€呭垪琛ㄧ殑寮曠敤锛岀敤浜庡湪鏁版嵁鍙樻洿鐨勬椂鍊欓€氱煡鍚勪釜瑙傚療鑰咃細

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();
    }
    // 娉ㄥ唽瑙傚療鑰?br>    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兘涓嶇敤瀹氫箟浜嗭紝涓嶈繃锛岄€氬父鍦烘櫙涓嬶紝鏃㈢劧鐢ㄥ埌浜嗚瀵熻€呮ā寮忥紝鎴戜滑灏辨槸甯屾湜涓€涓簨浠跺嚭鏉ヤ簡锛屼細鏈夊涓笉鍚岀殑绫婚渶瑕佸鐞嗙浉搴旂殑淇℃伅銆傛瘮濡傦紝璁㈠崟淇敼鎴愬姛浜嬩欢锛屾垜浠笇鏈涘彂鐭俊鐨勭被寰楀埌閫氱煡銆佸彂閭欢鐨勭被寰楀埌閫氱煡銆佸鐞嗙墿娴佷俊鎭殑绫诲緱鍒伴€氱煡绛夈€?/p>

鎴戜滑鏉ュ畾涔夊叿浣撶殑鍑犱釜瑙傚療鑰呯被锛?/p>

public class BinaryObserver extends Observer {
    // 鍦ㄦ瀯閫犳柟娉曚腑杩涜璁㈤槄涓婚
    public BinaryObserver(Subject subject) {
        this.subject = subject;
        // 閫氬父鍦ㄦ瀯閫犳柟娉曚腑灏?nbsp;this 鍙戝竷鍑哄幓鐨勬搷浣滀竴瀹氳灏忓績
        this.subject.attach(this);
    }
    // 璇ユ柟娉曠敱涓婚绫诲湪鏁版嵁鍙樻洿鐨勬椂鍊欒繘琛岃皟鐢?br>    @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) {
    // 鍏堝畾涔変竴涓富棰?br>    Subject subject1 = new Subject();
    // 瀹氫箟瑙傚療鑰?br>    new BinaryObserver(subject1);
    new HexaObserver(subject1);

    // 妯℃嫙鏁版嵁鍙樻洿锛岃繖涓椂鍊欙紝瑙傚療鑰呬滑鐨?nbsp;update 鏂规硶灏嗕細琚皟鐢?br>    subject.setState(11);
}

output:

璁㈤槄鐨勬暟鎹彂鐢熷彉鍖栵紝鏂扮殑鏁版嵁澶勭悊涓轰簩杩涘埗鍊间负锛?011
璁㈤槄鐨勬暟鎹彂鐢熷彉鍖栵紝鏂扮殑鏁版嵁澶勭悊涓哄崄鍏繘鍒跺€间负锛欱

褰撶劧锛宩dk 涔熸彁渚涗簡鐩镐技鐨勬敮鎸侊紝鍏蜂綋鐨勫ぇ瀹跺彲浠ュ弬鑰?java.util.Observable 鍜?java.util.Observer 杩欎袱涓被銆?/p>

瀹為檯鐢熶骇杩囩▼涓紝瑙傚療鑰呮ā寮忓線寰€鐢ㄦ秷鎭腑闂翠欢鏉ュ疄鐜帮紝濡傛灉瑕佸疄鐜板崟鏈鸿瀵熻€呮ā寮忥紝绗旇€呭缓璁鑰呬娇鐢?Guava 涓殑 EventBus锛屽畠鏈夊悓姝ュ疄鐜颁篃鏈夊紓姝ュ疄鐜帮紝鏈枃涓昏浠嬬粛璁捐妯″紡锛屽氨涓嶅睍寮€璇翠簡銆?/p>

杩樻湁锛屽嵆浣挎槸涓婇潰鐨勮繖涓唬鐮侊紝涔熶細鏈夊緢澶氬彉绉嶏紝澶у鍙璁颁綇鏍稿績鐨勯儴鍒嗭紝閭e氨鏄竴瀹氭湁涓€涓湴鏂瑰瓨鏀句簡鎵€鏈夌殑瑙傚療鑰咃紝鐒跺悗鍦ㄤ簨浠跺彂鐢熺殑鏃跺€欙紝閬嶅巻瑙傚療鑰咃紝璋冪敤瀹冧滑鐨勫洖璋冨嚱鏁般€?/p>

璐d换閾炬ā寮?/span>

璐d换閾鹃€氬父闇€瑕佸厛寤虹珛涓€涓崟鍚戦摼琛紝鐒跺悗璋冪敤鏂瑰彧闇€瑕佽皟鐢ㄥご閮ㄨ妭鐐瑰氨鍙互浜嗭紝鍚庨潰浼氳嚜鍔ㄦ祦杞笅鍘汇€傛瘮濡傛祦绋嬪鎵瑰氨鏄竴涓緢濂界殑渚嬪瓙锛屽彧瑕佺粓绔敤鎴锋彁浜ょ敵璇凤紝鏍规嵁鐢宠鐨勫唴瀹逛俊鎭紝鑷姩寤虹珛涓€鏉¤矗浠婚摼锛岀劧鍚庡氨鍙互寮€濮嬫祦杞簡銆?/p>

鏈夎繖涔堜竴涓満鏅紝鐢ㄦ埛鍙傚姞涓€涓椿鍔ㄥ彲浠ラ鍙栧鍝侊紝浣嗘槸娲诲姩闇€瑕佽繘琛屽緢澶氱殑瑙勫垯鏍¢獙鐒跺悗鎵嶈兘鏀捐锛屾瘮濡傞鍏堥渶瑕佹牎楠岀敤鎴锋槸鍚︽槸鏂扮敤鎴枫€佷粖鏃ュ弬涓庝汉鏁版槸鍚︽湁闄愰銆佸叏鍦哄弬涓庝汉鏁版槸鍚︽湁闄愰绛夌瓑銆傝瀹氱殑瑙勫垯閮介€氳繃鍚庯紝鎵嶈兘璁╃敤鎴烽璧板鍝併€?/p>

鉂?/span>

濡傛灉浜у搧缁欎綘杩欎釜闇€姹傜殑璇濓紝鎴戞兂澶ч儴鍒嗕汉涓€寮€濮嬭偗瀹氭兂鐨勫氨鏄紝鐢ㄤ竴涓?List 鏉ュ瓨鏀炬墍鏈夌殑瑙勫垯锛岀劧鍚?foreach 鎵ц涓€涓嬫瘡涓鍒欏氨濂戒簡銆備笉杩囷紝璇昏€呬篃鍏堝埆鎬ワ紝鐪嬬湅璐d换閾炬ā寮忓拰鎴戜滑璇寸殑杩欎釜鏈変粈涔堜笉涓€鏍凤紵

鉂?/span>

棣栧厛锛屾垜浠瀹氫箟娴佺▼涓婅妭鐐圭殑鍩虹被锛?/p>

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;
    }
}

鎺ヤ笅鏉ワ紝鎴戜滑闇€瑕佸畾涔夊叿浣撶殑姣忎釜鑺傜偣浜嗐€?/p>

鏍¢獙鐢ㄦ埛鏄惁鏄柊鐢ㄦ埛锛?/p>

public class NewUserRuleHandler extends RuleHandler {
    public void apply(Context context) {
        if (context.isNewUser()) {
            // 濡傛灉鏈夊悗缁ц妭鐐圭殑璇濓紝浼犻€掍笅鍘?br>            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();

    // 鍋囪鏈娲诲姩浠呮牎楠屽湴鍖哄拰濂栧搧鏁伴噺锛屼笉鏍¢獙鏂拌€佺敤鎴?br>    locationHandler.setSuccessor(limitHandler);

    locationHandler.apply(context);
}

浠g爜鍏跺疄寰堢畝鍗曪紝灏辨槸鍏堝畾涔夊ソ涓€涓摼琛紝鐒跺悗鍦ㄩ€氳繃浠绘剰涓€鑺傜偣鍚庯紝濡傛灉姝よ妭鐐规湁鍚庣户鑺傜偣锛岄偅涔堜紶閫掍笅鍘汇€?/p>

鑷充簬瀹冨拰鎴戜滑鍓嶉潰璇寸殑鐢ㄤ竴涓?List 瀛樻斁闇€瑕佹墽琛岀殑瑙勫垯鐨勫仛娉曟湁浠€涔堝紓鍚岋紝鐣欑粰璇昏€呰嚜宸辩悽纾ㄥ惂銆?/p>

妯℃澘鏂规硶妯″紡

鍦ㄥ惈鏈夌户鎵跨粨鏋勭殑浠g爜涓紝妯℃澘鏂规硶妯″紡鏄潪甯稿父鐢ㄧ殑銆?/p>

閫氬父浼氭湁涓€涓娊璞$被锛?/p>

public abstract class AbstractTemplate {
    // 杩欏氨鏄ā鏉挎柟娉?br>    public void templateMethod() {
        init();
        apply(); // 杩欎釜鏄噸鐐?br>        end(); // 鍙互浣滀负閽╁瓙鏂规硶
    }

    protected void init() {
        System.out.println("init 鎶借薄灞傚凡缁忓疄鐜帮紝瀛愮被涔熷彲浠ラ€夋嫨瑕嗗啓");
    }

    // 鐣欑粰瀛愮被瀹炵幇
    protected abstract void apply();

    protected void end() {
    }
}

妯℃澘鏂规硶涓皟鐢ㄤ簡 3 涓柟娉曪紝鍏朵腑 apply() 鏄娊璞℃柟娉曪紝瀛愮被蹇呴』瀹炵幇瀹冿紝鍏跺疄妯℃澘鏂规硶涓湁鍑犱釜鎶借薄鏂规硶瀹屽叏鏄嚜鐢辩殑锛屾垜浠篃鍙互灏嗕笁涓柟娉曢兘璁剧疆涓烘娊璞℃柟娉曪紝璁╁瓙绫绘潵瀹炵幇銆備篃灏辨槸璇达紝妯℃澘鏂规硶鍙礋璐e畾涔夌涓€姝ュ簲璇ヨ鍋氫粈涔堬紝绗簩姝ュ簲璇ュ仛浠€涔堬紝绗笁姝ュ簲璇ュ仛浠€涔堬紝鑷充簬鎬庝箞鍋氾紝鐢卞瓙绫绘潵瀹炵幇銆?/p>

鎴戜滑鍐欎竴涓疄鐜扮被锛?/p>

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爜涓€?/p>

鐘舵€佹ā寮?/span>

update: 2017-10-19

搴熻瘽鎴戝氨涓嶈浜嗭紝鎴戜滑璇翠竴涓畝鍗曠殑渚嬪瓙銆傚晢鍝佸簱瀛樹腑蹇冩湁涓渶鍩烘湰鐨勯渶姹傛槸鍑忓簱瀛樺拰琛ュ簱瀛橈紝鎴戜滑鐪嬬湅鎬庝箞鐢ㄧ姸鎬佹ā寮忔潵鍐欍€?/p>

鏍稿績鍦ㄤ簬锛屾垜浠殑鍏虫敞鐐逛笉鍐嶆槸 Context 鏄杩涜鍝鎿嶄綔锛岃€屾槸鍏虫敞鍦ㄨ繖涓?Context 浼氭湁鍝簺鎿嶄綔銆?/p>

瀹氫箟鐘舵€佹帴鍙o細

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;
    }
}

鎴戜滑鏉ョ湅涓嬪鎴风璋冪敤锛屽ぇ瀹跺氨涓€娓呬簩妤氫簡锛?/p>

public static void main(String[] args) {
    // 鎴戜滑闇€瑕佹搷浣滅殑鏄?nbsp;iPhone X
    Context context = new Context("iPhone X");

    // 鐪嬬湅鎬庝箞杩涜琛ュ簱瀛樻搷浣?br>      State revertState = new RevertState();
      revertState.doAction(context);

    // 鍚屾牱鐨勶紝鍑忓簱瀛樻搷浣滀篃闈炲父绠€鍗?br>      State deductState = new DeductState();
      deductState.doAction(context);

      // 濡傛灉闇€瑕佹垜浠彲浠ヨ幏鍙栧綋鍓嶇殑鐘舵€?br>    // context.getState().toString();
}

璇昏€呭彲鑳戒細鍙戠幇锛屽湪涓婇潰杩欎釜渚嬪瓙涓紝濡傛灉鎴戜滑涓嶅叧蹇冨綋鍓?context 澶勪簬浠€涔堢姸鎬侊紝閭d箞 Context 灏卞彲浠ヤ笉鐢ㄧ淮鎶?state 灞炴€т簡锛岄偅鏍蜂唬鐮佷細绠€鍗曞緢澶氥€?/p>

涓嶈繃锛屽晢鍝佸簱瀛樿繖涓緥瀛愭瘯绔熷彧鏄釜渚嬶紝鎴戜滑杩樻湁寰堝瀹炰緥鏄渶瑕佺煡閬撳綋鍓?context 澶勪簬浠€涔堢姸鎬佺殑銆?/p>

琛屼负鍨嬫ā寮忔€荤粨

琛屼负鍨嬫ā寮忛儴鍒嗕粙缁嶄簡绛栫暐妯″紡銆佽瀵熻€呮ā寮忋€佽矗浠婚摼妯″紡銆佹ā鏉挎柟娉曟ā寮忓拰鐘舵€佹ā寮忥紝鍏跺疄锛岀粡鍏哥殑琛屼负鍨嬫ā寮忚繕鍖呮嫭澶囧繕褰曟ā寮忋€佸懡浠ゆā寮忕瓑锛屼絾鏄畠浠殑浣跨敤鍦烘櫙姣旇緝鏈夐檺锛岃€屼笖鏈枃绡囧箙涔熸尯澶т簡锛屾垜灏变笉杩涜浠嬬粛浜嗐€?/p>

鎬荤粨

瀛︿範璁捐妯″紡鐨勭洰鐨勬槸涓轰簡璁╂垜浠殑浠g爜鏇村姞鐨勪紭闆呫€佹槗缁存姢銆佹槗鎵╁睍銆傝繖娆℃暣鐞嗚繖绡囨枃绔狅紝璁╂垜閲嶆柊瀹¤浜嗕竴涓嬪悇涓璁℃ā寮忥紝瀵规垜鑷繁鑰岃█鏀惰幏杩樻槸鎸哄ぇ鐨勩€傛垜鎯筹紝鏂囩珷鐨勬渶澶ф敹鐩婅€呬竴鑸兘鏄綔鑰呮湰浜猴紝涓轰簡鍐欎竴绡囨枃绔狅紝闇€瑕佸珐鍥鸿嚜宸辩殑鐭ヨ瘑锛岄渶瑕佸鎵惧悇绉嶈祫鏂欙紝鑰屼笖锛岃嚜宸卞啓杩囩殑鎵嶆渶瀹规槗璁颁綇锛屼篃绠楁槸鎴戠粰璇昏€呯殑寤鸿鍚с€?/p>

-- END --

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣? | 鏇村绮惧僵鏂囩珷 -



銆奐ava瀛︿範-杩涢樁鎵嬪唽銆?/strong>

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?

涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?

 
   
   
 
鈻芥兂鐭ラ亾鏇村锛?/span>

闀挎寜/鎵爜鍏虫敞鎴戝惂鈫戔啈鈫?/span>
              
                
                
              

以上是关于涓€鏂囧悆閫?3绉嶈璁℃ā寮?瀹冧笉棣?的主要内容,如果未能解决你的问题,请参考以下文章

鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡

璁╄璁℃ā寮忛涓€浼氬効|鈶犲紑绡囪幏濂栨劅瑷€

涓€鏂囧交搴曟悶鎳?zookeeper 鏍稿績鐭ヨ瘑鐐?

涓€鍙ヨ瘽璇勮璁捐妯″紡鍏ぇ鍘熷垯