问题
Suppose that I have the following class defined in Scala:
class ClassA(val n: Int) {
...
}
I want to limit this class instances to only those that have n between 5 to 10 using Factory Pattern. For example, In case I write something like:
val a = new ClassA(11)
This raises an exception with an appropriate error message, or at least it returns null or something. How can I achieve this behaviour?
Update:
It is possible to achieve this in java with Factory pattern.
Update2:
This questions seems to be answered here, notably with a verbose title though. I tweak the title and content to save the question being deleted, because of two reasons: 1) The example in this one is concise, 2) the provided answer by @Chris Martin explains briefly the way Factory pattern can be reached in Scala by Using Companion Objects.
回答1:
The conventional way to write a factory in Scala is to define an apply
method on the companion object.
Here's an example using Either
(because null
is never/rarely used in Scala, and exceptions are ugly):
class A private (n: Int) {
override def toString = s"A($n)"
}
object A {
def apply(n: Int): Either[String, A] =
if (n < 5) Left("Too small")
else if (n > 10) Left("Too large")
else Right(new A(n))
}
A(4) // Left(Too small)
A(5) // Right(A(5))
A(11) // Left(Too large)
This is the essentially the same as the Java example you referenced. The A
constructor is private, so the class can only be instantiated via the factory method.
来源:https://stackoverflow.com/questions/30836638/preventing-a-class-instantiation-in-scala-using-factory-pattern