`
shake863
  • 浏览: 664720 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns in ActionScript-Singleton

阅读更多

In our real world, many things are one and only. For example, there is only one god in our world, and only one president in the USA, eh, I mean the current president. One and only is very important to our world, and so does to our program.

 
     In some case, we may only need one instance of some classes. If you have any experience on developing the GUI application, you can feel it. For example, there’s only one display object in J2ME application, and only one form in windows application. During those situations, we need to control the number of the class, and it was forbidden that using the new operator to generate a new instance. Further more, we need to supply an interface for accessing the one and only instance.

 

That’s all about the singleton pattern. And the intent defined by GOF is as follows.

Ensure a class only has one instance, and provide a global point of access to it.

–By GOF BOOK

Now, we come to the operation layer.

In general, we will meet two problems here, one is when to initialize the instance and the other is how to ensure thread-safe in the multithread environment. Because Action Script 3 doesn’t support multithread, so we don’t need to care the thread-safe.

Before we solve the first problem, we need to provide a global access point for getting the instance. Of course, we don’t except the users use the new operator to get a new instance. Maybe we could change the constructor modifier from public to private. Eh, if you really do so, you’ll get a complier error, because in Action Script 3, the modifier of a constructor can only be public. So, we need to change our method.

If we can’t change the modifier, the constructor can be called outside. One solution is we can throw an error in the constructor, just like below.

  1. public function President ()
  2. {
  3. if ( president != null )
  4. throw   new Error ( You shouldn t use the new operator !” ) ;
  5. }

If there is already have an instance, we need to throw an error. If not, let it go, and then we can get an instance.

This is not a good way, but it works :). Now, let’s come to the first problem. In general, there will be two kinds solution. One is called early initialization, the other is lazy initialization. The first one initializes the instance when the program first runs, and the other initializes the instance when the getInstance () method called.

The code below shows early initialization. The instance will get initialized when the class gets initialized.

  1. static var president : President = new President () ;
  2. public   static function getInstance () : President
  3. {
  4. return   president ;
  5. }

The code below shows lazy initialization. As you see, after the getInstance() method was called, the instance will get initialized.

  1. static var president : President = null ;
  2.  
  3. public   static function getInstance () : President
  4. {
  5. if ( president == null )
  6. president = new   President () ;
  7. return   president ;
  8. }

Which way should be taken depends on your demand. Actually, you’ll need to consider these two situations only when you using this pattern to get a big object in the memory limit environment.

You can download the full source code from download here .

Enjoy!

分享到:
评论

相关推荐

    Go Design Patterns for Real-World Projects epub

    Go Design Patterns for Real-World Projects 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Go Design Patterns for Real-World Projects azw3

    Go Design Patterns for Real-World Projects 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss.chm

    Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss

    Laracasts - design-patterns-in-php

    "Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...

    Design-Patterns-in-Modern-C++

    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...

    Design Patterns in Modern C++--2018

    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 ...

    design-patterns-for-humans设计模式中文翻译版

    https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码

    Design-Patterns-In-Kotlin,在kotlin中实现的设计模式.zip

    《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。

    Go Design Patterns for Real-World Projects.epub

    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+3

    Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3

    Design Patterns in ActionScript

    《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...

    Pro-Objective-C-Design-Patterns-for-iOS

    《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...

    2010-13-Design Patterns for Safety-Critical.pdf

    在本论文“2010-13-Design Patterns for Safety-Critical”中,作者Ashraf Armoush探讨了如何将设计模式的概念应用于安全关键嵌入式系统的开发。他构建了一个设计模式目录,这个目录包含了一系列针对硬件和软件的...

    design-patterns-c-sharp

    "design-patterns-c-sharp"这个主题涵盖了C#编程环境中如何应用这些模式的知识。 首先,我们需要理解设计模式的基本概念。设计模式并不具体到任何特定的代码或库,而是一种描述在特定情况下如何设计软件组件的通用...

    java-design-patterns-master_JAVAdesignpattens_java_design_

    这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...

Global site tag (gtag.js) - Google Analytics