I\'m looking for a simple example of how to implement a factory class, but without the use of a Switch or an If-Then statement. All the examples I can find use one. F
public class PositionFactory
{
private Dictionary _positions;
public PositionFactory()
{
_positions = new Dictionary();
}
public void RegisterPosition(int id) where PositionType : Position
{
_positions.Add(id, typeof(PositionType));
}
public Position Get(int id)
{
return (Position) Activator.CreateInstance(_positions[id]);
}
}
Used like this:
var factory = new PositionFactory();
factory.RegisterPosition(0);
factory.RegisterPosition(1);
Position p = factory.Get(0); //Returns a new Manager instance