偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

【設(shè)計模式】通過游戲存檔了解備忘錄模式

開發(fā) 前端
本文通過游戲的存檔、回退存檔,抽象出與符合的設(shè)計模式——備忘錄模式。同時,與傳統(tǒng)Java的說教不同,本次的實(shí)現(xiàn)基于企業(yè)開發(fā)必用的Spring框架,貼近實(shí)際開發(fā)場景。

背景

你(Caretaker)在玩一個游戲,可以對游戲進(jìn)行存檔(Memento),以便后面遇到不符合預(yù)期的游戲場景,通過存檔管理(Originator)恢復(fù)。

模式定義

Without violating encapsulation,capture and externalize an object's internal state so that the object can be restored to this state later.

在不違反封裝的情況下,捕獲并外部化對象的內(nèi)部狀態(tài),以便以后可以將對象恢復(fù)到該狀態(tài)。

模式結(jié)構(gòu)

模式實(shí)現(xiàn)

1.定義游戲狀態(tài)類

package com.example.designpattern.memento.domain;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 游戲狀態(tài)
 *
 * @author hongcunlin
 */
@Data
@AllArgsConstructor
public class GameState {
    /**
     * 經(jīng)驗(yàn)值
     */
    private int exp;

    /**
     * 等級
     */
    private int level;

    /**
     * 存檔
     *
     * @return 存檔
     */
    public GameMemento save() {
        return new GameMemento(exp, level);
    }

    /**
     * 加載存檔
     *
     * @param gameMemento 存檔
     */
    public void restore(GameMemento gameMemento) {
        exp = gameMemento.getExp();
        level = gameMemento.getLevel();
    }
}

2.定義游戲存檔類

package com.example.designpattern.memento.domain;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 游戲存檔
 *
 * @author hongcunlin
 */
@Data
@AllArgsConstructor
public class GameMemento {
    /**
     * 經(jīng)驗(yàn)值
     */
    private int exp;

    /**
     * 等級
     */
    private int level;
}

3.定義游戲存檔管理接口

package com.example.designpattern.memento.manager;

import com.example.designpattern.memento.domain.GameMemento;

/**
 * 游戲存檔管理
 *
 * @author hongcunlin
 */
public interface GameSaveManger {
    /**
     * 保持存檔
     *
     * @param memento 存檔
     */
    void addSave(GameMemento memento);

    /**
     * 獲取存檔
     *
     * @param index 索引
     * @return 存檔
     */
    GameMemento getSave(int index);
}

4.實(shí)現(xiàn)游戲存檔管理接口

package com.example.designpattern.memento.manager.impl;

import com.example.designpattern.memento.domain.GameMemento;
import com.example.designpattern.memento.manager.GameSaveManger;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 游戲管理
 *
 * @author hongcunlin
 */
@Component("gameSaveManger")
public class GameSaveMangerImpl implements GameSaveManger {
    /**
     * 存檔列表
     */
    private final List<GameMemento> saves = new ArrayList<>();

    /**
     * 保持存檔
     *
     * @param memento 存檔
     */
    @Override
    public void addSave(GameMemento memento) {
        saves.add(memento);
    }

    /**
     * 獲取存檔
     *
     * @param index 索引
     * @return 存檔
     */
    @Override
    public GameMemento getSave(int index) {
        return saves.get(index);
    }
}

5.測試

package com.example.designpattern.memento;

import com.example.designpattern.memento.domain.GameState;
import com.example.designpattern.memento.manager.GameSaveManger;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * 備忘錄模式測試
 *
 * @author hongcunlin
 */
@SpringBootTest
public class DesignPatternTest {
    /**
     * 游戲存檔管理
     */
    @Resource(name = "gameSaveManger")
    private GameSaveManger gameSaveManger;

    @Test
    public void test() {
        // 開始游戲
        GameState gameState = new GameState(1, 1);

        // 存檔游戲
        gameSaveManger.addSave(gameState.save());

        // 玩游戲
        gameState.setExp(2);
        gameState.setLevel(2);

        // 加載存檔
        gameState.restore(gameSaveManger.getSave(0));
        System.out.println(gameState);
    }
}

可以看到,游戲是可以正常會退到存檔的內(nèi)容的

回顧

本文通過游戲的存檔、回退存檔,抽象出與符合的設(shè)計模式——備忘錄模式。同時,與傳統(tǒng)Java的說教不同,本次的實(shí)現(xiàn)基于企業(yè)開發(fā)必用的Spring框架,貼近實(shí)際開發(fā)場景。

不過備忘錄模式在實(shí)際開發(fā)中的應(yīng)用并不多,很少有數(shù)據(jù)存儲在服務(wù)器容器運(yùn)行的內(nèi)存中,而是會將數(shù)據(jù)存儲到專門的數(shù)據(jù)庫中,如磁盤型數(shù)據(jù)庫MySQL,內(nèi)存型數(shù)據(jù)庫Redis等。

責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2020-11-02 10:41:33

備忘錄模式

2023-10-31 09:07:16

備忘錄模式保存

2024-05-15 17:41:37

備忘錄模式多線程

2023-04-19 08:03:52

Go設(shè)計模式

2025-02-17 14:48:14

2018-12-24 21:40:12

2014-04-17 10:30:41

Linux 命令黑白備忘錄

2023-10-10 15:26:30

內(nèi)存泄露OOM

2017-03-21 11:02:59

基礎(chǔ)深度學(xué)習(xí)備忘錄

2011-04-11 10:03:32

錢伯斯思科

2011-08-16 18:38:23

Core Animat動畫

2023-09-26 00:27:07

設(shè)計模式鏈接

2018-06-20 13:14:16

MySQL數(shù)據(jù)優(yōu)化查詢備忘錄

2013-08-29 10:50:48

移動網(wǎng)站性能優(yōu)化移動web

2016-03-03 10:09:26

2021-03-08 00:12:44

Grid 備忘錄 函數(shù)

2011-12-07 09:19:49

JavaJ2MEBicaVM

2023-10-07 00:17:06

AirDrop中介者模式

2009-08-14 15:50:45

C#正則表達(dá)式

2009-06-17 16:54:27

MySpace備忘錄裁員
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號