I am developing an iOS application, in which I have to draw some patterns on a view based on option selected by user. Let me explain you more clearly.
User will be shown number of images as options to choose from.
On selecting an option, a view will be drawn. This view is inheriting from a class say ParentClass
where I have set up common properties for child classes.
ParentClass
@interface ParentClass : UIView
-(ImagePlaceHolder *) imageHolderTouched:(CGPoint) position;
//common properties declared
@end
Right now, my approach is use of separate classes for each pattern. I override drawRect:
for different patterns. So that's why I am creating separate child classes for each.
ChildDesign
@interface ChildDesign : ParentClass
@end
@implementation CollageDesign
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self drawViews];
}
return self;
}
-(void) drawRect:(CGRect)rect
{
//different implementation for different classes
}
@end
There are many similar child classes just having different drawRect
implementation. In main controller, I initialize pattern child class based on selection using switch case.
ParentClass collView;
switch (selectedOptionIndex) {
case 1:
collView = [[ChildDesign alloc] initWithFrame:newframe];
break;
case 2:
collView = [[ChildDesign2 alloc] initWithFrame:newframe]; //similar other classes
break;
case 3:
collView = [[ChildDesign3 alloc] initWithFrame:newframe];
break;
}
Problem is that I have got alot of options, say 50 - 100. Creating separate inherited classes (50 - 100 .h and .m files) would be really bad approach in my opinion and is bad programming practice.
So What pattern should I follow here to optimize my code? How should I create my child classes/ patterns? What will be best approach? An idea in my mind is to pass selected index to I hope my Question is clear.. Thanks.
An idea in my mind is to create single child pattern class having selected index as a property. Based on selected index, I will implement drawRect:
method. But there will be lot of if-else or switch cases.