Factory Method pattern and public constructor
- by H4mm3rHead
Hi,
Im making a factory method that returns new instances of my objects. I would like to prevent anyone using my code from using a public constructor on my objects. Is there any way of doing this?
How is this typically accomplished:
public abstract class CarFactory
{
public abstract ICar CreateSUV();
}
public class MercedesFactory : CarFactory
{
public override ICar CreateSUV()
{
return new Mercedes4WD();
}
}
I then would like to limit/prevent the other developers (including me in a few months) from making an instance of Mercedes4WD. But make them call my factory method. How to?