Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy initialization
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Haxe === This example is in [[Haxe]].<ref>{{Cite news|url=https://code.haxe.org/category/design-patterns/lazy-initialization.html|title=Lazy initialization - Design patterns - Haxe programming language cookbook|date=2018-01-11|access-date=2018-11-09|language=en}}</ref> <syntaxhighlight lang="haxe"> class Fruit { private static var _instances = new Map<String, Fruit>(); public var name(default, null):String; public function new(name:String) { this.name = name; } public static function getFruitByName(name:String):Fruit { if (!_instances.exists(name)) { _instances.set(name, new Fruit(name)); } return _instances.get(name); } public static function printAllTypes() { trace([for(key in _instances.keys()) key]); } } </syntaxhighlight>Usage<syntaxhighlight lang="haxe"> class Test { public static function main () { var banana = Fruit.getFruitByName("Banana"); var apple = Fruit.getFruitByName("Apple"); var banana2 = Fruit.getFruitByName("Banana"); trace(banana == banana2); // true. same banana Fruit.printAllTypes(); // ["Banana","Apple"] } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)