I know questions with this text has been asked and already answered, but my question is mainly limited to only one method of this flow and where it should be.
I have an assembly for Shape's area calculation (ShapeAreaCalculator
) and this functionality is exposed to client program:
public enum ShapeType
{
SquareShape,
CircleShape,
TriangleShape,
}
public interface IShape
{
void CalculateArea();
void ShowArea();
}
public class Square : IShape
{
public void ShowArea()
{
Console.WriteLine("Logic For ShowArea of Square");
}
public void CalculateArea()
{
Console.WriteLine("Logic For CalcualteArea of Square");
}
}
public class Circle : IShape
{
public void ShowArea()
{
Console.WriteLine("Logic For ShowArea of Circle");
}
public void CalculateArea()
{
Console.WriteLine("Logic For CalculateArea of Circle");
}
}
public class Triangle : IShape
{
public void ShowArea()
{
Console.WriteLine("Logic For ShowArea of Triangle");
}
public void CalculateArea()
{
Console.WriteLine("Logic For CalculateArea of Triangle");
}
}
Factory class:
public interface IShapeFactory
{
IShape GetShape(ShapeType shapeType);
}
public class ShapeFactory : IShapeFactory
{
public IShape GetShape(ShapeType shapeType)
{
switch(shapeType)
{
case ShapeType.SquareShape:
return new Square();
case ShapeType.CircleShape:
return new Circle();
case ShapeType.TriangleShape:
return new Triangle();
default :
throw new ArgumentException();
}
}
}
And I'm using this at client side like this:
static void Main(string[] args)
{
ShapeType selectedShape;
while (true)
{
Console.WriteLine("Enter your choise");
foreach (ShapeType shapeType in Enum.GetValues(typeof(ShapeType)))
{
Console.WriteLine((int)shapeType + " For " + Enum.GetName(shapeType.GetType(), shapeType));
}
int selection;
if (int.TryParse(Console.ReadLine(), out selection) && Enum.IsDefined(typeof(ShapeType), selection))
{
selectedShape = (ShapeType)selection;
break;
}
}
CalculateAndShowAreaOfShape(selectedShape);
}
static void CalculateAndShowAreaOfShape(ShapeType selectedShape)
{
IShapeFactory shapeFactory = new ShapeFactory();
IShape currentShape = shapeFactory.GetShape(selectedShape);
currentShape.CalculateArea();
currentShape.ShowArea();
}
But now I am concerned about this method CalculateAndShowAreaOfShape()
. If in the future some functionality added in the procedure of area calculation, client code will be needed to be modified. It is unnecessary coupling.
Should it be implemented at client end or somewhere in ShapeAreaCalculator
assembly?
Also, is it good practice to ask for user input from assembly? (I think it will limit this assembly to be used in console application only.)