When I want to write the Prototype pattern, I firstly look up the
ActionScript 3.0 manual. I want to find out whether there is a clone
method in Object class. If so, I will use this as an example. But,
unfortunately, I can’t find this method in the Object class. Then I
found the prototype attribute, but the explanation confused me. So, I
decide to show you this pattern in my own way without using the Object
class.
Firstly, you need to know the intent of this pattern. You can read the following text.
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
–By GOF BOOK
This pattern is about how to instantiate a new object. You may
say, we could use the new operator to instantiate the object. Of
course, you can do it in this way. But there are still some another
things you need to considering, such as the new object can’t get any
information from the current object. It just likes in some fantasy
movies, the magician copy himself, and two or more magicians looks the
same appears in the screen.
When the object has much the same with the existing object, you
need to consider this pattern. By using this pattern, you can get a
copy of the existing object.
Do it the simplest way, we provide an interface named Cloneable, and the concrete class will implement this interface.
- public
interface
Cloneable
- {
- function
clone
()
:
Object
- }
And the concrete class named Magician. It implements the clone method, and returns and new magician with the current state.
The class diagram is as follows.
Now, you can use Magician.clone() to get a new instance. And the
new instance will be the same as the existing one. If you want the
classes be the same, this is a good way for you.
When you want to apply this pattern, I should tell you some
thing more about this pattern. Have you ever heard deep copy and
shallow copy? If you have ever heard that, I think you’re already what
I will say next. If not, you’d better to get some information about
those things.
If you use the shallow copy, the reference field of new object
and old object will points to the same object. So, when you change the
object via the old object. The new object will also be affected. So, be
careful about the shallow copy.
Download Full Project
Enjoy!
分享到:
相关推荐
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...
Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as ...
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。
《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...
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
《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...
在本论文“2010-13-Design Patterns for Safety-Critical”中,作者Ashraf Armoush探讨了如何将设计模式的概念应用于安全关键嵌入式系统的开发。他构建了一个设计模式目录,这个目录包含了一系列针对硬件和软件的...
这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...