3 ms·
> Each shape (such as a rectangle or an oval) had a different set of handles, Yet, that's not what the code says. The objects have multiple methods with simila
by kazinator 3y ago
> Each shape (such as a rectangle or an oval) had a different set of handles,
Yet, that's not what the code says. The objects have multiple methods with similar signatures representing handling.
The right thing here is to make the handles objects. The objects know what their parent is and send it messages "I have moved". There is one method in the shape which is reponsible for handle-initiated movement. The object looks at which handle has moved (it's an argument), and other state like Shift constraint, and adjusts its representation accordingly.
The behavior of how to respond to which handles should still be the responsibility of the shape. Commonality there could be factored out into some common functions such that if you need to special-case something, the in that shape you stop using the common functions entirely, or use them as a fallback for whatever behavior remains that is common.
Multiple dispatch is made for this; the movement behavior is a combination which shape and which handle. With a precise specialization of a multiply-dispatched method, you can code behavior for an exact shape and handle, overriding any less specific definition.
This is all a Solved Problem.