🍏 OBJ-C RUNES DIFFICULTY: Archmage Structural

The Flyweight

Sharing intrinsic ethereal essence to save memory.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Subschool: Efficiency
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// OBJC PATTERN //
@interface TKSigil : NSObject
@property (nonatomic, strong, readonly) NSString *runeName;
- (instancetype)initWithName:(NSString *)name;
- (void)glowAtPosition:(CGPoint)position;
@end
@implementation TKSigil
- (instancetype)initWithName:(NSString *)name {
    if (self = [super init]) { _runeName = name; }
    return self;
}
- (void)glowAtPosition:(CGPoint)position { }
@end

@interface TKSigilFactory : NSObject
@property (nonatomic, strong) NSMutableDictionary *sigils;
- (TKSigil *)sigilForName:(NSString *)name;
@end
@implementation TKSigilFactory
- (instancetype)init {
    if (self = [super init]) { _sigils = [NSMutableDictionary dictionary]; }
    return self;
}
- (TKSigil *)sigilForName:(NSString *)name {
    if (!self.sigils[name]) {
        self.sigils[name] = [[TKSigil alloc] initWithName:name];
    }
    return self.sigils[name];
}
@end

Flyweight shares memory-heavy runes among countless invocations. A master cache of sigils guarantees we do not exhaust our mana pool.

Runic Tags: #objc #flyweight #structural