Re: Circles and ellipses

From: Robert C. Martin <rmartin_at_objectmentor.com>
Date: Wed, 22 Aug 2001 02:38:11 GMT
Message-ID: <3b831894.9558574_at_news.supernews.com>


On Sun, 19 Aug 2001 06:08:28 GMT, "David Cressey" <david_at_dcressey.com> wrote:

>But let's return to cicle/ellipse for a minute? Why would anyone WANT to
>consider a circle an ellipse? Well, I can see one immediate benefit: if
>all the relevant operations that work on an ellipse also work on a circle,
>then you can get more useful software written by construing a cirlce as a
>specialized ellipse, and not writing an implementation for a whole
>different class.

Granted.

Now, consider that Ellipse has the following method:

        public setFoci(Point f1, Point f2);

This would be inherited by Circle. Circle can't tolerate two foci, so the implementation of this function in Circle would have to ignore one of the two foci. This is wierd.

To make matters worse, some client of Ellipse could call setFoci and fully expect that the excentricity of the resulting ellipse to reflect to two foci. If a circle is passed to this client, his expectation would be violated.

This is the essential problem that the LSP (Liskov Substitution Principle) tries to resolve; or at least identify. Clients of a base class can be confounded by certain derivatives that appear natural. Circle is a natural derivative of Ellipse but can confound clients of Ellipse. This implies that the "naturalness" of the derivation is not useful.

Robert C. Martin    | "Uncle Bob"              | Software Consultants
Object Mentor Inc.  | rmartin_at_objectmentor.com | We'll help you get
PO Box 5757         | Tel: (800) 338-6716      | your projects done.
565 Lakeview Pkwy   | Fax: (847) 573-1658      | www.objectmentor.com
Suite 135           |                          | www.XProgramming.com
Vernon Hills, IL,   | Training and Mentoring   | www.junit.org
60061               | OO, XP, Java, C++, Python|

"One of the great commandments of science is:

    'Mistrust arguments from authority.'" -- Carl Sagan Received on Wed Aug 22 2001 - 04:38:11 CEST

Original text of this message