当前位置: 首页 > news >正文

网站设计的公司皆选奇点网络做做网站

网站设计的公司皆选奇点网络,做做网站,网络公司哪个最好,自己做网站引用别人的电影备忘录模式(Memento Pattern)是一种行为设计模式,它允许在不破坏封装性的前提下,捕获并外部化一个对象的内部状态,以便以后可以恢复到这个状态。 核心概念 设计原则 备忘录模式遵循以下设计原则: 封装性…

备忘录模式(Memento Pattern)是一种行为设计模式,它允许在不破坏封装性的前提下,捕获并外部化一个对象的内部状态,以便以后可以恢复到这个状态。

核心概念

设计原则

备忘录模式遵循以下设计原则:

  1. 封装性原则:不暴露对象内部实现细节

  2. 单一职责原则:将状态保存与恢复职责分离

  3. 开闭原则:可以引入新的备忘录类型而不修改原始类

主要优点

  1. 状态保存:可以轻松实现撤销/恢复功能

  2. 封装保护:不破坏对象的封装性

  3. 简化原发器:将状态管理职责分离

  4. 时间快照:支持创建多个时间点的状态快照

模式结构

主要组件

  1. Originator(原发器)

    • 需要保存状态的对象

    • 创建备忘录以保存当前状态

    • 使用备忘录恢复状态

  2. Memento(备忘录)

    • 存储原发器的内部状态

    • 防止原发器以外的对象访问

  3. Caretaker(管理者)

    • 负责保存备忘录

    • 不操作或检查备忘录内容

完整代码示例

#include <iostream>
#include <string>
#include <vector>
#include <memory>// ==================== 备忘录类 ====================
class EditorMemento {// 备忘录保存的状态std::string content_;// 只允许Editor类创建和访问备忘录friend class Editor;// 私有构造函数,确保只有Editor可以创建备忘录explicit EditorMemento(const std::string& content) : content_(content) {}public:// Editor可以通过此方法获取保存的状态std::string getContent() const { return content_; }
};// ==================== 原发器类 ====================
class Editor {std::string content_;public:void type(const std::string& words) {content_ += words;}std::string getContent() const {return content_;}// 创建备忘录:保存当前状态std::unique_ptr<EditorMemento> save() {return std::make_unique<EditorMemento>(content_);}// 恢复状态:从备忘录恢复void restore(const EditorMemento* memento) {content_ = memento->getContent();}
};// ==================== 管理者类 ====================
class History {std::vector<std::unique_ptr<EditorMemento>> mementos_;Editor* editor_;public:explicit History(Editor* editor) : editor_(editor) {}void backup() {std::cout << "保存状态..." << std::endl;mementos_.push_back(editor_->save());}void undo() {if (mementos_.empty()) return;auto memento = std::move(mementos_.back());mementos_.pop_back();std::cout << "恢复状态..." << std::endl;editor_->restore(memento.get());}void showHistory() const {std::cout << "\n历史记录:" << std::endl;for (size_t i = 0; i < mementos_.size(); ++i) {std::cout << i+1 << ". " << mementos_[i]->getContent().substr(0, 10) << (mementos_[i]->getContent().length() > 10 ? "..." : "")<< std::endl;}}
};// ==================== 客户端代码 ====================
int main() {std::cout << "=== 文本编辑器撤销功能演示 ===" << std::endl;Editor editor;History history(&editor);// 编辑并保存状态editor.type("Hello, ");history.backup();editor.type("world! ");history.backup();editor.type("This is a memento pattern example.");std::cout << "\n当前内容: " << editor.getContent() << std::endl;// 显示历史history.showHistory();// 撤销一次history.undo();std::cout << "\n撤销后内容: " << editor.getContent() << std::endl;// 再撤销一次history.undo();std::cout << "再撤销后内容: " << editor.getContent() << std::endl;return 0;
}

模式变体

1. 增量备忘录

class IncrementalMemento {std::string change_; // 只保存变化部分friend class Editor;explicit IncrementalMemento(const std::string& change) : change_(change) {}
};class Editor {std::string content_;std::vector<std::unique_ptr<IncrementalMemento>> changes_;public:void type(const std::string& words) {changes_.push_back(std::make_unique<IncrementalMemento>(words));content_ += words;}void undo() {if (changes_.empty()) return;auto lastChange = changes_.back()->getChange();content_.erase(content_.length() - lastChange.length());changes_.pop_back();}
};

2. 多状态备忘录

class FullStateMemento {std::string content_;int cursorPosition_;std::vector<std::string> openFiles_;// 可以保存更多状态...friend class IDE;FullStateMemento(const std::string& content, int pos, const std::vector<std::string>& files): content_(content), cursorPosition_(pos), openFiles_(files) {}
};class IDE {// 各种状态...
public:std::unique_ptr<FullStateMemento> save() {return std::make_unique<FullStateMemento>(content_, cursorPos_, openFiles_);}
};

实际应用场景

  1. 文本编辑器:实现撤销/重做功能

  2. 游戏开发:保存游戏进度和存档

  3. 图形软件:绘图步骤的撤销

  4. 事务管理:操作回滚

  5. 配置管理:系统配置的保存和恢复

http://www.dtcms.com/a/421153.html

相关文章:

  • 免费的外网服务器seo顾问服务咨询
  • html5和ria网站设计头条权重查询站长工具
  • 网站建设部门wordpress 自动加载下一页
  • 蓝色 网站威海建设公司网站
  • 做网站买域名怎么弄网络接入服务商是什么意思
  • 企业展示类网站模板口碑营销成功的案例
  • 个人网站怎么做口碑不属于网页制作工具
  • 那些cps网站做的比较好成交型网站建设公司
  • 网站建设客户来源环球贸易网网址
  • 如何在电脑里做网站网站的反链怎么做
  • 做网站开发一般用什么语言中国国际进口博览会上海
  • 国外做mg动画的网站大全例点估算网站开发项目工作量
  • 经典 网站wordpress 宕机原因
  • 河北保定建设集团招聘信息网站哪里有网站培训的
  • 做网页大概需要多少钱株洲企业关键词优化最新报价
  • 搭建钓鱼网站教程站长seo计费系统
  • 广州自助网站设计平台商城网站建设都需要多少钱
  • 网站建设是自学好还是小程序搭建需要多久
  • php做音乐网站温州网站开发多少钱
  • 专门做房地产设计的图片网站好网站建设公司哪家好?
  • 湖口县建站公司没钱可以注册一千万的公司吗
  • 查看自己网站访问量搞定在线图片编辑
  • 沈阳酒店企业网站制作公司如何营销推广
  • 如何自己做网站域名备案和网站备案
  • 网站联系方式设置要求苏州网站建设模版
  • 株洲网站做的好的公司博兴网站建设招聘
  • 郑州flash网站建设红酒网站制作
  • 桂林出网站一个app开发大概要多少钱
  • 白银市城县建设局网站dede网站架设教程
  • 官方网站作用抖音网站怎么做