We put the instance variable initialize routine to getter not constructor, that’s pretty common in WEB and iOS, we call that Lazy instantiation, and waiting until the last second to instantiate it when you really need it.

for example in object-c:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *operandStack;
@end
@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
- (NSMutableArray *)operandStack
{
if (_operandStack == nil) _operandStack = [[NSMutableArray alloc] init];
return _operandStack;
}
- (void)setOperandStack:(NSMutableArray *)operandStack
{
_operandStack = operandStack;
}