/
Facade.m
154 lines (133 loc) · 3.57 KB
/
Facade.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
146
147
148
149
150
151
152
153
154
// Facade.m
// ObjC_Patterns_Kata
//
// Created by Zbigniew Kominek on 12/22/10.
// Copyright 2010. All rights reserved.
#import <Foundation/Foundation.h>
#pragma mark -
#pragma mark Company's Divisions
/*! \brief Subsystem
*/
@interface HumanResourcesDepartment : NSObject
- (void) fireWorkerWithName:(NSString*)name;
@end
@implementation HumanResourcesDepartment
- (void) fireWorkerWithName:(NSString*)name { NSLog(@"Firing employee: %@", name); }
@end
/*! \brief Subsystem
*/
@interface AccountancyDepartment : NSObject
- (void) increaseSalaryForEmployee:(NSString*)name;
@end
@implementation AccountancyDepartment
- (void) increaseSalaryForEmployee:(NSString*)name { NSLog(@"Increasing salary for: %@", name); }
@end
/*! \brief Subsystem
*/
@interface PublicRelationsDepartment : NSObject
- (void) tvCommercial;
- (void) freeBeerForEveryone;
@end
@implementation PublicRelationsDepartment
- (void) tvCommercial { NSLog(@"TV Commerial: Best company in the universe."); }
- (void) freeBeerForEveryone { NSLog(@"Free beer for every employee"); }
@end
#pragma mark -
#pragma mark The Secretary
/*! \brief Facade
*/
@interface Secretary : NSObject
{
HumanResourcesDepartment* hr;
AccountancyDepartment* ac;
PublicRelationsDepartment* pr;
}
- (id) initWithHRDep: (HumanResourcesDepartment*) hrDep
accountancy: (AccountancyDepartment*) acDep
publicRelations: (PublicRelationsDepartment*) prDep;
- (void) fireBestWorker;
- (void) giveBossARise;
- (void) tellEveryoneCompanyIsTheBest;
@end
@implementation Secretary
- (id) initWithHRDep: (HumanResourcesDepartment*) hrDep
accountancy: (AccountancyDepartment*) acDep
publicRelations: (PublicRelationsDepartment*) prDep
{
if ([super init] != nil)
{
hr = [hrDep retain];
ac = [acDep retain];
pr = [prDep retain];
}
return self;
}
- (void) fireBestWorker { [hr fireWorkerWithName:@"Zbigniew Kominek"]; }
- (void) giveBossARise { [ac increaseSalaryForEmployee:@"The Boss"]; }
- (void) tellEveryoneCompanyIsTheBest
{
[pr tvCommercial];
[pr freeBeerForEveryone];
}
- (void) dealloc
{
[hr release];
[ac release];
[pr release];
[super dealloc];
}
@end
#pragma mark -
#pragma mark The Boss
/*! \brief Client
*/
@interface Boss : NSObject
{
Secretary* secretary;
}
- (id) initWithSecretary:(Secretary*)newSecretary;
- (void) doMyJob;
@end
@implementation Boss
- (id) initWithSecretary:(Secretary*)newSecretary
{
if ([super init] != nil)
{
secretary = [newSecretary retain];
}
return self;
}
- (void) doMyJob
{
[secretary giveBossARise];
[secretary fireBestWorker];
[secretary tellEveryoneCompanyIsTheBest];
}
- (void) dealloc
{
[secretary release];
[super dealloc];
}
@end
#pragma mark -
#pragma mark Helpful function
Secretary* buildTheCompany()
{
HumanResourcesDepartment* hrDep = [[[HumanResourcesDepartment alloc] init] autorelease];
AccountancyDepartment* acDep = [[[AccountancyDepartment alloc] init] autorelease];
PublicRelationsDepartment* prDep = [[[PublicRelationsDepartment alloc] init] autorelease];
Secretary* secretary = [[Secretary alloc] initWithHRDep:hrDep accountancy:acDep publicRelations:prDep];
return secretary;
}
#pragma mark -
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Secretary* secretary = buildTheCompany();
Boss* boss = [[Boss alloc] initWithSecretary:secretary];
[boss doMyJob];
[secretary release];
[boss release];
[pool drain];
return 0;
}