Let's say I create a sports game, and in this game there are various positions that the player can play, attack, defend, etc. Therefore, I start by creating a base class:
public abstract class Position { public abstract string Name { get; } }
and subclasses ...
public class Defender : Position { public override string Name { get { return "Defender"; } } }
etc. Everything is good.
But now I need a function to create these objects. I need a create by position function. Thus, one of the possible solutions is to create an enumeration of all positions and transfer this value to a function that includes the enumeration and returns the corresponding object. But this is alarming from my code smell. This soul binds classes, listing, and switch inside a function:
public static Position GetByType(Types position) { switch(position) { case Types.Defender: return new Defender(); ... and further terrible code
What solution should I look for? What design pattern is this?
source share