美文网首页
设计模式系列:备忘录模式、状态模式

设计模式系列:备忘录模式、状态模式

作者: coder_liu | 来源:发表于2017-08-18 23:58 被阅读0次

    场景

    腾讯最近有一款手机游戏特别火:王者荣耀。小编是游戏小白,只知道有这么个游戏。但是,听别人说玩游戏下次再进来游戏,进来的是上一个关卡。

    包括,我们看视频的时候也是,一个视频看到一半的时候,退出软件或关闭浏览器。再次进来的时候是近类似于上次观看的点。

    实现视频恢复到上次观看场景。

    分析

    备忘点包含:视频id,用户名id,最新片段时间点

    视频进程:watch方法,reset方法,backup方法

    代码

    
    public class Main {
        public static void  main(String[] args) {
            BackupManager manager = new BackupManager();
            PersonTV personTV = new PersonTV("tvid", "userid", 100*60);
            Watcher watcher = new Watcher();
            watcher.setBackupManager(manager);
            watcher.setPersonTV(personTV);
    
            watcher.watchTV();
            try {
                Thread.sleep(57*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            watcher.interupte();
    
            watcher.watchTV();
        }
    }
    //这里是简单的内存存储,真实情况会用redis或数据库等其他存储方式存储
    public class BackupManager {
        private Map<String,Map<String, TVBackup>>  backupMap = new HashMap<String, Map<String, TVBackup>>();
        public void backup(TVBackup backup) {
            Map<String, TVBackup> backupUserMap = backupMap.get(backup.getUserId());
            if(backupUserMap==null) {
                backupUserMap = new HashMap<String, TVBackup>();
                backupMap.put(backup.getUserId(), backupUserMap);
            }
            backupUserMap.put(backup.getTvId(), backup);
    
        }
        public TVBackup get(String userId, String tvid) {
            Map<String, TVBackup> backupUserMap = backupMap.get(userId);
            if(backupUserMap == null) {
                return null;
            }
            return backupUserMap.get(tvid);
        }
    }
    //get set 篇幅长,省略
    public class PersonTV {
    
        private String tvid;
    
        private String userid;
    
        private long currentTime;
    
        private long totalTime;
    
        public PersonTV(String tvid, String userid, long totalTime) {
            this.tvid = tvid;
            this.userid = userid;
            this.totalTime = totalTime;
        }
    }
    //get set 篇幅长,省略
    public class TVBackup {
    
        private String tvId;
    
        private String userId;
    
        private long time;
    
        public TVBackup(String tvId, String userId, long time) {
            this.tvId = tvId;
            this.userId = userId;
            this.time = time;
        }
    }
    
    public class Watcher {
    
        private static final long backupTime = 1000l;
        private BackupManager manager;
    
        private PersonTV personTV;
    
        private Timer timer;
    
        private void saveTVState(PersonTV person) {
            TVBackup backUp = new TVBackup(person.getTvid(), person.getUserid(), person.getCurrentTime());
            manager.backup(backUp);
        }
    
        private  void recoveryTVState() {
          TVBackup backup = manager.get(personTV.getUserid(), personTV.getTvid());
          if(backup == null) {
              return;
          }
            personTV.setCurrentTime(backup.getTime());
        }
    
        public void setPersonTV(PersonTV personTV) {
            this.personTV = personTV;
        }
    
        public void watchTV() {
            this.timer = new Timer();
            final Timer timer = this.timer;
            final PersonTV tv = personTV;
            recoveryTVState();
            System.out.println("我们看到第"+personTV.getCurrentTime()+"秒了");
            timer.schedule(new TimerTask(){
                /**
                 * The action to be performed by this timer task.
                 */
                public void run() {
                  long currentTime = tv.getCurrentTime();
                  long totalTime = tv.getTotalTime();
                  currentTime++;
                  if (currentTime > totalTime){
                      timer.cancel();
                  }
                    System.out.println("我们看到第"+currentTime+"秒了");
                  tv.setCurrentTime(currentTime);
                  if(currentTime%30== 0) {
                      saveTVState(personTV);
                  }
                }
            }, 0, 1000);
        }
        public void interupte(){
            timer.cancel();
        }
        public void setBackupManager(BackupManager manager) {
            this.manager = manager;
        }
    }
    

    定义

    在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态

    类图

    屏幕快照 2017-08-18 下午11.54.20.png

    场景

    电梯是现在楼房的业主必不可少的工具,因为没有就得用爬的上20楼。哈哈,累死人了!!今天就是要实现人按电梯,上电梯,电梯到站的场景。

    代码

    public abstract class ElevatorState {
        protected  Elevator elevator;
        public void setElevator(Elevator elevator) {
            this.elevator = elevator;
        }
        public abstract void open();
        public abstract  void close();
        public abstract void run();
        public abstract void stop();
    }
    
    public class OpenningElvatorState extends ElevatorState {
        public void open() {
            System.out.println("电梯开启了");
        }
        public void close() {
          super.elevator.setState(Elevator.closingElvatorState);
          super.elevator.close();
        }
        public void run() {
            //nothing
        }
        public void stop() {
            //nothing
        }
    }
    
    public class RunningElevatorState extends ElevatorState {
        public void open() {
            //nothing
        }
        public void close() {
            //nothing
        }
        public void run() {
            System.out.println("电梯开始跑咯");
        }
        public void stop() {
            super.elevator.setState(Elevator.stoppingElvatorState);
            super.elevator.stop();
        }
    }
    
    public class StoppingElevatorState extends ElevatorState {
        public void open() {
          super.elevator.setState(Elevator.openningElvatorState);
          super.elevator.open();
        }
        public void close() {
           //nothing
        }
        public void run() {
          super.elevator.setState(Elevator.runningElvatorState);
          super.elevator.run();
        }
        public void stop() {
          System.out.println("电梯停了");
        }
    }
    
    public class ClosingElevatorState extends ElevatorState {
        public void open() {
            super.elevator.setState(Elevator.openningElvatorState);
            super.elevator.open();
        }
        public void close() {
          System.out.println("电梯门关闭了");
        }
        public void run() {
            super.elevator.setState(Elevator.runningElvatorState);
            super.elevator.run();
        }
        public void stop() {
          //nothing
        }
    }
    
    public class Elevator {
        public final static ElevatorState openningElvatorState = new OpenningElvatorState();
        public final static ElevatorState closingElvatorState = new ClosingElevatorState();
        public final static ElevatorState stoppingElvatorState = new StoppingElevatorState();
        public final static ElevatorState runningElvatorState = new RunningElevatorState();
        private ElevatorState state;
        public void open(){
            state.open();
        }
        public void close(){
           state.close();
        }
        public void run(){
           state.run();
        }
        public void stop(){
           state.stop();
        }
        public void setState(ElevatorState state) {
            this.state = state;
            this.state.setElevator(this);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Elevator elevator = new Elevator();
            elevator.setState(new StoppingElevatorState());
            elevator.run();
            elevator.stop();
            elevator.open();
            elevator.close();
            elevator.run();
            elevator.stop();
        }
    }
    

    定义

    当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

    类图

    屏幕快照 2017-08-18 下午11.54.32.png

    相关文章

      网友评论

          本文标题:设计模式系列:备忘录模式、状态模式

          本文链接:https://www.haomeiwen.com/subject/ykrirxtx.html