From 1af0027ee7609eac4600da724157eaef9099650e Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 19 Nov 2010 12:57:12 +0300 Subject: [PATCH] Factories --- examples/src/AnonymousObjects.jetl | 2 ++ examples/src/Factories.jetl | 27 ++++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/examples/src/AnonymousObjects.jetl b/examples/src/AnonymousObjects.jetl index 1419ef768c1..0f844cf66df 100644 --- a/examples/src/AnonymousObjects.jetl +++ b/examples/src/AnonymousObjects.jetl @@ -18,4 +18,6 @@ val GOD = object { fun sendMessage(message : GodMEssage) = throw new RuntimeException(message.name) }; + +// Groovy like syntax: too many things to assume addMouseListener([mouseClicked : {e => doFoo()}]); diff --git a/examples/src/Factories.jetl b/examples/src/Factories.jetl index 0ab00bd1b0f..27102c68250 100644 --- a/examples/src/Factories.jetl +++ b/examples/src/Factories.jetl @@ -1,13 +1,26 @@ -class ConcreteClass { +class Foo(bar : Bar) { - this() {} +} static { + private val cache = HashMap() + Foo() { + return Foo(null); + } + + Foo(bar : Bar) { + if (bar in cache.keys) return cache[bar] + val newFoo = this(bar) // calls the primary constructor + cahce[bar] = newFoo + return newFoo + } } -// Implicitly generated a factory for ConcreteClass with a no-argument factory method +// To create an object of Foo +val foo = Foo() +or +val foo = Foo(Bar()) -interface class IFooBarInterface { - new () { } -} +// Subclass (Foo must be marked as extendable/open, i.e. ont sealed) +class FooSub(S : String) : Foo() { // Delegates to Foo created as a result of the factory method Foo() -factory \ No newline at end of file +} \ No newline at end of file