• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sensible way to extend base class?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sensible way to extend base class?


  • Subject: Re: Sensible way to extend base class?
  • From: Michael Ash <email@hidden>
  • Date: Wed, 20 May 2009 11:14:55 -0400

On Wed, May 20, 2009 at 8:09 AM, Graham Cox <email@hidden> wrote:
> I have a base class in a framework, let's call it A. This has a number of
> subclasses, B C and D.
>
> I have some functionality I'd like to add that would apply to all A, B, C
> and D. So a category on A would be fine - except that I need some support in
> the form of a couple of ivars. The added functionality isn't appropriate to
> the framework, so though I have the source for A, adding ivars that have no
> place there grieves me.
>
> It also grieves me that the solution appears to be separate subclasses of B,
> C and D to add the functionality which must be duplicated for each class.
>
> Any solution I've missed?

A good compromise between redundancy and evil can be to put the
required functionality into an unrelated class, call it X. Then you
subclass B, C, and D in a minimal way, just enough to add an ivar for
an instance of X and pass messages through it. Depending on what X
implements, this could involve simply providing an accessor for the X,
implementing methods in your subclasses that call through to the X, or
even implementing forwarding so that all X methods get automatically
passed along. (The new -forwardingTargetForSelector: method in Leopard
makes this sort of forwarding super easy, although it's poorly
documented and largely unknown.)

If you want to get into extreme evils-ville, you can dynamically
create subclasses of all subclasses of A at runtime. Not recommended,
but it could be fun.

Mike
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Sensible way to extend base class?
      • From: Jerry Krinock <email@hidden>
References: 
 >Sensible way to extend base class? (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Sensible way to extend base class?
  • Next by Date: Re: Sensible way to extend base class?
  • Previous by thread: Re: Sensible way to extend base class?
  • Next by thread: Re: Sensible way to extend base class?
  • Index(es):
    • Date
    • Thread