Assume for this polygon class:
public class Polygon { Point[] _vertices; public class Polygon(Point[] vertices) { _vertices = vertices; } }
To make triangles, squares, hexagons, you would prefer:
- Inherit from Polygon your Triangle, Square, etc. a class that provides a specific constructor and generate points programmatically?
- Add a static
CreateSquare method that returns a ready-to-use Polygon class?
It:
public class Square : Polygon { public class Polygon(double size) { _vertices = new Point[]{ new Point(0,0), new Point(size,0), new Point(size,size), new Point(0,size)}; } }
or that:
public class Polygon { Point[] _vertices; public class Polygon(Point[] vertices) { _vertices = vertices; } public static Polygon CreateSquare(double size) { double verts = new Point[]{ new Point(0,0), new Point(size,0), new Point(size,size), new Point(0,size)}; return new Polygon(verts); } }
Which approach is more correct in terms of OOP programming? Note that derived classes do not add anything to the original Polygon one.
Also, in the second case, is there any convenient naming convention?
Is there any additional approach that I don't know about?
Thanks.
source share