Note that there are some explanatory texts on larger screens.

plurals
  1. POObject subclassing approaches
    primarykey
    data
    text
    <p>Suppose I was designing a component representing grips on a UI control for resizing it. </p> <p><img src="https://i.stack.imgur.com/Hgplp.jpg" alt="Control"></p> <p>now obviously each grip is not exactly the same in terms of behavior, for instance the top left grip will have to display a North West South East mouse cursor and dragging it will change the size of the control whereas the top middle grip needs to show a north south mouse cursor and dragging it will only allow you to change the height.</p> <p>In terms of implementation there are two basic approaches:-</p> <p><strong>(1)</strong> a constructor which requires you to pass in information as to what kip of grip you are designing and then have a huge switch statement inside to define the actual behavior of the grip like so :-</p> <pre><code>- initWithGripType(int)gripType { switch(gripType { case TOP_MIDDLE_GRIP: cursorType = northSouthCursor; draggedMovement = upDown; break; case TOP_LEFT_GRIP: cursorType = northWestSouthEastCursor; draggedMovement = upDownLeftRight; break; ... ... ... } } </code></pre> <p><strong>(2)</strong> have a grip base class that defines the common attributes. e.g painting a grip is the same no matter which grip it is. And then sub classes for each grip type liks so :-</p> <pre><code>@interface Grip : NSObject { NSCursor _cursorType; int _draggedMovement; } // force init to be unavilable for base class - (id)init __attribute__((unavailable("Instantiate subclass instead."))); - (void)drawGrip; @end; @interface GripTopLeft : Grip { } - (id)init; @end; @interface GripBottomLeft : Grip { } - (id)init; @end; ... ... ... </code></pre> <p>Approach two has the advantage that I don't have to maintain any switch statements and can add new grip types at will, the downside is that if I have many grip types, each one is now a separate source and header file.</p> <p>Is there a third approach?</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.
    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