鍦ㄨ涓虹被璁捐妯″紡涓紝鐘舵佹ā寮忓拰绛栫暐妯″紡鏄翰鍏勫紵锛屼袱鑰呴潪甯哥浉浼硷紝鎴戜滑鍏堢湅鐪嬩袱鑰呯殑閫氱敤绫诲浘锛屾妸涓よ呮斁鍦ㄤ竴璧锋瘮杈冧竴涓嬶紝濡傚浘鎵绀猴細

浜岃呮槸涓嶆槸寰堝儚锛屽厜鐪嬭繖涓 UML 鎴戜滑鐪嬩笉鍑轰粈涔堢鍊潵锛屾帴涓嬫潵鎴戜滑缁撳悎渚嬪瓙锛屾潵瀵规瘮涓涓嬩簩鑰呬箣闂寸殑鍖哄埆銆网易快三开奖—官方网址22270.COM涓嬮潰鐨勪緥瀛愭槸銆奌ead First 璁捐妯″紡銆嬩腑鐨勪緥瀛愩


绛栫暐妯″紡

绛栫暐妯″紡瀹氫箟浜嗙畻娉曟棌锛屽垎鍒皝瑁呰捣鏉ワ紝璁╀粬浠箣闂村彲浠ヤ簰鐩告浛鎹紝姝ゆā寮忚绠楁硶鐨勫彉鍖栫嫭绔嬩簬浣跨敤绠楁硶鐨勫鎴枫

鏌愬叕鍙稿紑鍙戜簡涓涓腑瀛愭父鎴忥紝閲岄潰浼氬嚭鐜板悇绉嶇壒鐐圭殑楦瓙锛氱豢澶撮腑锛岀孩澶撮腑锛屾鐨腑鈥︹︾敤绋嬪簭濡備綍瀹炵幇杩欎簺楦瓙锛 鐪嬪埌杩欎釜鍦烘櫙闂锛屽緢瀹规槗鎯冲埌锛屽厛瀹氫箟涓涓墍鏈夐腑瀛愮殑瓒呯被Duck锛屾妸鍏叡鏂规硶鍜屽睘鎬у皝瑁呰繘鍘汇備緥濡傦紝楦瓙閮戒細娓告吵锛岄腑瀛愰兘鏈夊悇鑷殑澶栬矊锛

瀹炰緥

public abstract class Duck {
    public void swim(){
        System.out.println("All duck can swim!");
    }
    public abstract void display();
    public void fly(){
        System.out.println("椋瀪~~");
    }
    public void quack(){
        System.out.println("鍛卞懕鍛眫");
    }
}

浣嗘槸寰堝揩锛屾垜浠彂鐜拌繖涓秴绫荤殑瀹氫箟鏈夐棶棰樸備笉鏄墍鏈夐腑瀛愰兘浼氶锛屼笉鏄墍鏈夐腑瀛愰兘鏄懕鍛卞彨锛堝亣璁炬鐨腑鏄惐鍚卞彨锛夈傚彧瑕佺户鎵夸簡杩欎釜瓒呯被锛岄偅鎵鏈夌殑楦瓙閮戒細椋炰簡锛屾墍鏈夌殑楦瓙閮芥槸鍛卞懕鍙簡銆

鎬庝箞鍔炲憿锛

Solution 1

网易快三开奖—官方网址22270.COM绗竴涓兂鍒扮殑瑙e喅鏂规硶鏄細瀛愮被杩涜鏂规硶瑕嗙洊銆傚緢绠鍗曪紝涓嶄細椋炵殑瀛愮被瑕嗙洊fly鏂规硶锛岄噸鍐欎笉灏辫浜嗭紵

浣嗘槸锛屽紛绔緢鏄庢樉锛屾墍鏈変笉浼氶鐨勫瓙绫诲矀涓嶉兘鏄瑕嗙洊锛屽亣璁50绉嶉腑瀛愰兘涓嶄細椋烇紝閭i噸鍐欑殑宸ヤ綔閲忓拰缁存姢閲忓緱鏈夊澶э紵

Solution 2

濂界殑锛屾墍浠ユ垜浠兂鍒扮浜屼釜鏂规硶锛氱户缁璁″瓙鎶借薄绫伙紝渚嬪浼氶涓嶄細鍙殑鎶借薄瀛愮被FlyNoQuackDuck,浼氬彨涓嶄細闈炵殑鎶借薄瀛愮被QuackNoFlyDuck锛屼笉浼氬彨涓嶄細椋炵殑鎶借薄瀛愮被NoQuackNoFlyDuck锛屽張浼氶鍙堜細鍙殑鎶借薄瀛愮被FlyAndQuackDuck鈥︹

