//////////////////////////////////////////////////////////////////////////////// // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to You under the Apache License, Version 2.0 // (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // //////////////////////////////////////////////////////////////////////////////// package mx.core { /** * A deferred instance factory that creates and returns an instance * of a specified class. * An application can use the getInstance() method to * create an instance of the class when it is first needed and get * a reference to the instance thereafter. * * @see DeferredInstanceFromFunction * * @langversion 3.0 * @playerversion Flash 9 * @playerversion AIR 1.1 * @productversion Flex 3 */ public class DeferredInstanceFromClass implements ITransientDeferredInstance { include "../core/Version.as"; //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * Constructor. * * @param generator The class whose instance the getInstance() * method creates and returns. * * @langversion 3.0 * @playerversion Flash 9 * @playerversion AIR 1.1 * @productversion Flex 3 */ public function DeferredInstanceFromClass(generator:Class) { super(); this.generator = generator; } //-------------------------------------------------------------------------- // // Variables // //-------------------------------------------------------------------------- /** * @private * The generator class. */ private var generator:Class; /** * @private * The generated value. */ private var instance:Object = null; //-------------------------------------------------------------------------- // // Methods // //-------------------------------------------------------------------------- /** * Creates and returns an instance of the class specified in the * DeferredInstanceFromClass constructor, if it does not yet exist; * otherwise, returns the already-created class instance. * * @return An instance of the class specified in the * DeferredInstanceFromClass constructor. * * @langversion 3.0 * @playerversion Flash 9 * @playerversion AIR 1.1 * @productversion Flex 3 */ public function getInstance():Object { if (!instance) instance = new generator(); return instance; } /** * Resets the state of our factory to the initial, uninitialized state. * The reference to our cached instance is cleared. * * @langversion 3.0 * @playerversion Flash 9 * @playerversion AIR 1.1 * @productversion Flex 4 */ public function reset():void { instance = null; } } }