[Biococoa-dev] Factories
Koen van der Drift
kvddrift at earthlink.net
Sat Aug 28 19:31:11 EDT 2004
Hi,
Still thinking about the best way to implement a flyweight pattern to
create symbols. I finally think I understand how this works and made a
new class called BCAminoAcidFactory. Here's its implementation:
#import "BCAminoAcidFactory.h"
static NSMutableDictionary *aminoAcidDictionary;
@implementation BCAminoAcidFactory
- ( BCAminoAcid *)aminoAcidWithSymbol: (unichar) aSymbol
{
BCAminoAcid *aminoAcid = nil;
NSString *symbolString = [NSString stringWithCharacters: &aSymbol
length: 1];
if ( aminoAcidDictionary == nil )
{
aminoAcidDictionary = [[NSMutableDictionary alloc] init];
}
aminoAcid = [aminoAcidDictionary objectForKey: symbolString];
if ( aminoAcid == nil )
{
aminoAcid = [[BCAminoAcid alloc] initWithSymbol: aSymbol];
[aminoAcidDictionary setObject: aminoAcid forKey: symbolString];
}
return aminoAcid;
}
This is in turn called by any object that needs an amino acid. So
initWithSymbol is never called directly, alwayd through the factory
object. If the aa already exists, it will return the one that's in the
static dictionary, otherwise it'll create a new one.
Let me know what you think, and if I overlooked something critical.
I'll wait for comments before committing it to CVS.
- Koen.
More information about the Biococoa-dev
mailing list