鍐欑潃鍐欑潃鎴戜滑鍙戠幇杩欑鏂规硶涔熶笉琛岋紝澶笉鐏垫椿锛岃屼笖鏀瑰彉鐨勯儴鍒嗚秺澶氾紝杩欑鎶借薄瀛愮被寰楀畾涔夌殑瓒婂銆

Solution 3

閭d粈涔堟柟娉曟墠濂藉憿锛熸垜浠濊冿紝涔嬫墍浠ュ嚭鐜颁笂闈㈢殑闂锛屾槸鍥犱负鎴戜滑涔犳儻鎬х殑鎬绘兂鐢ㄧ户鎵挎潵瀹炵幇鏀瑰彉鐨勯儴鍒嗭紝瀹為檯鎴戜滑鍙互灏嗘敼鍙樼殑閮ㄥ垎鎶界鍑烘潵锛岀敤缁勫悎鏉ュ疄鐜般

杩欓噷鐢ㄥ埌浜嗕笁涓璁″師鍒欙細

  • 鎵惧嚭搴旂敤涓彲鑳介渶瑕佸彉鍖栦箣澶勶紝鎶婁粬浠嫭绔嬪嚭鏉ャ
  • 閽堝鎺ュ彛缂栫▼锛岃屼笉鏄拡瀵瑰疄鐜
  • 澶氱敤缁勫悎锛屽皯鐢ㄧ户鎵

杩愮敤绗竴涓璁″師鍒欙紝鎴戜滑灏嗘敼鍙樼殑鏂规硶fly()鍜宷uack()鐙珛鍑烘潵锛屽皝瑁呮垚涓や釜琛屼负绫绘帴鍙c傜劧鍚庢牴鎹笉鍚岀殑闇姹傦紝璁捐鍑哄疄鐜版帴鍙g殑涓嶅悓琛屼负绫汇

杩愮敤绗簩涓拰绗笁涓璁″師鍒欙紝鎴戜滑鍦―uck绫讳腑缁勫悎涓や釜鎴愬憳鍙橀噺鐨勬帴鍙o紝鍦ㄥ瓙绫讳腑鍔ㄦ佺殑璧嬪笺

鏁翠綋鐨"绫诲浘"濡備笅锛

鎬荤粨

杩欑瑙e喅鏂规硶寰堝畬缇庣殑瑙e喅浜嗘垜浠殑闂锛岃繍鐢"绛栫暐妯″紡"鐨勬濇兂锛屽皢鍙樺寲鐨勯儴鍒嗘娊绂诲嚭鏉ワ紝缁勫悎杩涚被涓紝鏍规嵁涓嶅悓鐨勫瓙绫伙紝鍙互"set"涓嶅悓鐨勮涓哄瓙绫昏繘琛岋紝瀹炵幇鍔ㄦ佹敼鍙樿涓恒

浠g爜瀹炵幇

涓や釜琛屼负鎺ュ彛绫

瀹炰緥

public interface FlyBehavior {
    public void fly();
}

public interface QuackBehavior {
    public void quack();
}

瀹炵幇椋炶鎺ュ彛鐨勪笉鍚岃涓虹被

瀹炰緥

public class FlyNoWay implements FlyBehavior{
    public void fly(){
        System.out.println("鎴戜笉鑳介鈥︹");
    }
}

public class FlyWithWings implements FlyBehavior{
    public void fly(){
        System.out.println("椋瀪~~");
    }
}

public class FlyWithRocket implements FlyBehavior{

    public void fly(){
        System.out.println("甯︿笂鐏绛掞紝椋瀪~~");
    }
}

瀹炵幇楦彨鐨勪笉鍚岃涓虹被

瀹炰緥

public class Quack implements QuackBehavior{
    public void quack(){
        System.out.println("鍛卞懕鍛眫");
    }
}


public class Squeak implements QuackBehavior{
    public void quack(){
        System.out.println("鍚卞惐鍚眫");
    }
}


public class MuteQuack implements QuackBehavior{
    public void quack(){
        System.out.println("鎴戜笉浼氬彨鈥︹");
    }
}

缁勫悎浜嗗疄鐜版帴鍙g殑瓒呯被

瀹炰緥

