Example:
public class person
{
public String Name {get;set;}
}
public static class FactoryStatic
{
public static person Create(string name)
{
return new person() {Name =name};
}
}
public class FactoryNoneStatic
{
public static person Create(string name)
{
return new person() {Name =name};
}
}
My question is what is difference between those two factory class, and when to use them?
For the direct use, the creation of a Person object, there is no difference.
It is however possible to create an instance of
FactoryNoneStatic, which is probably not desirable.By marking the class as
staticyou are clear about your intent and you prevent mis-use of the class.So in this situation, use the
static class FactoryStatic.