What is the "Named Constructor Idiom"?

chinmay.sahoo

New member
A technique that provides more intuitive and/or safer construction operations for users of your
class.

The problem is that constructors always have the same name as the class. Therefore the only way
to differentiate between the various constructors of a class is by the parameter list. But if there
are lots of constructors, the differences between them become somewhat subtle and error prone.

With the Named Constructor Idiom, you declare all the class's constructors in the private or
protected sections, and you provide public static methods that return an object. These static
methods are the so-called "Named Constructors." In general there is one such static method for
each different way to construct an object.
 
Back
Top