public abstract class Duck {
    protected FlyBehavior flyBehavior;
    protected QuackBehavior quackBehavior;

    public void swim(){
        System.out.println("All duck can swim!");
    }

    public abstract void display();

    /**
     * 鍔ㄦ佹敼鍙橀琛岃涓
     */

    public void setFlyBehavior(FlyBehavior flyBehavior) {
        this.flyBehavior = flyBehavior;
    }

    /**
     * 鍔ㄦ佹敼鍙橀腑鍙涓
     */

    public void setQuackBehavior(QuackBehavior quackBehavior) {
        this.quackBehavior = quackBehavior;
    }

    public void performFly(){
        flyBehavior.fly();
    }

    public void performQuack(){
        quackBehavior.quack();
    }
}

涓嶅悓鐨勯腑瀛愮被

瀹炰緥

/**
 * 缁垮ご楦
 */

public class MallarDuck extends Duck{

    public MallarDuck() {
        //鍙
        flyBehavior = new FlyWithWings();
        //鍛卞懕鍙
        quackBehavior = new Quack();
    }

    @Override
    public void display() {
        System.out.println("鐪嬬潃鍍忕豢澶撮腑");
    }

}

/**
 * 缁垮ご楦
 */

public class RedHeadDuck extends Duck{

    public RedHeadDuck() {
        //鍙
        flyBehavior = new FlyWithWings();
        //鍛卞懕鍙
        quackBehavior = new Quack();
    }

    @Override
    public void display() {
        System.out.println("鐪嬬潃鍍忕孩澶撮腑");
    }

}

/**
 * 姗$毊楦
 */

public class RubberDuck extends Duck{

    public RubberDuck() {
        //涓嶄細椋
        flyBehavior = new FlyNoWay();
        //鍚卞惐鍙
        quackBehavior = new Squeak();
    }

    @Override
    public void display() {
        System.out.println("鐪嬬潃鍍忔鐨腑");
    }

}

鐘舵佹ā寮

鐘舵佹ā寮忓厑璁稿璞″湪鍐呴儴鐘舵佹敼鍙樻椂鏀瑰彉瀹冪殑琛屼负锛屽璞$湅璧锋潵濂藉儚淇敼浜嗗畠鐨勭被

鐘舵佹ā寮忕瓥鐣ユā寮忓緢鐩镐技锛屼篃鏄皢绫荤殑"鐘舵"灏佽浜嗚捣鏉ワ紝鍦ㄦ墽琛屽姩浣滄椂杩涜鑷姩鐨勮浆鎹紝浠庤屽疄鐜帮紝绫诲湪涓嶅悓鐘舵佷笅鐨勫悓涓鍔ㄤ綔鏄剧ず鍑轰笉鍚岀粨鏋溿傚畠涓庣瓥鐣ユā寮忕殑鍖哄埆鍦ㄤ簬锛岃繖绉嶈浆鎹㈡槸"鑷姩"锛"鏃犳剰璇"鐨勩

鐘舵佹ā寮忕殑绫诲浘濡備笅

网易快三开奖—官方网址22270.COM鐘舵佹ā寮忕殑绫诲浘涓庣瓥鐣ユā寮忎竴妯′竴鏍凤紝鍖哄埆鍦ㄤ簬瀹冧滑鐨勬剰鍥俱傜瓥鐣ユā寮忎細鎺у埗瀵硅薄浣跨敤浠涔堢瓥鐣ワ紝鑰岀姸鎬佹ā寮忎細鑷姩鏀瑰彉鐘舵併网易快三开奖—官方网址22270.COM鐪嬪畬涓嬮潰鐨勬渚嬪簲璇ュ氨娓呮浜嗐

鐜板湪鏈変竴涓硸鏋滄満鐨勯渶姹傛憜鍦ㄤ綘闈㈠墠锛岄渶瑕佺敤Java瀹炵幇銆

鎴戜滑鍒嗘瀽涓涓嬶紝绯栨灉鏈虹殑鍔熻兘鍙互鍒嗕负涓嬪浘鎵绀虹殑鍥涗釜鍔ㄤ綔鍜屽洓涓姸鎬侊細

鍦ㄤ笉鍚岀姸鎬佷笅锛屽悓鏍风殑鍔ㄤ綔缁撴灉涓嶄竴鏍枫備緥濡傦紝鍦"鎶曚簡25鍒嗛挶"鐨勭姸鎬佷笅"杞姩鏇叉焺"锛屼細鍞嚭绯栨灉锛涜屽湪"娌℃湁25鍒嗛挶"鐨勭姸鎬佷笅"杞姩鏇叉焺"浼氭彁绀鸿鍏堟姇甯併

