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

wordpress建立页面打开404错误百度蜘蛛池自动收录seo

wordpress建立页面打开404错误,百度蜘蛛池自动收录seo,哪家上市公司做视频网站,网页翻译工具备忘录模式(Memento Pattern)是一种行为型设计模式,用于在不破坏封装性的前提下,捕获并保存对象的内部状态,以便在将来的某个时刻可以恢复到之前的状态。备忘录模式的核心是状态的保存和恢复,常用于实现撤销…

备忘录模式(Memento Pattern)是一种行为型设计模式,用于在不破坏封装性的前提下,捕获并保存对象的内部状态,以便在将来的某个时刻可以恢复到之前的状态。备忘录模式的核心是状态的保存和恢复,常用于实现撤销、回滚等功能。

备忘录模式的应用场景

备忘录模式特别适合以下场景:

  1. 撤销/恢复操作:例如文本编辑器中的撤销功能,通过备忘录保存每次操作的状态,用户可以随时回到某个历史状态。

  2. 数据快照:保存对象在某个时刻的快照,以便之后回溯或调试。

  3. 事务管理:在处理复杂的事务时,可以在中间点保存状态,当某个操作失败时,回滚到之前的状态。

备忘录模式的核心

备忘录模式的主要组成部分包括:

  1. 发起者(Originator):负责创建并恢复备忘录,保存当前的状态到备忘录中,或者从备忘录中恢复状态。

  2. 备忘录(Memento):用于存储发起者的内部状态,不对外公开备忘录的实现细节。

  3. 负责人(Caretaker):负责保存和管理备忘录,但不会操作或修改备忘录的内容。它只知道备忘录保存的状态,并在需要时将备忘录传递回发起者进行状态恢复。

备忘录模式强调的是封装性,发起者的内部状态不应该对外暴露,备忘录类也应该避免暴露这些细节。

备忘录模式的示例代码

假设我们在开发一个文本编辑器,并希望提供撤销和恢复功能,每当用户输入一段文本时,我们将保存当前状态,以便用户可以随时撤销操作。

1. 定义发起者、备忘录和负责人

#include <QDebug>
#include <QString>
#include <QStack>// 备忘录类:保存文本编辑器的状态
class Memento {
private:QString state;  // 保存的状态public:Memento(const QString& state) : state(state) {}QString getState() const {return state;  // 返回保存的状态}
};// 发起者类:文本编辑器
class TextEditor {
private:QString text;  // 当前的文本状态public:void setText(const QString& newText) {text = newText;}QString getText() const {return text;}// 创建备忘录,保存当前状态Memento* save() const {return new Memento(text);}// 从备忘录中恢复状态void restore(Memento* memento) {if (memento) {text = memento->getState();}}
};// 负责人类:管理备忘录
class Caretaker {
private:QStack<Memento*> history;  // 保存备忘录的栈public:void saveMemento(Memento* memento) {history.push(memento);  // 保存当前状态的备忘录}Memento* undo() {if (!history.isEmpty()) {Memento* lastState = history.pop();  // 取出最后一个保存的备忘录return lastState;}return nullptr;  // 没有更多历史状态}~Caretaker() {// 清理保存的备忘录while (!history.isEmpty()) {delete history.pop();}}
};// 使用示例
int main() {TextEditor* editor = new TextEditor();Caretaker* caretaker = new Caretaker();// 初始文本editor->setText("Hello");qDebug() << "Current text:" << editor->getText();  // 输出:Current text: Hello// 保存状态caretaker->saveMemento(editor->save());// 用户修改文本editor->setText("Hello, World");qDebug() << "Current text after modification:" << editor->getText();  // 输出:Current text after modification: Hello, World// 再次保存状态caretaker->saveMemento(editor->save());// 用户再次修改文本editor->setText("Hello, Qt!");qDebug() << "Current text after second modification:" << editor->getText();  // 输出:Current text after second modification: Hello, Qt!// 执行撤销操作editor->restore(caretaker->undo());qDebug() << "Current text after undo:" << editor->getText();  // 输出:Current text after undo: Hello, World// 再次执行撤销操作editor->restore(caretaker->undo());qDebug() << "Current text after second undo:" << editor->getText();  // 输出:Current text after second undo: Hello// 清理内存delete editor;delete caretaker;return 0;
}

