Hi all,
I am having a little bit a class design issue. I will outline the three patterns I am considering.
Example 1:
Class MyObject
{
public MyObject(IWrapper wrapper,string name)
{//set properties.}
public Void Declare()
{
//Some COM stuff with IWrapper
}
}
Use:
MyObject testobj = new MyObject(fakewrapper,"test");
testobj.Declare();
Example 2:
Class MyObject
{
public MyObject(IWrapper wrapper,string name)
{ //set properties.}
public MyObject Declare()
{
//Some COM stuff with IWrapper
return this;
}
}
Use:
MyObject testobj = new MyObject(fakewrapper,"Test");
testobj = testobj.Declare();
Example 3: Using private constructor
Class MyObject
{
private MyObject(IWrapper wrapper,string name)
{//set properties.}
public static MyObject Declare(IWrapper wrapper,string name)
{
//Some COM stuff with IWrapper
return new MyObject(wrapper,name);
}
}
Use:
MyObject testobj = MyObject.Declare(fakewrapper,"Test");
My main questions are;
Which one do you think is the better design?
Is it bad practice to hide the constructor in favor of a static factory method?
I am also trying to make this as testable as possible. The only reason I can't seem to make up my mind is because the MyObject class is a wrapper around another object in a COM application.
The problem is that this object has to be declared in the COM application which is what the declare method is doing before any other method can be called, but if I hide the constructor and then add more work to the Declare method I think it will make the rest of the class hard to test, becuase I have to go through the Declare method first.
Cheers.