绠鍗曟濊冨悗锛屾垜浠啓鍑哄涓嬬殑绯栨灉鏈哄疄鐜颁唬鐮

瀹炰緥

public class NoPatternGumballMachine{
    /*
     * 鍥涗釜鐘舵
     */

    /**娌℃湁纭竵鐘舵*/
    private final static int NO_QUARTER = 0;
    /**鎶曞竵鐘舵*/
    private final static int HAS_QUARTER = 1;
    /**鍑哄敭绯栨灉鐘舵*/
    private final static int SOLD = 2;
    /**绯栨灉鍞敖鐘舵*/
    private final static int SOLD_OUT = 3;

    private int state = SOLD_OUT;
    private int candyCount = 0;

    public NoPatternGumballMachine(int count) {
        this.candyCount = count;
        if(candyCount > 0)
            state = NO_QUARTER;
    }

    /*
     * 鍥涗釜鍔ㄤ綔
     */


    /**
     * 鎶曞竵
     */

    public void insertQuarter() {
        if(NO_QUARTER == state){
            System.out.println("鎶曞竵");
            state = HAS_QUARTER;
        }
        else if(HAS_QUARTER == state){
            System.out.println("璇蜂笉瑕侀噸澶嶆姇甯侊紒");
            returnQuarter();
        }
        else if(SOLD == state){
            System.out.println("宸叉姇甯侊紝璇风瓑寰呯硸鏋");
            returnQuarter();
        }else if(SOLD_OUT == state){
            System.out.println("绯栨灉宸茬粡鍞敖");
            returnQuarter();
        }
    }

    /**
     * 閫甯
     */

    public void ejectQuarter() {
        if(NO_QUARTER == state){
            System.out.println("娌℃湁纭竵锛屾棤娉曞脊鍑");
        }
        else if(HAS_QUARTER == state){
            returnQuarter();
            state = NO_QUARTER;
        }
        else if(SOLD == state){
            System.out.println("鏃犳硶閫甯侊紝姝e湪鍙戞斁绯栨灉锛岃绛夊緟");
        }else if(SOLD_OUT == state){
            System.out.println("娌℃湁鎶曞竵锛屾棤娉曢甯");
        }
    }

    /**
     * 杞姩鍑虹硸鏇茶酱
     */

    public void turnCrank() {
        if(NO_QUARTER == state){
            System.out.println("璇峰厛鎶曞竵");
        }
        else if(HAS_QUARTER == state){
            System.out.println("杞姩鏇茶酱锛屽噯澶囧彂绯");
            state = SOLD;
        }
        else if(SOLD == state){
            System.out.println("宸叉寜杩囨洸杞达紝璇风瓑寰");
        }else if(SOLD_OUT == state){
            System.out.println("绯栨灉宸茬粡鍞敖");
        }
    }

    /**
     * 鍙戠硸
     */

    public void dispense() {
        if(NO_QUARTER == state){
            System.out.println("娌℃湁鎶曞竵锛屾棤娉曞彂鏀剧硸鏋");
        }
        else if(HAS_QUARTER == state){
            System.out.println("this method don't support");
        }
        else if(SOLD == state){
            if(candyCount > 0){
                System.out.println("鍒嗗彂涓棰楃硸鏋");
                candyCount --;
                state = NO_QUARTER;
            }
            else{
                System.out.println("鎶辨瓑锛岀硸鏋滃凡鍞敖");
                state = SOLD_OUT;
            }
        }else if(SOLD_OUT == state){
            System.out.println("鎶辨瓑锛岀硸鏋滃凡鍞敖");
        }
    }

    /**
     * 閫杩樼‖甯
     */

    protected void returnQuarter() {
        System.out.println("閫甯佲︹");
    }

}

浠庝唬鐮侀噷闈㈠彲浠ョ湅鍑猴紝绯栨灉鏈烘牴鎹鍒讳笉鍚岀殑鐘舵侊紝鑰屼娇瀵瑰簲鐨勫姩浣滃憟鐜颁笉鍚岀殑缁撴灉銆傝繖浠戒唬鐮佸凡缁忓彲浠ユ弧瓒虫垜浠殑鍩烘湰闇姹傦紝浣嗙◢寰濊冧竴涓嬶紝浣犱細瑙夊緱杩欑瀹炵幇浠g爜浼间箮锛屽姛鑳藉お澶嶆潅浜嗭紝鎵╁睍鎬у緢宸紝娌℃湁闈㈠悜瀵硅薄鐨勯鏍笺

