What I usually do in this case is to pass constructor parameters in specific classes. Therefore, I would change your specific shapes to something like:
public class Circle : Shape { public int Radius { get; set; } public Circle(int radius) { this.Radius = radius; } public override double Area() { return 3.14 * this.Radius * this.Radius; } } public class Rectangle : Shape { public int Length { get; set; } public int Breadth { get; set; } public Rectangle(int lenght, int breadth) { this.Length = lenght; this.Breadth = breadth; } public override double Area() { return Length * Breadth; } }
etc.
Now I would use the factory method, so your fabric would look like this:
public abstract class ShapeFactory { abstract Create(); } public class CircleFactory : ShapeFactory { private int radius; public CircleFactory(int radius){ this.radius = radius; } protected override Shape Create() { return new Circle(this.radius); } } public class RectangleFactory : ShapeFactory { private int length; private int breadth; public RectangleFactory(int length, int breadth){ this.lenght = length; this.breadth = breadth; } protected override Shape Create() { return new Rectangle(this.length, this.breadth); } }
Note that now the factory knows how to build a form with the constructor passed in its own constructor.
So, every time you need a different form, you create a new factory.
ShapeFactory factory = new CircleFactory(5); Shape shape = factory.Create(); Console.WriteLine(shape.Area()));
I think this is the answer to your first and second question.
So, 3: What you can do to not modify your class is to use a strategy template to pass at runtime a method for implementing this method:
public interface IPerimeter { int calculatePerimeter(); } public class Circunference : IPerimeter { public int calculatePerimeter(Circle circle) { return 2*pi*circle.radius; } } public class Circle : Shape { public int Radius { get; set; } private IPerimeter perimeter; public Circle(int radius, IPerimeter perimeter) { this.Radius = radius; this.perimeter = perimeter; } public Circunference() { perimeter.calculatePerimeter(this); } public override double Area() { return 3.14 * this.Radius * this.Radius; } }
Hope this helps with your learning.
guijob
source share