AS3与设计模式(单例模式).docVIP

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  4. 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  5. 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  6. 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  7. 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
【AS3与设计模式】 Singleton Pattern (单例模式) [ 2007-08-31 15:05:00 | 发布: N神 ] 字体大小: 大 | 中 | 小 关于这一系列的索引页和说明请点这里 /blog/article.asp?id=510 想直接下Singleton演示代码的点这里 Singleton Pattern(单例模式)应该是我最早学到的设计模式 ,因为它是最简单也是最常用到的 ,哪里都会遇到它。 先看一下定义: The Singleton Pattern : ensures a class has only one instance, and provides a global point of access to it. 大概意思就是确保某个class只有一个实例,而且提供一个全局的访问点 从as1过来的我们喜欢把什么东西都往_root下或者_global下扔,然后在需要的时候直接访问_root.*** , _global.*** ,在面向对象的世界里这样做好像是不好的行为,也许可以考虑一下把这些东西放到一个singleton里。 测试代码: 首先创建一个叫做designpatterns的项目,主文件为designpatterns.as,以后的测试代码都在这个主文件里写 designpatterns.as : package { ?import flash.display.Sprite; ?import net.nshen.designpatterns.singleton.Singleton; ? ?public class designpatterns extends Sprite ?{ ?public function designpatterns() ?{ ?Test_Singleton() ? ?} ? ?public function Test_Singleton():void{ ? ?var s:Singleton=Singleton.getInstance() ?s.doSomething(); ?/* ?使用构造函数创建实例会报错 ?var s1:Singleton=new Singleton() ?s1.doSomething() ?*/ ?} ?} } Singleton类基本上有这三个特征 1. A private static property that holds the single instance of the class. 2. A public static method that provides access to the single instance if its created and creates the single instance if it hasnt been created yet. 3. A way of restricting access to instantiating the class. This is usually achieved by making the constructor private. However, ActionScript 3.0 doesnt have private constructors. Later well examine an alternative way of restricting instantiation in ActionScript 3.0. 大概的意思就是 1 . 有一个 private static 属性引用类的唯一的实例 2 . 有一个 public static 方法来访问这个实例(如果实例已经被创建),或创建实例(如果实例还没被创建) 3 . 阻止用构造函数实例化这个类,这个一般使用私有的构造函数来实现,但由于AS3不支持private的构造函数,我们只能用其他的方法 Singleton类有很多种写法但都差不多,我们使用《 Advanced ActionScript 3 with Design Patterns 》这本书里的写法,稍加改动 Singleton 示意代码: package net.nshen.designpatterns.singleton { ?public class Singleton ?{ ?private static var _instance:Singleton; ?public function Singleton(singletonEnforcer:SingletonEnforcer) { ?if(singletonEnforcer==null)throw new Error(singleton

文档评论(0)

j43h9nh7 + 关注
实名认证
文档贡献者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档