RE: Memory Management: Revamped Engine Example
RE: Memory Management: Revamped Engine Example
- Subject: RE: Memory Management: Revamped Engine Example
- From: Jordan Evans <email@hidden>
- Date: Sat, 31 Dec 2005 15:06:33 -0800 (PST)
Thanks for the help so far everone who responded.
I've tried to consider everyones input. Here is my
latest revamp. Any feedback is appreciated.
I added an attachement that is an XCode file of what
is below. I'm new to this site, so I'm not sure if
this works.
#import <Foundation/Foundation.h>
//I found a simple jet engine image with parts
labeled, so I had something to go by. The link
follows.
//
http://images.google.com/imgres?imgurl=http://virtualskies.arc.nasa.gov/aeronautics/tutorial/images/TurbEngine.gif&imgrefurl=http://virtualskies.arc.nasa.gov/aeronautics/tutorial/structure.html&h=154&w=375&sz=5&tbnid=y-fmuMR-BWoJ:&tbnh=48&tbnw=118&hl=en&start=17&prev=/images?q=engine+parts+tutorial&svnum=100&hl=en&lr=lang_en&client=safari&rls=en&sa=G
//These are the jet engine parts in the diagram, which
are used as filler objects for the Engine Class.
@interface CombustionChamber : NSObject{} @end
@implementation CombustionChamber @end
@interface Compressor : NSObject{} @end
@implementation Compressor @end
@interface FuelInjector : NSObject{} @end
@implementation FuelInjector @end
@interface Shaft : NSObject{} @end
@implementation Shaft @end
@interface Turbine : NSObject{} @end
@implementation Turbine @end
@interface Engine : NSObject
{
NSMutableArray *engineParts;
}
- (id)init;
- (void)maintenance:(NSArray*)badParts;
@end
@implementation Engine
- (id)init;
{
[super init];
engineParts = [[NSMutableArray alloc] init];
//Here are the parts for this engine. I have added
two Fuel Injectors.
[engineParts addObject:[[CombustionChamber alloc]
init]];
[engineParts addObject:[[Compressor alloc] init]];
[engineParts addObject:[[FuelInjector alloc] init]];
[engineParts addObject:[[FuelInjector alloc] init]];
[engineParts addObject:[[Shaft alloc] init]];
[engineParts addObject:[[Turbine alloc] init]];
return self;
}
- (void)dealloc;
{
[engineParts release];
[super dealloc];
}
- (void)maintenance:(NSArray*)badParts
{
int i;
id PartClass;//This is used to dynamically choose the
kind of class that will be used to allocate engine
parts.
NSLog(@"Parts before maintenance:\n");
for(i=0; i<[engineParts count]; i++)
NSLog(@"Part%i: %@", i, [[engineParts
objectAtIndex:i] description]);
//Remove and replace bad parts.
for( i=0; i<[badParts count]; i++ )
{
PartClass = [[engineParts objectAtIndex:[[badParts
objectAtIndex:i] intValue]] class];
[engineParts removeObjectAtIndex: [[badParts
objectAtIndex:i] intValue]];
[engineParts insertObject:[[PartClass alloc] init]
atIndex:[[badParts objectAtIndex:i] intValue]];
}
NSLog(@"Parts after maintenance:\n");
for(i=0; i<[engineParts count]; i++)
NSLog(@"Part%i: %@", i, [[engineParts
objectAtIndex:i] description]);
printf("\n");
}
@end
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool
alloc] init];
Engine *myEngine = [[Engine alloc] init];
//This is the bad parts list for the first
maintenance. The parts to be replaced are the first
two.
NSArray *badParts1 = [[NSArray alloc]
initWithObjects: [[NSNumber alloc] initWithInt:0],
[[NSNumber alloc] initWithInt:1], nil ];
//This list causes the last part to be removed.
NSArray *badParts2 = [[NSArray alloc]
initWithObjects: [[NSNumber alloc] initWithInt:5], nil
];
[myEngine maintenance:badParts1];
[myEngine maintenance:badParts2];
[badParts1 release];
[badParts2 release];
[myEngine release];
[pool release];
return 0;
}
__________________________________________
Yahoo! DSL Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
Attachment:
MemoryEngine.m
Description: 1471405315-MemoryEngine.m
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden