Re: Class design question
Re: Class design question
- Subject: Re: Class design question
- From: Ken Tozier <email@hidden>
- Date: Sun, 13 Feb 2005 13:44:25 -0500
On Feb 13, 2005, at 1:27 PM, Jason Jobe wrote:
ummm... I still not totally dialed into what the real differences are but perhaps more of an EOF "GenericRecord" plus "EntityDescription" might be more approriate. Something along these lines. For sake of example the Entity only has one attribute but this idea could be extended to by adding an AttributeDescription, etc.
@interface GenericObject {
Entity *entity;
}
- setValue:val forKey:(NSString*)key;
- valueForKey:(NSString*)key
{
if ([entity supportsKey:key])
return [entity loadDataForObject:self forKey:key];
else
// Or Raise if you wish
return nil;
}
@end
@interface Entity {
NSString *name; // the "type" if you will
NSDictionary *attributeTypes;
}
- loadDataForObject:owner forKey:(NSString *)key
{
NSString *attType = [attributeTypes valueForKey:key];
Class attributeClass = (attType ? NSClassFromString (attType) : nil);
id value;
if (attributeClass)
value = [[[attributeClass class] alloc] initWithContentsOfFile:[self filePathForKey:key forObject:owner]]
else
value = nil;
return value;
}
@end
Note that the "entity" can be shared among many instances and the Class for the attribute need only support "initWithContentsOfFile:".
Is this the sort of thing you are looking to do?
Sort of. I'm writing a database app (for a learning experience among other "better" reasons) and so need much finer control of the disk reads and writes than "initWithContentsOfFile" permits. I need to be able to access any chunk of data anywhere in a file that could potentially be terabytes in size, so need a way to jump to spots in a file, read a chunk of data and decode it correctly. Although I'm factoring the data by size and type, I want to support all of the SQL types in addition to some richer types and capabilities than SQL can do natively. This means that I need either a class for each SQL type, or a function pointer array arrangement to correctly decode the saved data.
I'll study your example more too see how I can adapt it.
Thanks!
Ken
On Feb 13, 2005, at 1:04 PM, Ken Tozier wrote:
On Feb 13, 2005, at 12:50 PM, Jason Jobe wrote:
What methods are being overloaded for each class and what is the nature of the functional variance of each one.
Although not dictionaries, I've adopted the NSMutableDictionary syntax for adding and changing values (ie: setObject:ForKey etc...)
The variance is primarily how big a chunk to read from disk into the one changing instance variable. This variable can basically be any size and any type.
For excellent instruction in data and functional abstractions I would refer you to
Thanks, I'll check it out.
Ken
Structure and Interpretation of Computer Programs
<x-tad-bigger>Video Lectures by Hal Abelson and Gerald Jay Sussman</x-tad-bigger>
at http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/
I highly recommend them.
On Feb 13, 2005, at 12:33 PM, Ken Tozier wrote:
Hi all,
I've been working on a class for about two weeks now and have tried out several different ways of approaching the problem but still don't know the "best" way to write it.
In the first pass I created a very generic class with an array of function pointers to handle the many different data types the class was to handle. The code was very compact but was kind of ugly from the OOP standpoint as it required careful synchronization by the programmer between constants and function pointer indexes.
In the second pass I created dedicated classes for each data type which adheres better to the Cocoa way of doing things, but yielded twenty different classes that vary only in the size of a single instance variable (Much like NSNumber) These classes are very nice in the sense that they're easy to use and code but it seems incredibly wasteful to have 20 different fairly large classes to handle a difference of a single variable.
I thought of using unions for the changing variable, but the data sizes can vary hugely (2 bytes up to megabytes) and for that, unions would cause a lot of wasted disk space.
It seems like this problem is tailor made for C++ templates, write one class and template the changing variable but I've always hated the way you have to call C++ template classes. Also my C++ experience is very limited so don't know if it's awkward to mix it with Objective C.
All three have their plusses and minuses
Option 1 is compact and fast but is "ugly" OOP wise
Option 2 is nice OOP wise but is incredibly bloated
Option 3 may be the best choice but I don't know C++ very well
I'd like to hear everyone's arguments pro and con for the different approaches as eventually others will have to be able to work on the source, not just me.
Thanks for any help
Ken
_______________________________________________
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
- jason
email@hidden
_______________________________________________
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
- jason
email@hidden
_______________________________________________
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