Generally, you shouldn't be accessing properties on Dog directly from your strategy object. Instead, what you can do is provide a move method that returns a new position based on the old position. So, for example, if you have:
@interface Dog : NSObject {
NSInteger position;
DogStrategy * strategy;
}
@property(nonatomic, assign) NSInteger position;
@property(nonatomic, retain) DogStrategy * strategy;
- (void)updatePosition;
@end
@implementation Dog
@synthesize position, strategy;
- (void)updatePosition {
self.position = [self.strategy getNewPositionFromPosition:self.position];
}
@end
@interface DogStrategy : NSObject { }
- (NSInteger)getNewPositionFromPosition:(NSInteger)pos;
@end
// some parts elided for brevity
@interface NormalDogStrategy : DogStrategy { }
@end
@implementation NormalDogStrategy
- (NSInteger)getNewPositionFromPosition:(NSInteger)pos {
return pos + 2;
}
@end
Then, when you instantiate a Dog, you can assign it the NormalDogStrategy and call [dog updatePosition]
- the Dog will ask its strategy for its updated position, and assign that to its instance variable itself. You've avoided exposing the internals of Dog to your DogStrategy and still accomplished what you intended.