What is the use of making constructor private in a

2019-01-02 19:29发布

Why should we make the constructor private in class? As we always need the constructor to be public.

标签: oop
23条回答
怪性笑人.
2楼-- · 2019-01-02 20:17

It's really one obvious reason: you want to build an object, but it's not practical to do it (in term of interface) within the constructor.

The Factory example is quite obvious, let me demonstrate the Named Constructor idiom.

Say I have a class Complex which can represent a complex number.

class Complex { public: Complex(double,double); .... };

The question is: does the constructor expects the real and imaginary parts, or does it expects the norm and angle (polar coordinates) ?

I can change the interface to make it easier:

class Complex
{
public:
  static Complex Regular(double, double = 0.0f);
  static Complex Polar(double, double = 0.0f);
private:
  Complex(double, double);
}; // class Complex

This is called the Named Constructor idiom: the class can only be built from scratch by explicitly stating which constructor we wish to use.

It's a special case of many construction methods. The Design Patterns provide a good number of ways to build object: Builder, Factory, Abstract Factory, ... and a private constructor will ensure that the user is properly constrained.

查看更多
时光乱了年华
3楼-- · 2019-01-02 20:18

Quoting from Effective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

(EDIT: As per the comment this is something which might be applicable only with Java, I'm unaware if this construct is applicable/needed in other OO languages (say C++))

An example as below:

public class Constants {
    private Contants():

    public static final int ADDRESS_UNIT = 32;
    ...
}

EDIT_1: Again, below explanation is applicable in Java : (and referring from the book, Effective Java)

An instantiation of utility class like the one below ,though not harmful, doesn't serve any purpose since they are not designed to be instantiated.

For example, say there is no private Constructor for class Constants. A code chunk like below is valid but doesn't better convey intention of the user of Constants class

unit = (this.length)/new Constants().ADDRESS_UNIT;

in contrast with code like

unit = (this.length)/Constants.ADDRESS_UNIT;

Also I think a private constructor conveys the intention of the designer of the Constants (say) class better.

Java provides a default parameterless public constructor if no constructor is provided, and if your intention is to prevent instantiation then a private constructor is needed.

One cannot mark a top level class static and even a final class can be instantiated.

查看更多
余生无你
4楼-- · 2019-01-02 20:18

Utility classes could have private constructors. Users of the classes should not be able to instantiate these classes:

public final class UtilityClass {
    private UtilityClass() {}

    public static utilityMethod1() {
        ...
    }
}
查看更多
听够珍惜
5楼-- · 2019-01-02 20:19

This can be very useful for a constructor that contains common code; private constructors can be called by other constructors, using the 'this(...);' notation. By making the common initialization code in a private (or protected) constructor, you are also making explicitly clear that it is called only during construction, which is not so if it were simply a method:

public class Point {
   public Point() {
     this(0,0); // call common constructor
   }
   private Point(int x,int y) {
     m_x = x; m_y = y;
   }
};
查看更多
与君花间醉酒
6楼-- · 2019-01-02 20:19

If it's private, then you can't call it ==> you can't instantiate the class. Useful in some cases, like a singleton.

There's a discussion and some more examples here.

查看更多
几人难应
7楼-- · 2019-01-02 20:20

You may want to prevent a class to be instantiated freely. See the singleton design pattern as an example. In order to guarantee the uniqueness, you can't let anyone create an instance of it :-)

查看更多
登录 后发表回答