鍋囪鐢变簬鏂伴渶姹傦紝瑕佸鍔犱竴绉嶇姸鎬侊紝閭f瘡涓姩浣滄柟娉曟垜浠兘闇瑕佷慨鏀癸紝閮借閲嶆柊澧炲姞涓鏉lse璇彞銆傝屽鏋滈渶姹傚彉鏇达紝鏌愪釜鐘舵佷笅鐨勫姩浣滈渶瑕佷慨鏀癸紝鎴戜滑涔熻鍚屾椂鏀瑰姩鍥涗釜鏂规硶銆傝繖鏍风殑宸ヤ綔灏嗘槸绻佺悙鑰屽ご澶х殑銆

鎬庝箞鍔? 鍏ぇ璁捐鍘熷垯涔嬩竴

鎵惧嚭搴旂敤涓彲鑳介渶瑕佸彉鍖栦箣澶勶紝鎶婁粬浠嫭绔嬪嚭鏉ャ

网易快三开奖—官方网址22270.COM鍦ㄧ硸鏋滄満涓紝鐘舵佸氨鏄竴鐩村湪鍙樺寲鐨勯儴鍒嗭紝涓嶅悓鐨勭姸鎬佸姩浣滀笉涓鏍枫傛垜浠畬鍏ㄥ彲浠ュ皢鍏舵娊绂诲嚭鏉

鏂扮殑璁捐鎯虫硶濡備笅锛

棣栧厛锛屾垜浠畾涔変竴涓猄tate鎺ュ彛锛屽湪杩欎釜鎺ュ彛鍐咃紝绯栨灉鏈虹殑姣忎釜鍔ㄤ綔閮芥湁涓涓搴旂殑鏂规硶

鐒跺悗涓烘満鍣ㄧ殑姣忎釜鐘舵佸疄鐜扮姸鎬佺被锛岃繖浜涚被灏嗚礋璐e湪瀵瑰簲鐨勭姸鎬佷笅杩涜鏈哄櫒鐨勮涓

鏈鍚庯紝鎴戜滑瑕佹憜鑴辨棫鐨勬潯浠朵唬鐮侊紝鍙栬屼唬涔嬬殑鏂瑰紡鏄紝灏嗗姩浣滃鎵樺埌鐘舵佺被

瀹氫箟涓涓猄tate鎺ュ彛

瀹炰緥

public abstract class State {
    /**
     * 鎶曞竵
     */

    public abstract void insertQuarter();

    /**
     * 閫甯
     */

    public abstract void ejectQuarter();

    /**
     * 杞姩鍑虹硸鏇茶酱
     */

    public abstract void turnCrank();

    /**
     * 鍙戠硸
     */

    public abstract void dispense();

    /**
     * 閫杩樼‖甯
     */

    protected void returnQuarter() {
        System.out.println("閫甯佲︹");
    }
}

涓烘満鍣ㄧ殑姣忎釜鐘舵佸疄鐜扮姸鎬佺被

瀹炰緥

/**
 * 娌℃湁纭竵鐨勭姸鎬
 */

public class NoQuarterState extends State{
    GumballMachine gumballMachine;

    public NoQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
    @Override
    public void insertQuarter() {
        System.out.println("浣犳姇鍏ヤ簡涓涓‖甯");
        //杞崲涓烘湁纭竵鐘舵
        gumballMachine.setState(gumballMachine.hasQuarterState);
    }

    @Override
    public void ejectQuarter() {
        System.out.println("娌℃湁纭竵锛屾棤娉曞脊鍑");
    }

    @Override
    public void turnCrank() {
        System.out.println("璇峰厛鎶曞竵");
    }

    @Override
    public void dispense() {
        System.out.println("娌℃湁鎶曞竵锛屾棤娉曞彂鏀剧硸鏋");
    }

}

/**
 * 鎶曠‖甯佺殑鐘舵
 */

public class HasQuarterState extends State{
    GumballMachine gumballMachine;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
    @Override
    public void insertQuarter() {
        System.out.println("璇蜂笉瑕侀噸澶嶆姇甯侊紒");
        returnQuarter();
    }

    @Override
    public void ejectQuarter() {
        returnQuarter();
        gumballMachine.setState(gumballMachine.noQuarterState);
    }

