Amazon.com Widgets 11.5 Composite Objects
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 01, 2014, 09:22:51 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 11
| | |-+ 11.5 Composite Objects
Pages: [1] Go Down
Print
Author Topic: 11.5 Composite Objects (Read 2209 times)
iOSro
Newbie
*
Posts: 5






on: March 13, 2012, 07:38:15 AM

Hi all,

just trying to see if i am understanding correct the concept of Compositions ...
so is the below the correct implementation we should look for ( while on a run test i would say so, yes - but want to be sure ... )

#import "Square.h"
#import "Rectangle.h"

@implementation Square

@synthesize myRectangle;

-(Square *) initwithSide:(int)s
{
   
    id mySquare = [super init];
   
    if ( !myRectangle )
        myRectangle = [[Rectangle alloc] init];
   
    [myRectangle setWidth:s andHeight:s];
   
    return mySquare;
}

-(void) setSide: (int)s
{
    [myRectangle setWidth:s andHeight:s];
}

-(int) side
{
    return [myRectangle width];
}

-(int) area
{
    return [myRectangle area];
}

-(int) perimeter
{
    return [myRectangle perimeter];
}

@end
 
Last Edit: March 13, 2012, 08:09:53 AM by iOSro Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: March 13, 2012, 09:10:10 AM

Yes, you've got it!  ;0

I would also suggest overriding init to call initWithSide to make sure the Rectangle object gets allocated if someone does an alloc/init.

Also, you should always check the result of the [super init] call to make sure it succeeds and assign it back to "self," which should also be returned.   That's in case a parent init changes the location of the object (it has the right to do that).

Cheers,

Steve Kochan
Logged
iOSro
Newbie
*
Posts: 5






Reply #2 on: March 14, 2012, 04:43:04 AM

Hi Steve,

thank you for your reply, much appreciated.
I felt i am missing something, i think the correct version should be
------------------------------------------------------------------
-(id) init
{
    self = [self initWithSide:0];
   
    return self;
}

-(Square *) initWithSide:(int)s
{
    self = [super init];
   
    if ( self )
    {
        if ( !myRectangle )
            myRectangle = [[Rectangle alloc] init];
   
        [myRectangle setWidth:s andHeight:s];
    }
   
    return self;
}
------------------------------------------------------------------
Still i wonder about: if i can do something more in my coding ( me as developer of the Square in this scenario ) to help other developers that will be using my Square class to make them aware that the init method is override and it defaults the initWithSide to 0 in my case. Could be something more i can do ? - thank you  

Last Edit: March 14, 2012, 06:10:27 AM by iOSro Logged
seerex
Full Member
***
Posts: 177


Email




Reply #3 on: March 14, 2012, 07:01:23 AM

To let other developers know about stuff in your code, using good variable / method / class names + commenting out sections that arent self-explanitory, you should be alright Smiley
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
iOSro
Newbie
*
Posts: 5






Reply #4 on: March 14, 2012, 08:37:34 AM

Hi seerex,
thanks for your comments.

Yes, indeed, it looks that's about it ( have just got an answer in Chapter 15 as i go through ... )  - just diving here into Chapter 15 AddressBook with the initWithName:@"NoName" called from init of the AddressBook.

But i can't help for still asking - what about if all the Square ( i keep the Square reference still ) is part of a library on which only the header will be available to developers ... yet the developers can notice that i have a new initWithSomethingOfMyOwn method and they can use it - while others could accidentally use the init that calls the initWithSomethingOfMyOwn with a default  ... maybe throwing a run time error notifying that the init was used and a default initWithSomethingOfMyOwn was used while somethingOfDeveloperOwn should be used :-) ... dunno - just a thought and still wonder if a mechanism is not desired for the library scenario - for the other scenario i agree on comments etc ...

@"thanks";
Logged
microapple
Jr. Member
**
Posts: 83



WWW Email




Reply #5 on: August 28, 2012, 09:40:08 AM

Hello,
it's all about hiding the intern mecanism of our class Squra because we can sublass the Rectangle and everything gonna be all right , but then we want to hdie for exemple setWidth:andHeight: method .... so you break our goal by

 
Code: (Objective-C)
@synthesize myRectangle;

so you make the Rectagle object avlaible for the user of Square class , and we don't want that , we make a compsite object to hide that our Square ! why it's matter ? okay imagine that I will call the getter method of your "myRectangle" then I can modify it without the Square class because I will get a Pointer to this object and I don't need to use Square class anymore , this is why when our objects contain other objects it the getter method of the objects of our class we return a copy , This is why i have learned from the book , and correct me if I am wrong Smiley
Logged

Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.