代码重构-以贪吃蛇为示例(五)-封装Scoring和SpeedManager
程序员文章站
2022-05-21 14:00:36
...
继续题外话:
上一篇忘记发布到博客频道了,都没有人看到,这次一定不要忘记了,上一篇发的牢骚要看哦
/* --------------------------------------------正文分割线------------------------------------- */
记录分数的时候要更新视图(虽然现在还没显示分数,但是先做个准备),另外一半速度也跟它有关,或者会有什么奖励提示之类的东西,所以说分数的更新需要做很多操作,谈到这里,我想有些人已经想到了观察者模式(Observer),没错,就是用观察者模式。
Scoring接口,用来记录分数,在模式中充当Subject角色;
SimpleScoring类,实现Scoring,简单记录分数;
ScoreChengeListener接口,观察者(或者说是监听器),监听分数更改事件做出相应处理;
SpeedManager类,速度管理器,可以设置速度,控制时间(具体看代码中的isTimeUp方法);
ScoreSpeedManager类,继承SpeedManager,监听分数来修改速度。
package snakes; /** * 计分器,用来记录分数,可以添加 <code>{@link ScoreChangeListener}</code> 获取分数更新 * * @author Chris * @see ScoreChangeListener */ public interface Scoring { /** * 添加 <code> ScoreChangeListener </code> * * @see ScoreChangeListener */ public void addScoreChangeListener(ScoreChangeListener listener); /** * 获取分数 * * @return */ public int getScore(); /** * 删除 <code> ScoreChangeListener </code> * * @see ScoreChangeListener */ public void removeScoreChangeListener(ScoreChangeListener listener); /** * 重置分数 */ public void reset(); /** * 计分 * * @return */ public void scoring(); /** * 设置分数值 * * @param score * @return */ public void setScore(int score); }
package snakes; import java.util.HashSet; import java.util.Set; public class SimpleScoring implements Scoring { private int score; private Set<ScoreChangeListener> listeners; public SimpleScoring() { this(0); } /** * 构造函数,设置初始分数 * @param score */ public SimpleScoring(int score) { this.score = score; listeners = new HashSet<ScoreChangeListener>(); } @Override public void addScoreChangeListener(ScoreChangeListener listener) { listeners.add(listener); } @Override public int getScore() { return score; } private void notifyScoreChange() { for(ScoreChangeListener lis:listeners) { lis.scoreChanged(score); } } @Override public void removeScoreChangeListener(ScoreChangeListener listener) { listeners.remove(listener); } @Override public void reset() { score = 0; notifyScoreChange(); } @Override public void scoring() { ++score; notifyScoreChange(); } @Override public void setScore(int score) { this.score = score; notifyScoreChange(); } }
package snakes; /** * 用来监听分数更改事件的类 * * @author Chris * @see Scoring */ public interface ScoreChangeListener { public void scoreChanged(int score); }
package snakes; public class SpeedManager { private volatile int speed; private long curTime = System.currentTimeMillis(); /** * * @param speed */ public SpeedManager(int speed) { if (speed <= 0) throw new IllegalArgumentException("speed must be positive"); this.speed = speed; } /** * 手动更新时间点 */ public void frashTime() { curTime = System.currentTimeMillis(); } public int getSpeed() { return speed; } /** * 判断是否到下一个更新时间点,如果是则更新时间点 * * @return */ public synchronized boolean isTime() { long time = System.currentTimeMillis(); if (time - curTime > (1000 / speed)) { curTime = time; return true; } return false; } public void setSpeed(int speed) { if (speed <= 0) throw new IllegalArgumentException("speed must be positive"); this.speed = speed; } }
package snakes; public class ScoreSpeedManager extends SpeedManager implements ScoreChangeListener { public ScoreSpeedManager(int speed) { super(speed); } @Override public void scoreChanged(int score) { setSpeed(score+1); } }
上一篇: PHP 单引号与双引号的区别
下一篇: 代码重构-以贪吃蛇为示例(四)-继续封装