Sujets sur : comment marche un singleton en as3
Cours ActionScript 3 ‘comment marche un singleton en as3’
Comment créer un Singleton en AS3 ?
Dans ce tutoriel, nous allons implémenter le design pattern (ou modèle de conception) Singleton en ActionScript 3.
Et voici le résultat obtenu dans un swf :
1 | http://e6msyji6epr.exactdn.com/wp-content/uploads/2011/03/singleton-ex.swf |
Téléchargez le code source complet de l’exemple :
Télécharger “Implémentation Design Pattern Singleton” singleton-ex.zip – Téléchargé 699 fois – 25,18 KoEt vous, comment implémentez-vous le pattern Singleton en AS3 ?
Il y a, bien évidemment, plusieurs solution pour créer une classe Singleton en ActionScript 3.
Postez votre code AS3 dans les commentaires, je suis curieux de voir votre code.
[codesyntax lang= »actionscript3″ title= »Pattern Singleton » bookmarkname= »Pattern Singleton »]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package { import nl.demonsters.debugger.MonsterDebugger; /** * Classe générique de configuration d'une application * */ public class Config { private static var _oInstance : Config = null;// contient l'instance unique de la classe private static var allowInstance:Boolean = false; // empêche l'instanciation de la classe à partir du constructeur /* paramètres de notre classe de Configuration */ public var texte:String; /** * Returns singleton instance of <code>Config</code> class. * * @return The singleton instance of <code>Config</code> class. */ public static function getInstance() : Config { // si l'instance n'existe pas, on la crée if ( !(Config._oInstance is Config) ) { allowInstance = true;// autorise la création d'une instance Config._oInstance = new Config(); allowInstance = false;// désactive la création d'autre instance } // renvoie toujours la même instance de la classe return Config._oInstance; } /** * Constructeur */ public function Config() : void { if (!allowInstance)// test s'il est possible d'instancier la classe { MonsterDebugger.trace(this, 'This class cannot be instantiated from the constructor' ); // throw new Error ('This class cannot be instantiated from the constructor'); } else // initialisation des paramètres de l'objet Config texte = "Bienvenue sur ActionScript-Facile"; } } } |
[/codesyntax]