Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C organization of hierarchies of class clusters
    primarykey
    data
    text
    <p>This is a question of class design with Objective-C. Here is an example:</p> <p>File systems have files and directories. Both are "nodes". Walking a directory for example yields a list of nodes, some being [sub]directories, other being files.</p> <p>This points to the following client-side abstract view of the class hierarchy:</p> <pre><code>@interface Node: NSObject {} @end @interface Directory: Node {} @end @interface File: Node {} @end </code></pre> <p>So far so good. At this point, all three classes are abstract. Now going to implementation, you realize there are two main routes: using URLs (recommended by Apple for Mac OS X ≥ 10.6), or paths (only possible way for Mac OS X ≤ 10.5 or <a href="http://cocotron.org" rel="nofollow">Cocotron</a>).</p> <p>So now, you need to develop two concrete implementations of each of the three abstract classes above:</p> <pre><code>// Node subclasses @class NodeWithPath; @class NodeWithURL; // Directory subclasses @class DirectoryWithPath; @class DirectoryWithURL; // File subclasses @class FileWithPath; @class FileWithURL; </code></pre> <p>Now consider, say, <code>FileWithURL</code>:</p> <ul> <li>it is a file, so it should inherit from <code>File</code>.</li> <li>it is a node implemented with an URL, so it should inherit from <code>NodeWithURL</code></li> </ul> <p>But <code>File</code> and <code>NodeWithURL</code> are not within the same class hierarchy line. Without multiple inheritance, there is no way to express that in Objective-C.</p> <p>So how would you design this situation? I can see two ideas:</p> <ul> <li>use protocols, which are a limited form of multiple inheritance.</li> <li>use members (has-a instead of is-a relationships).</li> </ul> <p>I tend to favor the protocol idea. In that case, <code>Directory</code> and <code>File</code> would be protocols, and the six concrete classes would inherit from a common <code>Node</code> superclass and conform to their counterpart protocol. <code>Node</code> would have two subclass hierarchies: one using URLs, one using Paths.</p> <p>Now there is the issue of hiding the implementation from the client code. A class cluster can be setup for this purpose with the <code>Node</code> common superclass. Client code would get objects typed as <code>Node&lt;File&gt;</code> or <code>Node&lt;Directory&gt;</code> as the case may be.</p> <p>Any additional/other/similar/different ideas?</p>
    singulars
    1. This table or related slice is empty.
    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