There are my shapes (for example):
public abstract class Shape
{
protected int _id;
protected string _description;
public abstract string ToXml();
}
public sealed class Triangle : Shape
{
public int TriangleId { get { return _id; } set { _id = value; } }
public string TriangleDescription { get { return _description; } set { _description = value; } }
public ConsoleColor color { get; set; }
public override string ToXml()
{
// First implementation
throw new NotImplementedException();
}
}
public sealed class Circle : Shape
{
public int CircleId { get { return _id; } set { _id = value; } }
public string CircleDescription { get { return _description; } set { _description = value; } }
public int Radius { get; set; }
public override string ToXml()
{
// Second implementation
throw new NotImplementedException();
}
}
public sealed class Square : Shape
{
public int SquareId { get { return _id; } set { _id = value; } }
public string SquareDescription { get { return _description; } set { _description = value; } }
public int Edge { get; set; }
public override string ToXml()
{
// Third implementation
throw new NotImplementedException();
}
}
Client have to choose one kind of shape to get xml-view:
public enum ShapeType
{
Triangle,
Circle,
Square
}
public class Module
{
public string GetXml( ShapeType type, int id )
{
switch( type )
{
case ShapeType.Circle:
return DB.GetEntity( id ).ToTypedEntity<Circle>().ToXml();
case ShapeType.Square:
return DB.GetEntity( id ).ToTypedEntity<Square>().ToXml();
case ShapeType.Triangle:
return DB.GetEntity( id ).ToTypedEntity<Triangle>().ToXml();
}
}
}
As you can see, there are too much the same code parts that are matching enumerations with shape's types. It is not good.
How can I avoid it?