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.
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
(becausenull
is never/rarely used in Scala, and exceptions are ugly):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.