Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.objective-c > #225

Re: A question on designated initializers

From "Pascal J. Bourguignon" <pjb@informatimago.com>
Newsgroups comp.lang.objective-c
Subject Re: A question on designated initializers
Date 2015-11-24 04:58 +0100
Organization Informatimago
Message-ID <87wpt8ujzb.fsf@kuiper.lan.informatimago.com> (permalink)
References <n2tcmc$t64$1@news.albasani.net> <n2u08d$7vs$1@dont-email.me> <n30iuo$c4b$1@news.albasani.net>

Show all headers | View raw


Jon Rossen <jonr17@comcast.net> writes:

> 1. Just a workflow question:  Re: the 'convenience' init methods that
> may be helpful to get objects accompany the designated one:  Is it
> typical for a developer when building a class to add the 'convenience'
> init methods later on or sort of on an 'as needed' basis depending?
> I'm not asking this as to what's good or bad, but am assuming it just
> depends on the situation and in your experience how does this usually
> play out?

The important criteria is whether the superclass -init establishes the
class invariant or not.


> 2. Ok, hopefully this question won't open up a can of worms.  What
> about sub-classes?  I guess this is a two part question:
>  a) If there's a sub-class that doesn't particularly need it's own
> class designated initializer, it could just use it's super's
> designated initializer, correct?  If so, is this a common design
> pattern?

You can change the designated initializer for each sub*class. 
But since this is not a property checked by the compiler, it would be
very hard for the programmer to track which is the designated
initializer of the current sub*class.

> b) If the sub-class *does* need a designated initializer, I suppose it
> could do an override of the super's version and that would be
> ok. However, I would think that any additional initialization would
> have to be done via assessor methods as the method names would have to
> be the same.  Would this be considered bad form to do this?  Or would
> the best course of action would be to write a new designated
> initializer and not do an override?

There's never a "need" for a specific designated initializer.  You can
always either establish the invariant in the -init method (by setting
good default ivars), or extend the invariant to allow for
"half-initilized" instances.

    @interface Rectangle
    {
        int width;
        int height;
    }
    -(BOOL)isValid;
    -(BOOL)invariant;
    -(void)setWidth:(int)aWidth;
    -(void)setHeight:(int)aHeight;
    @end

    // half-initialized instances:

    @implementation Rectangle
    -(BOOL)isValid{return((0<width)&&(0<height));}
    -(BOOL)invariant{return(![self isValid] || ((0<width)&&(0<height)));}
    -(void)setWidth:(int)aWidth{width=aWidth;}      // post: ![self isValid] || [self invariant]
    -(void)setHeight:(int)aHeight{height=aHeight;}; // post: ![self isValid] || [self invariant]
    @end

    [[[Rectangle alloc]init]invariant] --> YES
    [[[Rectangle alloc]init]isValid]   --> NO

    // default-initialized instances:

    @implementation Rectangle
    -(id)init{if((self=[super init])){width=1;height=1;}return(self);}
    -(BOOL)isValid{return YES;}
    -(BOOL)invariant{return((0<width)&&(0<height));}
    -(void)setWidth:(int)aWidth{if(0<aWidth){width=aWidth;}} 
       // pre:  [self invariant] && good=(0<aWidth)
       // post: [self invariant] && (good => width==aWidth)
    -(void)setHeight:(int)aHeight{if(0<aHeight){height=aHeight;}}
       // pre:  [self invariant] && good=(0<aHeight)
       // post: [self invariant] && (good => height==aHeight)
    @end

    [[[Rectangle alloc]init]invariant] --> YES
    [[[Rectangle alloc]init]isValid]   --> YES


> Last one, slightly unrelated to 2a and 2b but still about
> relationships between classes and sub-classes:
> c) At each step down the hierarchy through sub-classes, the 'plain'
> init would have to be overridden and it would have to return the
> current class' designated initializer, correct?  In other words, you'd
> have to re-write init for each sub-class where there is a new
> designated initializer.

No.  If you don't change the designated initializer, then the superclass
-init that sends the designated initializer message will invoke the
designated initializer method of the current class.  You only need to
write a new -init if it has to do something different to ensure the
class invariant.


   enum {red,green,blue};
   @interface ColoredRectangle
   {
      int color;
   }
   @end

   @implementation ColoredRectangle

   -(id)initWithWidth:(int)w andHeight:(int)h{
       if((self=[super initWithWidth:w andHeight:h])){
          color=red;
       }
       return self;
   }

   -(BOOL)invariant{
        return [super invariant]
                && ((color==red)||(color=green)||(color==blue));}

   @end

   [[[ColoredRectangle alloc]init]invariant] --> YES

   [[ColoredRectangle alloc]init] 
       calls -[Rectangle init]
       which sends -initWithWidht:andHeight: to self
       which calls -[ColoredRectangle initWithWidht:andHeight:]
       which sends -initWithWidht:andHeight: to super
       which calls -[Rectangle initWithWidht:andHeight:]
       which sends -init to super
       which calls -[NSObject init]

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk

Back to comp.lang.objective-c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 13:39 -0800
  Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 14:48 -0800
    Re: A question on designated initializers Louis Wu <louiswu@ringworld.net> - 2015-11-22 16:26 -0800
      Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 17:24 -0800
        Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:39 +0100
  Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 01:18 +0100
    Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 16:49 -0800
      Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:02 +0100
        Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-27 17:19 -0800
          Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-28 13:22 +0100
            Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-28 17:37 -0800
              Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-29 03:45 +0100
  Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-22 19:15 -0800
    Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-23 18:44 -0800
      Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-24 04:58 +0100
        Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 16:42 -0800
          Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 02:00 +0100
        Re: A question on designated initializers Greg Parker <gparker@apple.com> - 2015-11-25 01:25 -0800
      Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 07:38 -0800
        Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 15:54 -0800
          Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 01:49 +0100
            Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:51 -0800
              Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 04:41 +0100
                Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 00:25 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 14:16 +0100
                Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 17:30 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-26 03:03 +0100
                Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-25 09:08 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 18:41 +0100
                Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-12-02 14:37 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-12-03 00:58 +0100
          Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:29 -0800

csiph-web