Forked from matthandlersux/unclean creator in companion.scala
Created
July 6, 2012 03:49
-
-
Save jarhart/3057950 to your computer and use it in GitHub Desktop.
any way to make this cleaner?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class SuperClass { | |
var initialized = false | |
def initialize { | |
initialized = true | |
} | |
} | |
abstract trait MetaClass[A <: SuperClass] { self: A => | |
def apply(): A = { | |
val obj = newInstance | |
newInstance.initialize | |
obj | |
} | |
protected def newInstance: A | |
} | |
object Subclass1 extends SuperClass with MetaClass[Subclass1] { | |
def newInstance = new Subclass1() | |
} | |
class Subclass1 extends SuperClass { | |
} | |
object Subclass2 extends SuperClass with MetaClass[Subclass2] { | |
def newInstance = new Subclass2() | |
} | |
class Subclass2 extends SuperClass { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment