AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁(yè) > 科技  > 軟件

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

來(lái)源: 責(zé)編: 時(shí)間:2023-10-08 07:06:13 299觀看
導(dǎo)讀背景你(Caretaker)在玩一個(gè)游戲,可以對(duì)游戲進(jìn)行存檔(Memento),以便后面遇到不符合預(yù)期的游戲場(chǎng)景,通過(guò)存檔管理(Originator)恢復(fù)。模式定義Without violating encapsulation,capture and externalize an object's internal stat

背景

你(Caretaker)在玩一個(gè)游戲,可以對(duì)游戲進(jìn)行存檔(Memento),以便后面遇到不符合預(yù)期的游戲場(chǎng)景,通過(guò)存檔管理(Originator)恢復(fù)。dB528資訊網(wǎng)——每日最新資訊28at.com

dB528資訊網(wǎng)——每日最新資訊28at.com

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

1.定義游戲狀態(tài)類dB528資訊網(wǎng)——每日最新資訊28at.com

package com.example.designpattern.memento.domain;import lombok.AllArgsConstructor;import lombok.Data;/** * 游戲狀態(tài) * * @author hongcunlin */@Data@AllArgsConstructorpublic class GameState {    /**     * 經(jīng)驗(yàn)值     */    private int exp;    /**     * 等級(jí)     */    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.定義游戲存檔類dB528資訊網(wǎng)——每日最新資訊28at.com

package com.example.designpattern.memento.domain;import lombok.AllArgsConstructor;import lombok.Data;/** * 游戲存檔 * * @author hongcunlin */@Data@AllArgsConstructorpublic class GameMemento {    /**     * 經(jīng)驗(yàn)值     */    private int exp;    /**     * 等級(jí)     */    private int level;}

3.定義游戲存檔管理接口dB528資訊網(wǎng)——每日最新資訊28at.com

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)游戲存檔管理接口dB528資訊網(wǎng)——每日最新資訊28at.com

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.測(cè)試dB528資訊網(wǎng)——每日最新資訊28at.com

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;/** * 備忘錄模式測(cè)試 * * @author hongcunlin */@SpringBootTestpublic 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);    }}

可以看到,游戲是可以正常會(huì)退到存檔的內(nèi)容的dB528資訊網(wǎng)——每日最新資訊28at.com

dB528資訊網(wǎng)——每日最新資訊28at.com

回顧

本文通過(guò)游戲的存檔、回退存檔,抽象出與符合的設(shè)計(jì)模式——備忘錄模式。同時(shí),與傳統(tǒng)Java的說(shuō)教不同,本次的實(shí)現(xiàn)基于企業(yè)開發(fā)必用的Spring框架,貼近實(shí)際開發(fā)場(chǎng)景。dB528資訊網(wǎng)——每日最新資訊28at.com

不過(guò)備忘錄模式在實(shí)際開發(fā)中的應(yīng)用并不多,很少有數(shù)據(jù)存儲(chǔ)在服務(wù)器容器運(yùn)行的內(nèi)存中,而是會(huì)將數(shù)據(jù)存儲(chǔ)到專門的數(shù)據(jù)庫(kù)中,如磁盤型數(shù)據(jù)庫(kù)MySQL,內(nèi)存型數(shù)據(jù)庫(kù)Redis等。dB528資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-12374-0.html【設(shè)計(jì)模式】通過(guò)游戲存檔了解備忘錄模式

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 【設(shè)計(jì)模式】通過(guò)蘋果的隔空投送了解中介者模式

下一篇: 一套基于 .NET Core 開發(fā)的支付SDK集 - paylink

標(biāo)簽:
  • 熱門焦點(diǎn)
Top