    @Override
    public void turnCrank() {
        System.out.println("杞姩鏇茶酱锛屽噯澶囧彂绯");
        gumballMachine.setState(gumballMachine.soldState);
    }

    @Override
    public void dispense() {
        System.out.println("this method don't support");
    }

}

/**
 * 鍑哄敭鐨勭姸鎬
 */

public class SoldState extends State{
    GumballMachine gumballMachine;

    public SoldState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("宸叉姇甯侊紝璇风瓑寰呯硸鏋");
        returnQuarter();
    }

    @Override
    public void ejectQuarter() {
        System.out.println("鏃犳硶閫甯侊紝姝e湪鍙戞斁绯栨灉锛岃绛夊緟");
    }

    @Override
    public void turnCrank() {
        System.out.println("宸叉寜杩囨洸杞达紝璇风瓑寰");
    }

    @Override
    public void dispense() {
        int candyCount = gumballMachine.getCandyCount();
        if(candyCount > 0){
            System.out.println("鍒嗗彂涓棰楃硸鏋");
            candyCount--;
            gumballMachine.setCandyCount(candyCount);
            if(candyCount > 0){
                gumballMachine.setState(gumballMachine.noQuarterState);
                return;
            }
        }

        System.out.println("鎶辨瓑锛岀硸鏋滃凡鍞敖");
        gumballMachine.setState(gumballMachine.soldOutState);
    }

}

/**
 * 鍞敖鐨勭姸鎬
 */

public class SoldOutState extends State{
    GumballMachine gumballMachine;

    public SoldOutState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
    @Override
    public void insertQuarter() {
        System.out.println("绯栨灉宸茬粡鍞敖");
        returnQuarter();
    }

    @Override
    public void ejectQuarter() {
        System.out.println("娌℃湁鎶曞竵锛屾棤娉曢甯");
    }

    @Override
    public void turnCrank() {
        System.out.println("绯栨灉宸茬粡鍞敖");
    }

    @Override
    public void dispense() {
        System.out.println("绯栨灉宸茬粡鍞敖");
    }

}

灏嗙硸鏋滄満鐨勫姩浣滃鎵樺埌鐘舵佺被

瀹炰緥

public class GumballMachine extends State{
    public State noQuarterState = new NoQuarterState(this);
    public State hasQuarterState = new HasQuarterState(this);
    public State soldState = new SoldState(this);
    public State soldOutState = new SoldOutState(this);

    private State state = soldOutState;
    private int candyCount = 0;

    public GumballMachine(int count) {
        this.candyCount = count;
        if(count > 0)
            setState(noQuarterState);
    }

    @Override
    public void insertQuarter() {
        state.insertQuarter();
    }
    @Override
    public void ejectQuarter() {
        state.ejectQuarter();
    }
    @Override
    public void turnCrank() {
        state.turnCrank();
    }
    @Override
    public void dispense() {
        state.dispense();
    }

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void setCandyCount(int candyCount) {
        this.candyCount = candyCount;
    }

    public int getCandyCount() {
        return candyCount;
    }

}

鍙互鍙戠幇锛岃繖绉嶈璁′笅锛岀硸鏋滄満鏍规湰涓嶉渶瑕佹竻妤氱姸鎬佺殑鏀瑰彉锛屽畠鍙敤璋冪敤鐘舵佺殑鏂规硶灏辫銆傜姸鎬佺殑鏀瑰彉鏄湪鐘舵佸唴閮ㄥ彂鐢熺殑銆傝繖灏辨槸"鐘舵佹ā寮"銆

濡傛灉姝ゆ椂鍐嶅鍔犱竴绉嶇姸鎬侊紝绯栨灉鏈轰笉闇瑕佸仛浠讳綍鏀瑰彉锛屾垜浠彧闇瑕佸啀澧炲姞涓涓姸鎬佺被锛岀劧鍚庡湪鐩稿叧鐨勭姸鎬佺被鏂规硶閲岄潰澧炲姞杞崲鐨勮繃绋嬪嵆鍙

涓嶇悊瑙o紵杩欒繕鏈変竴绡囧叧浜庣姸鎬佹ā寮忕殑鏂囩珷: http://dzone.com/articles/state-pattern-simplified

鍘熸枃閾炬帴锛歨ttp://blog.csdn.net/z55887/article/details/73198039

鍘熸枃閾炬帴锛歨ttp://blog.csdn.net/z55887/article/details/60608898