Recommendations for creating OOP objects

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.

+5
source share
3 answers

There is never a definite answer to this question without knowing the context in which these classes are used, however, if there is no need for more specific classes, I would not have created them. You would not create RedPolygon or BlueSquare if you add the Color property to Polygon .

Once you can create a child class as square-exclusive behavior, I could, for example, imagine that some mathematical operations, such as a shock test, will be faster on squares than on polygons - it-be-be-squares .

+3
source

To bow to the principles of SOLID . I would go with a solution using inheritance.

The Open / Closed principle states that objects must be open for expansion, but closed for modification. To get a different form, you do not need to touch the Polygon class.

If someone wants to add Hexagon, he will simply create a new source file with Hexagon inheritance from Polygon instead of modifying the existing Polygon source, which will require him to access your source. Also, modifying existing source code will add the risk of introducing errors with your new code. Although for your simple example, the risk would not be so great as to simply create new forms, the risk would increase as soon as you want to add some calculations specific to different shapes.

You can argue with the YAGNI principle, which states that you should not implement any function if not necessary. Therefore, if you do not need to consider the further reasons for the extension, you get full control over your source and all programs using this source, you can also go with another solution using the static "factory" methods.

For a more detailed discussion, you can refer to this article.

+2
source

It makes no sense to add specific classes for Square, Triangle, etc., if they do not extend Polygon.

Your message that they produce classes does not add anything to Polygon, it may indicate a relationship of "HAS A", not "IS A". IS A Polygon square, but it has tops.

However, I would not put them as static methods in a class if you are not going to serve only a fixed number of polygon types. Since there are an infinite number of polygons, will you edit the class when you need to add a new shape?

If the polygon is an elementary object, it should be left as a simple elementary object and left its factory to create a specific one.

Again, this suggests that this is more like an exam question, which is a bit ambiguous and deliberate at the moment when everything becomes blurry.

+1
source

Source: https://habr.com/ru/post/1269221/


All Articles