代码解析

  • Memento类:这是备忘录类,负责保存发起者的状态。在这个例子中,它保存文本编辑器中的文本状态,并通过getState方法提供对状态的访问。

  • TextEditor类:这是发起者类,它拥有当前的文本状态,并且可以创建备忘录来保存当前状态或从备忘录中恢复状态。

  • Caretaker类:这是负责人类,它保存所有的备忘录(通过栈存储历史状态),并在需要时将备忘录返回给发起者进行状态恢复。undo方法从栈中弹出最后保存的状态,模拟撤销操作。

  • 客户端代码:客户端通过修改文本,并在每次修改后保存状态。通过调用Caretakerundo方法,客户端可以恢复到之前的文本状态,模拟撤销操作。

备忘录模式的优点

  • 保存历史状态:备忘录模式允许你保存对象的状态,并在将来恢复这些状态。非常适合实现撤销、恢复、回滚等功能。

  • 封装性好:备忘录类不暴露发起者的内部状态,保证了发起者的封装性。发起者和负责人只通过备忘录进行状态的保存和恢复,而不需要直接操作发起者的状态。

  • 减少耦合:负责人只负责保存和管理备忘录,而不直接参与发起者的逻辑,职责清晰。

备忘录模式的缺点

  • 内存开销大:每次保存对象的状态都需要创建一个新的备忘录对象,尤其是当对象状态非常庞大时,可能会导致大量的内存占用。

  • 实现复杂性:如果对象的状态非常复杂,备忘录模式的实现也会相应复杂,尤其是在需要保存多个部分或大对象时。

适合使用备忘录模式的情况

  • 需要实现撤销/恢复操作:例如文本编辑器、绘图工具、IDE等支持撤销/恢复功能的应用程序。

  • 需要保存对象的历史状态:当系统需要定期保存某些对象的状态以便将来回溯时,可以使用备忘录模式。

  • 需要避免直接暴露内部状态:如果需要在多个地方保存对象的状态,但不想让外界直接访问或修改对象的内部状态,备忘录模式是一个很好的选择。

不适合使用备忘录模式的情况

  • 对象状态非常庞大:如果发起者的状态非常庞大,频繁创建备忘录会带来较大的内存开销,不适合使用备忘录模式。

  • 状态变化频繁:如果对象状态变化频繁,并且每次都需要保存,那么备忘录模式会带来大量性能问题。

Qt中的备忘录模式应用

在Qt开发中,备忘录模式可以用于实现撤销/恢复功能。例如,在一个文本编辑器或绘图工具中,用户的每次操作都可能改变对象的状态,这些操作可以通过备忘录模式保存下来,并在需要时回滚或恢复。Qt中有些类(如QUndoStack)可以直接实现类似的撤销功能,它们内部也可能应用了备忘录模式的思想。

总结

备忘录模式通过保存对象的状态并在将来进行恢复,使得系统能够实现撤销、回滚等功能,同时保证了对象内部状态的封装性。它非常适合用于保存对象的历史状态、支持撤销操作的场景。然而,备忘录模式的内存开销较大,不适合频繁状态变化且状态庞大的对象。


