导读:
You have to register classes to Singleton before you use it. About Mixin metadata, See The [Transient] and [Mixin] metadata tags.
org.pepe.managers.HogeManager (AS3)
You have to add compiler option like below.
"-load-config+=config/custom-config.xml"
SingletonSampleForFlex2
source of SingletonSampleForFlex2
SingletonSampleForFlex3
source of SingletonSampleForFlex3
BTW, on Japanese environment, css definition of source view is output in Japanese. Therefore, source view doesn't work correctly.
本文转自
http://shigeru-nakagaki.com/index.cfm/2007/12/19/20071220-How-to-use-mx-core-Singleton
mx.core.Singleton is internal class in FlexFramework. I don't know Adobe supports mx.core.Singleton officially or not. Also FlexFrameworks's Singleton isn't like Java's because AS3 doesn't support Singleton. Also you don't need to use Singleton. I just show a way to use mx.core.Singleton.
singleton是FLEX框架内嵌的类,我不知道ADOBE是否官方的支持这个类,虽然此singleton和java的不一样,因为AS3不支持,
也许你不会用到这个类,但是没关系我只是告诉你如何用这个类
SingletonSampleForFlex2.mxml
SingletonSampleForFlex2.mxml
- version="1.0" encoding="utf-8"?>
-
- layout="vertical"
- xmlns:mx="http://www.adobe.com/2006/mxml"
- viewSourceURL="srcview/index.html">
- import mx.core.Singleton;
- import org.pepe.managers.IFooManager;
- import org.pepe.managers.IHogeManager;
- private function helloHandler():void
- {
- var cName:String = "org.pepe.managers::HogeManager";
- // This doesn't need.
- // But you can check which Class is registered or not
- var c:Class = Singleton.getClass(cName);
- // get instance
- var hoge:IHogeManager =
- Singleton.getInstance(cName) as IHogeManager;
- hoge.sayHello();
- }
- private function goodByeHandler():void
- {
- var cName:String = "org.pepe.managers::FooManager";
- // This doesn't need.
- // But you can check which Class is registered or not
- var c:Class = Singleton.getClass(cName);
- // get instance
- var foo:IFooManager =
- Singleton.getInstance(cName) as IFooManager;
- foo.sayGoodBye();
- }
- ]]>
- text="Singleton Sample for Flex 2.0.1"
- fontSize="26" fontWeight="bold" />
- height="20" />
- >
- label="Hello" click="helloHandler()" />
- label="GoodBye" click="goodByeHandler()" />
- >
- >
You have to register classes to Singleton before you use it. About Mixin metadata, See The [Transient] and [Mixin] metadata tags.
在使用之前,你必须将你的类注册到singleton至于MIXIN元标签,可以参看相应的链接
org.pepe.managers.AppBootstrap (AS3)
org.pepe.managers.AppBootstrap (AS3)
- package org.pepe.managers
- {
- import flash.utils.getDefinitionByName;
- import mx.core.Singleton;
- import mx.managers.ISystemManager;
- import mx.utils.ObjectUtil;
- [Mixin]
- public class AppBootstrap
- {
- // SystemManager call this function
- // before Application initialize
- public static function init(sm:ISystemManager):void
- {
- // set a break point here and try to debug:)
- trace(ObjectUtil.toString(sm.info()));
- // register Class info to Singleton class
- Singleton.registerClass(
- "org.pepe.managers::HogeManager",
- Class(getDefinitionByName("org.pepe.managers::HogeManager"))
- );
- Singleton.registerClass(
- "org.pepe.managers::FooManager",
- Class(getDefinitionByName("org.pepe.managers::FooManager"))
- );
- }
- private var _hoge:HogeManager; // for compiler
- private var _foo:FooManager; // for compiler
- public function AppBootstrap()
- {
- super();
- }
- }
- }
org.pepe.managers.HogeManager (AS3)
- package org.pepe.managers
- {
- import flash.events.EventDispatcher;
- import flash.events.IEventDispatcher;
- import mx.controls.Alert;
- public class FooManager extends EventDispatcher implements IFooManager
- {
- private static var _instance:FooManager;
- // Singleton call this function
- public static function getInstance():FooManager
- {
- if(!_instance){
- _instance = new FooManager();
- }
- return _instance;
- }
- public function FooManager(target:IEventDispatcher=null)
- {
- super(target);
- }
- public function sayGoodBye():void
- {
- Alert.show("GoodBye", "FooManager");
- }
- }
- }
You have to add compiler option like below.
"-load-config+=config/custom-config.xml"
你必须加上上面的这行编译器选项
custom-config.xml
custom-config.xml
SingletonSampleForFlex2
source of SingletonSampleForFlex2
SingletonSampleForFlex3
source of SingletonSampleForFlex3
BTW, on Japanese environment, css definition of source view is output in Japanese. Therefore, source view doesn't work correctly.
本文转自
http://shigeru-nakagaki.com/index.cfm/2007/12/19/20071220-How-to-use-mx-core-Singleton
本文介绍如何在Flex框架中使用mx.core.Singleton类实现单例模式。通过示例展示了如何注册和获取单例对象,并提供了完整的源代码。
255

被折叠的 条评论
为什么被折叠?



