开发者_C百科Possible Duplicate:
What is the need of private constructor in C#?
Hi,
I've seen plenty of classes in .NET which have private constructor (Stream is one of them I think). When would I want to model a class like this?
I keep thinking that if my class has no internal state/fields, then I can make it have a private constructor.
Am I on the right track with this idea? I can understand the use of a factory (I've run into the tipping point a few times), but not with a private constructor class.
Thanks
Private constructors are normally used for:
- Constructor-Chaining - As targets for other constructors in the same class.
- Controlling construction - Occasionally, all the constructors of a class are made private to:
- Enforce the singleton pattern.
- Only allow construction through static factory-methods.
- Only allow nested classes (if any) to inherit from it.
I've seen plenty of classes in .NET which have private constructor (Stream is one of them I think)
System.IO.Stream
is not an example of any of these - it has a single, protected constructor (at least as of .NET 4.0) . Since it is an abstract class, it does not make sense for it to have public constructors.
I keep thinking that if my class has no internal state/fields, then I can make it have a private constructor.
In this case, consider creating a static class instead, providing no instance constructors at all (a static class can't have instance constructors since instances of it cannot be created). Of course, there are cases when this may not be appropriate despite the lack of any state, such as when the class must implement an interface.
Make the constructor private if you don't want to allow external code to create an instance of your class. This is often used when implementing the Singleton pattern.
Actually, a private constructor is relatively rare. The constructor of the Stream class is protected, not private. That way only derived classes can call it.
The main reason for a private constructor is when implementing the Singleton design pattern. However there are other instances where limited the visibility of the ctor is useful. Sometimes I want to implement dependency injection (constructor injection) but only allow my own test classes to inject a dependency. In this case I'll make the constructor with the injection parameter internal
(not private) and allow my unit test project access through the AssemblyInfo.cs file (see here).
Additionally, sometimes you want to use a private constructor on an abstract base class so that you can implement a factory method to return particular concrete implementations that are not publicly exposed. Think something like this
public abstract class Base {
private Base() { }
public static Base CreateClass() {
if (someCondition()) return new Concrete1();
else return new Concrete2();
}
private class Concrete1 : Base { }
private class Concrete2 : Base { }
}
The enumerators in .NET are good examples of this pattern.
I think you can make the constructor private to hide it. That way you cannot call the constructor and you cannot instantiate the class. This can be useful for classes that don't need to be instantiated (helpers with only static methods) or base classes that need to be inherited from, although you would preferably make those abstract.
精彩评论