再次通過長達半小時的深思熟慮,對HandCard類系的重構是必然的,敏捷開發,就是得不停的提出想出更好的結構。下面咱們看下HandCard抽象類的實現:
java
package org.tunie.game.doudizhu.card; /** * <p>Title: HandCard.java</p> * <p>Description:一手牌 </p> * <p>Copyright: Copyright (c) 2007</p> * @author Tunie * @date 2014年9月13日 * @version 1.0 */ public abstract class HandCard { private int type; private int score; private CardGroup cardGroup; public HandCard() { } public int getType() { return type; } protected void setType(int type) { this.type = type; } public int getScore() { return score; } protected void setScore(int score) { this.score = score; } public CardGroup getCardGroup() { return cardGroup; } protected void setCardGroup(CardGroup cardGroup) { this.cardGroup = cardGroup; setScore(cardGroup.getCard(0).getType()); } /** * 判斷牌型 */ public final boolean judge(CardGroup cardGroup) { boolean result = false; if(determineConditions(cardGroup)) { result = judgeResult(cardGroup); if(result) { setCardGroup(cardGroup); } } return result; } /** * 判斷條件 * @return */ protected abstract boolean determineConditions(CardGroup cardGroup); /** * 判斷結果 * @return */ protected abstract boolean judgeResult(CardGroup cardGroup); }
這裏不但將以前的judge重載方法出掉了,還引進了模板方法設計模式。這樣優化後的好處就不說了,反正是好處多多,能夠從下面列舉的的幾個類能夠看出端倪。設計模式
Pairide
/** * <p>Title: Pair.java</p> * <p>Description: 對子</p> * <p>Copyright: Copyright (c) 2007</p> * @author Tunie * @date 2014年9月15日 * @version 1.0 */ public class Pair extends HandCard{ public Pair() { setType(HandCardType.PAIR); } @Override protected boolean determineConditions(CardGroup cardGroup) { return determineConditions(cardGroup , 2); } protected boolean determineConditions(CardGroup cardGroup ,int size) { return cardGroup.size() == size; } @Override protected boolean judgeResult(CardGroup cardGroup) { return cardGroup.isSame(); } }
我在Pair這個類中,新增了一個determineConditions的重載方法,這樣作主要是爲了對Three,Bomb的擴展使用。測試
ThreePair
優化
/** * <p>Title: ThreePair.java</p> * <p>Description:三帶對版型 </p> * <p>Copyright: Copyright (c) 2007</p> * @author Tunie * @date 2014年9月16日 * @version 1.0 */ public class ThreePair extends HandCard { public ThreePair() { setType(HandCardType.THREE_PAIR); } @Override protected boolean determineConditions(CardGroup cardGroup) { return cardGroup.size() == 5; } @Override protected boolean judgeResult(CardGroup cardGroup) { cardGroup.doSort(CardGroupSortType.PAIR_PRECEDENCE); return cardGroup.subCardGroup(0, 3).isSame() && cardGroup.subCardGroup(3, 5).isSame(); } }
這個是剛纔新增的類,是對三帶對牌型的判斷。this
下面就看下引發這一系列重構的元兇,Junko類
設計
/** * <p>Title: Junko.java</p> * <p>Description: 順子</p> * <p>Copyright: Copyright (c) 2007</p> * @author Tunie * @date 2014年9月16日 * @version 1.0 */ public class Junko extends HandCard { public Junko() { setType(HandCardType.JUNKO); } @Override protected boolean determineConditions(CardGroup cardGroup) { return cardGroup.size() > 5; } @Override protected boolean judgeResult(CardGroup cardGroup) { cardGroup.doSort(CardGroupSortType.DECREASE); boolean result = true; int score = cardGroup.getCard(0).getType(); for(int i = 1; i <cardGroup.size() ; i ++ , score --) { if(score != cardGroup.getCard(i).getType()){ result = false; break; } } return result; } }
這也是個尚未進行測試的類,很差意思。code