Loading Initial Data
Sometimes some of the objects we create have a lot of code in the constructor. Being that constructors aren't inherited by a derived class, it may be nice to have this code in a method and run at construction time. By removing this code out to a method, and changing the approach to the Factory/Factory Method pattern, this would be possible. Say for the example object:
public class MyClass
{
public MyClass()
{
//Has a lot of code
}
}
To change this, the logic above can be changed to this:
public class MyClass
{
private MyClass() { }
private void InitializeObject()
{
//Put code from constructor here
}
}
To construct this object, the factory method approach could be as below:
public static MyClass CreateMyClassObject()
{
MyClass cls = new MyClass();
cls.InitializeObject();
return cls;
}
So, the construction logic can then be inherited, could be overridden, and all happen at construction time.