Factory Method pattern and public constructor
Posted
by H4mm3rHead
on Stack Overflow
See other posts from Stack Overflow
or by H4mm3rHead
Published on 2010-05-10T08:24:31Z
Indexed on
2010/05/10
8:34 UTC
Read the original article
Hit count: 305
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?
© Stack Overflow or respective owner