• DOTA版设计模式——备忘录


    备忘录模式,最直接的就是记录游戏进度了,如果有个18关的游戏不能记录游戏进度,每次都必须从第一关开始玩,那你是不是要放弃游戏了,我肯定会的,嘿嘿。
    在C#中序列化使我们能够更方便的完成备忘录模式,UML图先给大家一个概念:

    具体代码见完整代码。
    测试代码:
                DotaPatternLibrary.Memento.Game game = new DotaPatternLibrary.Memento.Game();
                game.GameLevel = 1;
                LandpyForm.Form.OutputResult("GameLevel:" + game.GameLevel);
                LandpyForm.Form.OutputResult("Ten minute left");
                game.GameLevel = 5;
                LandpyForm.Form.OutputResult("GameLevel:" + game.GameLevel);
                LandpyForm.Form.OutputResult("SaveGame");
                DotaPatternLibrary.Memento.GameProgress gameProgress = game.GetGameProgress();
                System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                System.IO.FileStream fsObj = new System.IO.FileStream("GameProgress.dat", System.IO.FileMode.Create);
                formatter.Serialize(fsObj, gameProgress);
                fsObj.Close();
                LandpyForm.Form.OutputResult("Save OK");
                Thread.Sleep(3000);
                LandpyForm.Form.OutputResult("3 second left");

                System.IO.FileStream fsObj2 = new System.IO.FileStream("GameProgress.dat", System.IO.FileMode.Open);
                gameProgress = formatter.Deserialize(fsObj2) as DotaPatternLibrary.Memento.GameProgress;
                DotaPatternLibrary.Memento.Game game2 = new DotaPatternLibrary.Memento.Game();
                LandpyForm.Form.OutputResult("GameLevel:" + game2.GameLevel);
                LandpyForm.Form.OutputResult("LoadGame");
                game2.SetProgress(gameProgress);
                LandpyForm.Form.OutputResult("GameLevel:" + game2.GameLevel);
    完整代码:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization;
    using System.IO;

    namespace DotaPatternLibrary.Memento
    {
        [Serializable]
        
    public class Game
        {
            
    private int _gameLevel;

            
    public int GameLevel
            {
                
    get { return _gameLevel; }
                
    set { _gameLevel = value; }
            }

            
    public GameProgress GetGameProgress()
            {
                
    return new GameProgress(this);
            }
            
    public void SetProgress(GameProgress gameProgress)
            {
                
    this._gameLevel = gameProgress.game.GameLevel;
            }
        }

        [Serializable]
        
    public class GameProgress
        {
            
    public Game game;
            
    public GameProgress(Game game)
            {
                
    this.game = game;
            }
        }
    }
  • 相关阅读:
    Entity Framework 批量操作
    Tsak多线程学习记录
    .net webservice 动态更换地址
    .NET EF Core2.0 (DBFirst)使用与配置
    MVC发布IIS后提示未配置默认文档
    未能加载文件或程序集“Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。
    Redis教程
    使用git将项目上传到github
    Redis集群(一)搭建Cluster模式[超简单]
    Redis 常见5大数据类型结构,附录3个数据类型
  • 原文地址:https://www.cnblogs.com/pangxiaoliang/p/1531554.html
Copyright © 2020-2023  润新知