Multiple Inheritance

  • I have an abstract class M with subclasses MAB, MAX, MXB.

    There are several lengthy methods A which are used in MAB and MAX, and others methods B which are used in MAB and MXB.

    Methods A use a property NSUInteger index.
    Class MXB has no such property, is has NSIndexSet *indices instead.

    Similar for methods B.

    Currently these common methods are present in both classes.
    But: this is less than perfect - meaning that each change has to be done twice.

    What to do?
    Have a file methodsA.m which is included in both MAB and MAX (and same for methodsB.m) ?

    Other, better solutions?

    Kind regards,

    Gerriet.
  • Turn it into two separate protocols instead, and adopt whichever protocols are needed in the concrete implementations. Or if one protocol is a superset of the other, a protocol can extend another.

    --Graham

    On 24/05/2012, at 7:14 PM, Gerriet M. Denkmann wrote:

    > Other, better solutions?
  • Implement abstract classes that implement the methods, then use the runtime to copy the methods into the classes that should have them in +initialize.

    Bob

    On 24 May 2012, at 10:14, Gerriet M. Denkmann wrote:

    > I have an abstract class M with subclasses MAB, MAX, MXB.
    >
    > There are several lengthy methods A which are used in MAB and MAX, and others methods B which are used in MAB and MXB.
    >
    > Methods A use a property NSUInteger index.
    > Class MXB has no such property, is has NSIndexSet *indices instead.
    >
    > Similar for methods B.
    >
    > Currently these common methods are present in both classes.
    > But: this is less than perfect - meaning that each change has to be done twice.
    >
    > What to do?
    > Have a file methodsA.m which is included in both MAB and MAX (and same for methodsB.m) ?
    >
    > Other, better solutions?
    >
    >
    > Kind regards,
    >
    > Gerriet.
  • On 24 May 2012, at 17:37, Graham Cox wrote:

    > Turn it into two separate protocols instead, and adopt whichever protocols are needed in the concrete implementations. Or if one protocol is a superset of the other, a protocol can extend another.

    I thought that a protocol only declares methods and properties.

    How do I avoid duplicating method implementations using a protocol?

    Kind regards,

    Gerriet.
  • On May 24, 2012, at 4:14 AM, Gerriet M. Denkmann wrote:

    > I have an abstract class M with subclasses MAB, MAX, MXB.
    >
    > There are several lengthy methods A which are used in MAB and MAX, and others methods B which are used in MAB and MXB.
    >
    > Methods A use a property NSUInteger index.
    > Class MXB has no such property, is has NSIndexSet *indices instead.
    >
    > Similar for methods B.
    >
    > Currently these common methods are present in both classes.
    > But: this is less than perfect - meaning that each change has to be done twice.
    >
    > What to do?
    > Have a file methodsA.m which is included in both MAB and MAX (and same for methodsB.m) ?
    >
    > Other, better solutions?

    Perhaps this is a case calling for composition.  Maybe a MAB and a MAX should own an A object, while a MAB and MXB should own a B object.  The A object would implement the A methods and the index property.  The B object would implement the B methods and whatever properties.  If the A and B methods are part of the public interface of the subclasses, then they could implement thin wrappers that simply forward through to the internal objects.  You could use protocols to enforce the requirement that the subclass implement the required interface faithfully, if you want.

    Or, you can leverage the preprocessor as you suggest.

    Regards,
    Ken
  • Implement both A and B in the base class M, but don't declare them in its @interface. Then MAB and MAX can both declare method A and have implementations that just call super. Likewise for B.

    Or, factor out A into a helper class, and similarly for B. Then have MAB and MAX instantiate an A helper object and delegate method A to it.

    Or, similarly to #2, implement A and B as functions that take the 'receiver' object as the first parameter. Then you can easily implement method -A as a call to A(self).

    —Jens
  • On May 24, 2012, at 4:15 AM, Gerriet M. Denkmann wrote:

    > How do I avoid duplicating method implementations using a protocol?

    You don't, other than by using techniques like the ones I described. Protocols are just for the convenience (and type-safety) of the _users_ of a class; they don't really simplify the implementation.

    —Jens
previous month may 2012 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today