Note that there are some explanatory texts on larger screens.

plurals
  1. POObject Oriented Design Problem, Liskov Substitution Principle
    primarykey
    data
    text
    <p>I'm making the design of a OO framework and I'm facing the following problem.</p> <p>Let's say that in the framework I have a <em>Shape</em> interface and the users are free to implement and extends (adding new functions) the <em>Shape</em> interface to create their own figures, e.g. <em>Square</em> and <em>Circle</em>. To make these new objects available the users have to register them into a <em>ShapeFactory</em> specifying the name of the shape (string) and the object.</p> <p>Furthermore, the framework provides an interface called <em>ShapeWorker</em> which defines the following function:</p> <pre><code>class ShapeWorker { public: void processShape( Shape&amp; shape ) = 0; }; </code></pre> <p>The users are free to implement the <em>ShapeWorker</em> interface to make specific shape worker, e.g. <em>SquareWorker</em> and <em>CircleWorker</em>. To make these new objects available the users have to register them into a <em>WorkerFactory</em>, specifying the name of shape (string) and the object.</p> <p>At a certain point, the framework, given a string representing the shape's name, creates a new <em>Shape</em>, by using the <em>ShapeFactory</em>, and afterwards (somewhere else in the code) creates a new <em>ShapeWorker</em>, by using the <em>WorkerFactory</em> with the same shape's name. The <em>processShape</em> is then called providing the <em>Shape</em> instance created before.</p> <pre><code>[ ... ] Shape* myShape = shapeFactory.create( shapeName ); [ ... ] ShapeWorker* myWorker = workerFactory.create( shapeName ); myWorker-&gt;processShape( *myShape ); [ ... ] </code></pre> <p>The point is that, doing so, I force the user implementing, for example, the <em>SquareWorker</em> to make a down-cast from <em>Shape</em> to <em>Square</em> into the <em>processShape</em> function so to access to the full <em>Square</em>'s interface:</p> <pre><code>class SquareWorker { public: void processShape( Shape&amp; shape ) { Square&amp; square = dynamic_cast&lt; Square&amp; &gt;( shape ); // using Square interface } }; </code></pre> <p>This is against the Liskov substitution principle.</p> <p>Now, is this approach wrong? What would it be the better solution? Note that I don't want to implement the processShape as <em>Shape</em>'s member function.</p> <p>I hope the description has been clear enough.</p> <p>Thanks in advance for your help.</p> <p>Simo</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload