欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 备忘录模式(大话设计模式)C/C++版本

备忘录模式(大话设计模式)C/C++版本

2024/12/24 11:30:25 来源:https://blog.csdn.net/m0_47104421/article/details/139842605  浏览:    关键词:备忘录模式(大话设计模式)C/C++版本

备忘录模式

在这里插入图片描述

C++

#include <iostream>
#include <string>
using namespace std;// Memento类,备忘录,此处为角色状态存储箱
class RoleStateMemento
{
private:int m_vit; // 生命力int m_atk; // 攻击力int m_def; // 防御力
public:RoleStateMemento(int vit, int atk, int def) : m_vit(vit), m_atk(atk), m_def(def) {} // 把状态存入 备忘录中void setVitality(int vit) { m_vit = vit; }int getVitality() { return m_vit; }void setAttack(int atk) { m_atk = atk; }int getAttack() { return m_atk; }void setDefense(int def) { m_def = def; }int getDefense() { return m_def; }
};// Originator,发起人,此处为游戏角色
class GameRole
{
private:int m_vit; // 生命力int m_atk; // 攻击力int m_def; // 防御力public:RoleStateMemento *SaveState(){ // 保存角色状态return (new RoleStateMemento(m_vit, m_atk, m_def));}void RecoveryState(RoleStateMemento *memento) // 恢复角色状态{m_vit = memento->getVitality();m_atk = memento->getAttack();m_def = memento->getDefense();}// 获得初始状态void GetInitState(){m_atk = m_def = m_vit = 100;}void Fight(){m_atk = m_def = m_vit = 0;}void StateDisplay(){cout << "角色当前状态:" << endl;cout << "体力:" << m_vit << " "<< "攻击力:" << m_atk << " "<< "防御力:" << m_def << endl;}
};
// Caretaker,管理者,此处为游戏角色管理类
class RoleStateCaretaker
{
private:RoleStateMemento *m_memento;public:~RoleStateCaretaker(){delete m_memento;}void setMemento(RoleStateMemento *memento){m_memento = memento;}RoleStateMemento *getMemento(){return m_memento;}
};
int main()
{// 大战Boss前GameRole *lixiaoyao = new GameRole;lixiaoyao->GetInitState();lixiaoyao->StateDisplay();cout << "\n大战Boss中..." << endl;// 保存进度RoleStateCaretaker *stateAdmin = new RoleStateCaretaker;stateAdmin->setMemento(lixiaoyao->SaveState());// 大战Boss时,损耗严重lixiaoyao->Fight();lixiaoyao->StateDisplay();cout << "\n大战Boss后,恢复进度..." << endl;// 恢复之前状态lixiaoyao->RecoveryState(stateAdmin->getMemento());lixiaoyao->StateDisplay();delete lixiaoyao;delete stateAdmin;return 0;
}

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>// Memento结构体,用于存储游戏角色的状态
typedef struct
{int vitality;int attack;int defense;
} RoleStateMemento;// 创建备忘录
RoleStateMemento *create_memento(int vit, int atk, int def)
{RoleStateMemento *memento = (RoleStateMemento *)malloc(sizeof(RoleStateMemento));memento->vitality = vit;memento->attack = atk;memento->defense = def;return memento;
}// 角色结构体
typedef struct
{int vitality;int attack;int defense;
} GameRole;// 初始化角色状态
void init_game_role(GameRole *role)
{role->vitality = 100;role->attack = 100;role->defense = 100;
}// 保存角色状态
RoleStateMemento *save_state(GameRole *role)
{return create_memento(role->vitality, role->attack, role->defense);
}// 恢复角色状态
void restore_state(GameRole *role, RoleStateMemento *memento)
{role->vitality = memento->vitality;role->attack = memento->attack;role->defense = memento->defense;
}// 显示角色状态
void display_state(GameRole *role)
{printf("角色当前状态:\n");printf("体力:%d 攻击力:%d 防御力:%d\n", role->vitality, role->attack, role->defense);
}// 管理者结构体,用于管理备忘录
typedef struct
{RoleStateMemento *memento;
} RoleStateCaretaker;// 设置备忘录
void caretaker_set_memento(RoleStateCaretaker *caretaker, RoleStateMemento *memento)
{caretaker->memento = memento;
}// 获取备忘录
RoleStateMemento *caretaker_get_memento(RoleStateCaretaker *caretaker)
{return caretaker->memento;
}// 释放角色状态和管理者
void cleanup(GameRole *role, RoleStateCaretaker *caretaker)
{free(caretaker->memento);free(caretaker);// 注意:此处未释放role的内存,因为在实际应用中可能需要继续使用或有其他管理机制
}int main()
{GameRole lixiaoyao;RoleStateCaretaker *stateAdmin = (RoleStateCaretaker *)malloc(sizeof(RoleStateCaretaker));init_game_role(&lixiaoyao);display_state(&lixiaoyao);printf("\n大战Boss中...\n");// 保存进度caretaker_set_memento(stateAdmin, save_state(&lixiaoyao));// 大战Boss后,状态受损lixiaoyao.vitality = lixiaoyao.attack = lixiaoyao.defense = 0;display_state(&lixiaoyao);printf("\n大战Boss后,恢复进度...\n");// 恢复状态restore_state(&lixiaoyao, caretaker_get_memento(stateAdmin));display_state(&lixiaoyao);cleanup(&lixiaoyao, stateAdmin);return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com