- 浏览: 664726 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
Now, I’m using Microsoft word to write these articles. These word processor programs are very useful when you writing something down. Sometimes, we heard someone was very familiar with these programs, such as word, can remember many short-cuts. Though I use this program frequently, I can’t remember many short-cuts. I can remember some short-cuts, such as “Ctrl+Z”, which means UNDO.
The UNDO command is widely use in today’s program. You can see it almost everywhere. Besides the word processor program, you can find it in photoshop, in flash, or even in games. Eh, in games, this command isn’t called UNDO, it use another name SAVE.
I think you should be familiar with this command. But, have you ever implemented this command in your application? Maybe, your application doesn’t need this kind of command, but you won’t deny the importance of this command. So, it’s worth to consider how to implement this command.
In the GoF book, there is a pattern, which has much to do with this command, called Memento. The intent is as follows.
Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
–By GOF BOOK
In my opinion, without practice, the definition is nothing. So, let’s write some code to implement this pattern.
This little program will mimic the game command SAVA. Generally, we need to store some basic information about the role, such as the blood, the experience and so on. Do it the easy way, we will only store the blood and the experience, nothing more.
Our demo will be looks like follows.
This is a little game with no rules, just fight And you can save the characters state, including blood and experience, with the save button. Of course, you can load the states you’ve just saved. One more thing, the experience here can’t help you level up; you can take a look at the source code for its effect.
The class diagram is as follows.
The SaveManager class is used for manage the save state, and all the state of Character will be put into the CharacterState. When we want to save the character’s state, we just need to call the SaveManager.save, and pass the character’s state into it. Then, when we want to load it, call the load method of SaveManager.
This is a basic application of this pattern. If you want to implement the UNDO and REDO commands, you may need a memento stack. How to use the stack depends on you program.
Enjoy!
发表评论
-
设计模式 图析
2011-09-24 14:27 734【observer】 【adapter】 【comm ... -
一个设计模式的图片
2009-05-03 02:14 539... -
Design Patterns in ActionScript–Factory Method
2009-02-24 13:37 768In our last topic, we talk abou ... -
Design Patterns in ActionScript-Strategy
2009-02-24 13:34 572Today, we’re going to talk abou ... -
Design Patterns in ActionScript-Mediator
2009-02-24 13:29 507Ok, the last pattern now. Let’s ... -
Design Patterns in ActionScript-Command
2009-02-24 13:21 619A few months ago, I was an inte ... -
Design Patterns in ActionScript-Chain of Responsib
2009-02-24 13:12 600When you need some help in a ho ... -
Design Patterns in ActionScript-Flyweight
2009-02-24 13:07 638In Action Script 3.0 we have th ... -
Design Patterns in ActionScript-Visitor
2009-02-24 11:26 511It’s our winter holiday now, an ... -
Design Patterns in Action Script-Composite
2009-02-23 14:29 515Still remember the Interpreter ... -
Design Patterns in ActionScript-Interpreter
2009-02-23 14:26 664In web programming, we often us ... -
Design Patterns in ActionScript-Proxy
2009-02-23 14:19 630Have you ever use HTTP-proxy or ... -
Design Patterns in Action Script-State
2009-02-23 14:17 958Yesterday, when I was on my way ... -
Design Patterns in ActionScript-Builder
2009-02-23 14:16 520Have you ever buy a computer on ... -
Design Patterns in ActionScript-Prototype
2009-02-23 14:12 666When I want to write the Protot ... -
Design Patterns in ActionScript-Iterator
2009-02-23 14:07 575There is a famous saying in com ... -
Design Patterns in Action Script-Template Method
2009-02-23 14:04 497Do you like playing cards? If y ... -
Design Patterns in ActionScript-Observer
2009-02-23 14:00 592In GUI programming, event-drive ... -
Design Patterns in ActionScript-Singleton
2009-02-23 13:55 527In our real world, many things ... -
Design Patterns in Action Script-Bridge
2009-02-23 13:44 556I think many of you are the fan ...
相关推荐
Go Design Patterns for Real-World Projects 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Go Design Patterns for Real-World Projects 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss
"Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...
《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。
Go: Design Patterns for Real-World Projects What You Will Learn: Install and configure the Go development environment to quickly get started with your first program Use the basic elements of the ...
Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3
《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...
《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...
在本论文“2010-13-Design Patterns for Safety-Critical”中,作者Ashraf Armoush探讨了如何将设计模式的概念应用于安全关键嵌入式系统的开发。他构建了一个设计模式目录,这个目录包含了一系列针对硬件和软件的...
design pattern in C# language