Insight 17.5.1.
Remember: copy/paste is a design flaw. We want to avoid repeating significant pieces of program logic.
Point that represents a point in 2D space. Now we would like to build a class to represent a Circle. A circle is defined by its center and its radius - given those two pieces of information, we can calculate anything else we might need like its area or diameter.
classDiagram
class PrimitiveCircle{
-m_radius : double
-m_centerX : double
-m_centerY : double
+getRadius() double
...otherFunctions()
}
Point class. For example, if I want to know if two circles overlap, I could see if the distance between their centers is less than the sum of their radii. To do that, I would need to use the distance formula. Point already has a method to do the distance formula - copy/pasting that code into Circle sounds like the wrong approach.
classDiagram
class Circle{
-m_radius : double
-m_center : Point
+getRadius() double
...otherFunctions()
}
classDiagram
class Circle{
-m_radius : double
-m_center : Point
+getRadius() double
...otherFunctions()
}
class Point{
-m_x : double
-m_y : double
+getX() double
+getY() double
...otherFunctions()
}
Circle *-- Point