/
Prototype.m
145 lines (129 loc) · 3.34 KB
/
Prototype.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Prototype.m
// ObjC_Patterns_Kata
//
// Created by Zbigniew Kominek on 12/10/10.
// Copyright 2010. All rights reserved.
#import <Foundation/Foundation.h>
#pragma mark -
#pragma mark Warriors
/*! \brief Prototype
*/
@interface Warrior : NSObject <NSCopying>
- (void) presentMyself;
@end
@implementation Warrior
- (void) presentMyself
{
NSLog(@"Just a warrior");
}
- (id)copyWithZone:(NSZone *)zone
{
Warrior* warrior = [[Warrior allocWithZone:zone] init];
return warrior;
}
@end
/*! \brief Concrete Prototype
*/
@interface StarWarsWarrior : Warrior
{
NSString* type;
NSString* side;
NSString* weapon;
}
@property (nonatomic, retain) NSString* type;
@property (nonatomic, retain) NSString* side;
@property (nonatomic, retain) NSString* weapon;
- (id) initWithType:(NSString*)hisType side:(NSString*)fightingSide weapon:(NSString*)fightingWeapon;
@end
@implementation StarWarsWarrior
@synthesize type, side, weapon;
- (id) initWithType:(NSString*)hisType side:(NSString*)fightingSide weapon:(NSString*)fightingWeapon
{
if ([super init] != nil)
{
self.type = hisType;
self.side = fightingSide;
self.weapon = fightingWeapon;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
StarWarsWarrior* warrior = [[StarWarsWarrior allocWithZone:zone] initWithType:self.type side:self.side weapon:self.weapon];
return warrior;
}
- (void) presentMyself
{
NSLog(@"I'm %@, fighting on %@ and I'll kill you with my %@.", self.type, self.side, self.weapon);
}
- (void) dealloc
{
[type release];
[side release];
[weapon release];
[super dealloc];
}
@end
/*! \brief Concrete Prototype
*/
@interface ShaolinMonk : Warrior
{
NSString* name;
NSString* style;
}
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* style;
- (id) initWithName:(NSString*)realName style:(NSString*)kungFuStyle;
@end
@implementation ShaolinMonk
@synthesize name, style;
- (id) initWithName:(NSString*)realName style:(NSString*)kungFuStyle
{
if ([super init] != nil)
{
self.name = realName;
self.style = kungFuStyle;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
ShaolinMonk* warrior = [[ShaolinMonk allocWithZone:zone] initWithName:self.name style:self.style];
return warrior;
}
- (void) presentMyself
{
NSLog(@"My name is %@. I know %@ kung-fu style, be aware.", self.name, self.style);
}
- (void) dealloc
{
[name release];
[style release];
[super dealloc];
}
@end
#pragma mark -
#pragma mark Helpful method
/*! \brier Client
* \param Uses this prototype to create new warrior.
*/
void cloneWarrior(Warrior* warriorPrototype)
{
Warrior* clonedWarrior = [warriorPrototype copy];
[clonedWarrior presentMyself];
[clonedWarrior release];
}
#pragma mark -
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// creating prototypes, Abstract Factory or Factory Method could be used
StarWarsWarrior* sithLord = [[StarWarsWarrior alloc] initWithType:@"Sith Lord" side:@"the Dark Side" weapon:@"Red Lightsaber"];
ShaolinMonk* bruceLee = [[ShaolinMonk alloc] initWithName:@"Bruce Lee" style:@"Jeet Kune Do"];
cloneWarrior(sithLord);
cloneWarrior(bruceLee);
[bruceLee release];
[sithLord release];
[pool drain];
return 0;
}