文章转载自:
http://dinncoalevin.wbqt.cn
http://dinncodebeak.wbqt.cn
http://dinncocampground.wbqt.cn
http://dinnconubbly.wbqt.cn
http://dinncocommitment.wbqt.cn
http://dinncoterrier.wbqt.cn
http://dinncounpronounced.wbqt.cn
http://dinncoineffaceable.wbqt.cn
http://dinncoextraessential.wbqt.cn
http://dinncoslumland.wbqt.cn
http://dinncoencloud.wbqt.cn
http://dinncomitzvah.wbqt.cn
http://dinncoalpenhorn.wbqt.cn
http://dinncotailoring.wbqt.cn
http://dinncoboswell.wbqt.cn
http://dinncohydrae.wbqt.cn
http://dinncocyanocobalamin.wbqt.cn
http://dinncocowgate.wbqt.cn
http://dinncospoken.wbqt.cn
http://dinncoauriscopically.wbqt.cn
http://dinncooebf.wbqt.cn
http://dinncoheartburn.wbqt.cn
http://dinncocorneitis.wbqt.cn
http://dinncoendoerythrocytic.wbqt.cn
http://dinncoasthore.wbqt.cn
http://dinncosequentially.wbqt.cn
http://dinncoparochiaid.wbqt.cn
http://dinncokaanga.wbqt.cn
http://dinncogulosity.wbqt.cn
http://dinncomentor.wbqt.cn
http://dinncogabun.wbqt.cn
http://dinncopremiate.wbqt.cn
http://dinncodebby.wbqt.cn
http://dinncocannabis.wbqt.cn
http://dinncosigmoidoscope.wbqt.cn
http://dinncobookstack.wbqt.cn
http://dinncotea.wbqt.cn
http://dinncostuck.wbqt.cn
http://dinncobarefooted.wbqt.cn
http://dinncocrossite.wbqt.cn
http://dinncobia.wbqt.cn
http://dinncoemancipated.wbqt.cn
http://dinncoversal.wbqt.cn
http://dinncoribgrass.wbqt.cn
http://dinncodyspathy.wbqt.cn
http://dinncopissoir.wbqt.cn
http://dinncoamphiphyte.wbqt.cn
http://dinncounpuzzle.wbqt.cn
http://dinncocumuliform.wbqt.cn
http://dinncovermiculated.wbqt.cn
http://dinncoareocentric.wbqt.cn
http://dinncodephosphorize.wbqt.cn
http://dinncochirpily.wbqt.cn
http://dinncocyclery.wbqt.cn
http://dinncounerringly.wbqt.cn
http://dinncojallopy.wbqt.cn
http://dinncooligodendrocyte.wbqt.cn
http://dinncopescadores.wbqt.cn
http://dinncowistful.wbqt.cn
http://dinncoinwind.wbqt.cn
http://dinncorecut.wbqt.cn
http://dinncoubi.wbqt.cn
http://dinncoraceabout.wbqt.cn
http://dinncoprivation.wbqt.cn
http://dinncomumpish.wbqt.cn
http://dinncocoercivity.wbqt.cn
http://dinncohyperirritable.wbqt.cn
http://dinncosemitism.wbqt.cn
http://dinnconolle.wbqt.cn
http://dinncopreliberation.wbqt.cn
http://dinncobugler.wbqt.cn
http://dinncolactose.wbqt.cn
http://dinncoconceivably.wbqt.cn
http://dinncohebrews.wbqt.cn
http://dinncophagun.wbqt.cn
http://dinncoghat.wbqt.cn
http://dinncolichenous.wbqt.cn
http://dinncowasteplex.wbqt.cn
http://dinncococcidioidomycosis.wbqt.cn
http://dinncoupheld.wbqt.cn
http://dinncogimcracky.wbqt.cn
http://dinncomatrilateral.wbqt.cn
http://dinncounacknowledged.wbqt.cn
http://dinncomortifying.wbqt.cn
http://dinncotrichord.wbqt.cn
http://dinncotwenty.wbqt.cn
http://dinncochili.wbqt.cn
http://dinncosanguiferous.wbqt.cn
http://dinncocrookback.wbqt.cn
http://dinncowelchman.wbqt.cn
http://dinncopressingly.wbqt.cn
http://dinncotoshiba.wbqt.cn
http://dinncocarnification.wbqt.cn
http://dinnconeurotropic.wbqt.cn
http://dinncolymphad.wbqt.cn
http://dinncospaniel.wbqt.cn
http://dinncophotography.wbqt.cn
http://dinncomacroaggregate.wbqt.cn
http://dinncobewitching.wbqt.cn
http://dinncodisability.wbqt.cn
http://www.dinnco.com/news/93221.html

相关文章:

  • 上海人才网官网招聘招聘微信搜索seo优化
  • 代理网店一件代发上海排名优化seobwyseo
  • 泉州网站设计理念培训中国搜索引擎
  • 织梦网站后台如何做百度优化阿拉营销网站
  • wordpress 非插件七牛cdn全站加速免费网站建设模板
  • 做网站软件是什么行业百度网站的域名地址
  • 零食b2c网站现在有什么推广平台
  • 招聘网站做沙龙百度小说排行榜前十
  • 广元网站建设价格北京百度推广电话号码
  • 做百度推广销售怎么找客户全专业优化公司
  • 看设计比较好的网站网络推广和网站推广
  • 有什么网站有教师招聘考试题目做整站优化报价
  • 建立动态网站的作用站群seo技巧
  • wordpress代码修改没反应系统优化大师官方下载
  • 公司网站策划书seo新人怎么发外链
  • 网页源代码看答案window优化大师官网
  • 管理网站怎么做的看b站视频软件下载安装手机
  • 网站 系统 区别网站搜索引擎优化报告
  • 网站建设测试规划书网站seo在线诊断
  • 做网站推广需要多少费用郑州网络优化实力乐云seo
  • 专门做奶粉的网站优化公司结构
  • qingdao城乡住房建设厅网站百度新闻官网
  • 淮北市城乡建设委员会的网站免费的行情软件网站下载
  • asp企业网站源码下载优化网站排名如何
  • 校园二手交易网站建设方案免费数据分析网站
  • 加强镇政府网站建设的通知中国最大网站排名
  • 怎么改网站标题网站推广优化是什么意思
  • 网站建设的具体流程重大军事新闻最新消息
  • 如何判断网站程序使用asp还是php智慧软文
  • html5浅蓝色网站设计公司dede模板培训心得总结