GO-V.ORG
As seen in

Circle–ellipse problem

The circle–ellipse problem in software development (sometimes called the square–rectangle problem) illustrates several pitfalls which can arise when using subtype polymorphism in object modelling. The issues are most commonly encountered when using object-oriented programming (OOP). By definition, this problem is a violation of the Liskov substitution principle, one of the SOLID principles. The problem concerns which subtyping or inheritance relationship should exist between classes which represent circles and ellipses (or, similarly, squares and rectangles). More generally, the problem illustrates the difficulties which can occur when a base class contains methods which mutate an object in... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

The circle–ellipse problem in software development (sometimes called the square–rectangle problem) illustrates several pitfalls which can arise when using subtype polymorphism in object modelling. The issues are most commonly encountered when using object-oriented programming (OOP). By definition, this problem is a violation of the Liskov substitution principle, one of the SOLID principles. The problem concerns which subtyping or inheritance relationship should exist between classes which represent circles and ellipses (or, similarly, squares and rectangles). More generally, the problem illustrates the difficulties which can occur when a base class contains methods which mutate an object in a manner which may invalidate a (stronger) invariant found in a derived class, causing the Liskov substitution principle to be violated. The existence of the circle–ellipse problem is sometimes used to criticize object-oriented programming. It may also imply that hierarchical taxonomies are difficult to make universal, implying that situational classification systems may be more practical. (defgeneric check-constraints (shape)) The accessors on shape objects. Constraints on objects need to be checked after either axis value is set. (defgeneric h-axis (shape)) (defgeneric (setf h-axis) (new-value shape) (:method :after (new-value shape) (check-constraints shape))) (defgeneric v-axis (shape)) (defgeneric (setf v-axis) (new-value shape) (:method :after (new-value shape) (check-constraints shape))) (defclass ellipse () ((h-axis :type real :accessor h-axis :initarg :h-axis) (v-axis :type real :accessor v-axis :initarg :v-axis))) (defclass circle (ellipse) ((radius :type real :accessor radius :initarg :radius))) A circle has a radius, but also a h-axis and v-axis that it inherits from an ellipse. These must be kept in sync with the radius when the object is initialized and when those values change. (defmethod initialize-instance :after ((c circle) &key radius) (setf (radius c) radius)) ;; via the setf method below (defmethod (setf radius) :after ((new-value real) (c circle)) ;; We use SLOT-VALUE, rather than the accessors, to avoid changing ;; class unnecessarily between the two assignments; as the circle ;; will have different h-axis and v-axis values between the ;; assignments, and then the same values after assignments. (setf (slot-value c 'h-axis) new-value (slot-value c 'v-axis) new-value)) After an assignment is made to the circle's h-axis or v-axis, a change of type is necessary, unless the new value is the same as the radius. (defmethod check-constraints ((c circle)) (unless (= (radius c) (h-axis c) (v-axis c)) (change-class c 'ellipse))) Ellipse changes to a circle if accessors mutate it such that the axes are equal, or if an attempt is made to construct it that way. (defmethod initialize-instance :after ((e ellipse) &key) (check-constraints e)) (defmethod check-constraints ((e ellipse)) (when (= (h-axis e) (v-axis e)) (change-class e 'circle))) Method for an ellipse becoming a circle. In this metamorphosis, the object acquires a radius, which must be initialized. There is a "sanity check" here to signal an error if an attempt is made to convert an ellipse which axes are unequal with an explicit change-class call. The handling strategy here is to base the radius off the h-axis and signal an error. This doesn't prevent the class change; the damage is already done. (defmethod update-instance-for-different-class :after ((old-e ellipse) (new-c circle) &key) (setf (radius new-c) (h-axis old-e)) (unless (= (h-axis old-e) (v-axis old-e)) (error "ellipse ~s can't change into a circle because it's not one!" old-e))) This code can be demonstrated with an interactive session, using the CLISP implementation of Common Lisp. $ clisp -q -i circle-ellipse.lisp [1]> (make-instance 'ellipse :v-axis 3 :h-axis 3) [2]> (make-instance 'ellipse :v-axis 3 :h-axis 4) [3]> (defvar obj (make-instance 'ellipse :v-axis 3 :h-axis 4)) OBJ [4]> (class-of obj) [5]> (radius obj) - NO-APPLICABLE-METHOD: When calling # with arguments (# ), no method is applicable. The following restarts are available: RETRY :R1 try calling RADIUS again RETURN :R2 specify return values ABORT :R3 Abort main loop Break 1 [6]> :a [7]> (setf (v-axis obj) 4) 4 [8]> (radius obj) 4 [9]> (class-of obj) [10]> (setf (radius obj) 9) 9 [11]> (v-axis obj) 9 [12]> (h-axis obj) 9 [13]> (setf (h-axis obj) 8) 8 [14]> (class-of obj) [15]> (radius obj) - NO-APPLICABLE-METHOD: When calling # with arguments (# ), no method is applicable. The following restarts are available: RETRY :R1 try calling RADIUS again RETURN :R2 specify return values ABORT :R3 Abort main loop Break 1 [16]> :a [17]> class Person { void walkNorth(int meters) {...} void walkEast(int meters) {...} } Now, a prisoner is obviously a person. So logically, a sub-class can be created: class Prisoner extends Person { void walkNorth(int meters) {...} void walkEast(int meters) {...} } Also obviously, this leads to trouble, since a prisoner is not free to move an arbitrary distance in any direction, yet the contract of the class states that a Person can. Thus, the class could better be named . If that were the case, then the idea that is clearly wrong. By analogy, then, a Circle is an Ellipse, because it lacks the same degrees of freedom as an Ellipse. Applying better naming, then, a Circle could instead be named and an ellipse could be named . With such names it is now more obvious that should extend , since it adds another property to it; whereas has a single diameter property, has two such properties (i.e., a major and a minor axis length). This strongly suggests that inheritance should never be used when the sub-class restricts the freedom implicit in the base class, but should only be used when the sub-class adds extra detail to the concept represented by the base class as in 'Monkey' is-an 'Animal'. However, stating that a prisoner can not move an arbitrary distance in any direction and a person can is a wrong premise once again. Any object which is moving to any direction can encounter obstacles. The right way to model this problem would be to have a contract. Now, when implementing walkToDirection for the subclass Prisoner, you can check the boundaries and return proper walk results.

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Circle–ellipse problem". 1760003856. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1315917351

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·