[isabelle-dev] ADTs in Scala

Holger Gast gast at informatik.uni-tuebingen.de
Sun Apr 15 20:46:08 CEST 2012


On 04/14/2012 03:09 PM, Makarius wrote:
> On Sat, 14 Apr 2012, Lars Hupel wrote:
> 
>>> The main thing is the "private" in this odd position, which is Odersky's
>>> way to make the constructor private, but the keep the type itself
>>> public. This detail seems to have been forgotten in Java -- in fact in a
>>> recent talk some Java guru admitted that -- which was also my starting
>>> point to google for "private constructor" to find the above solution.
>>
>> In fact, the pattern you used is quite common amongst Java developers,
>> they just call it "Factory Pattern".
> 
> I've heard of this recent addition to the OO vocabulary to fix some early
> conceptual problems of the approach.  That is "object Graph" part only.
Just for a reference: factories have been discussed in the seminal
book on patterns, Gamma et al. "Design Patterns: Elements of Reusable
Object-Oriented Software", in 1995. (There are two variants here: abstract factories
and factory methods, but this is only a detail.)

> How would a Java person solve the private constructor problem?
One would simply define the public default constructor
(or more precisely: zero-argument constructor) to provide a fully initialized
empty graph by setting the fields appropriately. This approach does not
destroy the properties of abstract data types, since the invariants
remain under the class's control and clients cannot break the abstraction.
In effect, the solution merely reflects the (sensible) convention that the
"default" content of data structures is empty. The Java standard
library uses this throughout: writing new HashMap() yields an empty
HashMap, just as expected.

As Lars has pointed out, it is a common coding pattern to complement
constructors by static construction methods to allow the method
names to designate clearly the expected initial content. It is
usually considered a matter of style, and the number of
constructors and their arguments, which formulation is preferable.

Actually, even with such factory methods, it is usually left to the
constructors to do the proper initialization according to the
desired invariants etc. The static methods merely pass on parameters
and thus select the right constructor.

I hope this somewhat historical review helps in the discussion of
coding idioms & practices,

Best wishes,

 Holger



More information about the isabelle-dev mailing list