samedi 3 janvier 2015

How to avoid typecasting or object class checking in state design pattern?

I am implementing state manager, StateManager has a property currentMode, which can be of type PenState, EraserState, ZoomState (any subclass of State Object).


But in the receiver side while fetching currentMode how to avoid typecasting. what is wrong with the design? what is correct way to do it?



State *state = [StateManager sharedManager].currentMode;
if([state isKindOfClass:[PenState Class]]) //how to avoid this type checking
{
penState *penState = (PenState*)state;
CGSize penSize = penState.size;
}

My classes
——————

@interface PenState : State

@property(nonatomic,strong) NSString *color;
@property(nonatomic,assign) CGSize size;

@end

@interface StateManager : NSObject
{

}
@property(nonatomic,strong)State *currentMode;

@end

Aucun commentaire:

Enregistrer un commentaire