I have gone through couple of video and blog tuts online. Felt like i understood everything, however still struggling to implement the Abstract factory pattern. Here is my requirement:
- I have a User class which should give a user object.
- There are two types of users in my application for e.g Service Provider (Provider) and Service receiver (Consumer).
- There are some common properties between these two types of users like name, email id , mobile number etc. For Provider type there will be some extra properties.
- Provider types could be of for e.g. TaxiDriver or Restaurant etc.
I want to implement Abstract factory and factory method pattern for this user class so that the application can be decoupled from the User model and whenever the application wants an User of type Provider or Consumer it should get the right object.
What I tried so far:
AbstracUserProtocol.h
@protocol AbstractUserProtocol
@required
@property(nonatomic, strong) id delegate;
@property(nonatomic, readonly, getter=isExist) BOOL exist;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *emailID;
@property (nonatomic, assign) NSInteger phoneNumber;
-(void)saveUserData;
-(void)retrievUserData;
@end
AbstractUser.h
@interface AbstractUser : NSObject <AbstractUserProtocol>
-(id)initWithType:(UserType)usrType;
@end
AbstractUser.m
@implementation AbstractUser
@synthesize delegate, exist, name, emailID, phoneNumber;
-(id)initWithType:(UserType)usrType
{
self = nil;
if (usrType == kConsumer) {
self = [Consumer alloc]init];
}
else if (usrType == kProvider) {
self = [ProviderFactory alloc] initWithServiceType:TaxiService];
}
return self;
}
-(void)saveUserData {
}
-(void)retrievUserData {
}
@end
Now created two subclasses Consumer and ProviderFactory from AbstractUser class.
Consumer.h
@interface Consumer : AbstractUser
@end
ProviderFactory.h
@interface ProviderFactory : AbstractUser
-(id)initWithServiceType:(ServiceType)srvType;
@property(nonatomic, strong) NSString *ownerDetails;
@end
So whenever in future if my application want to support another business user like Taxi and Restaurant type then I just have to create a class and init through ProviderFactory class.
Is my approach correct for the abstract Factory pattern? Appreciate any guidance.
Aucun commentaire:
Enregistrer un commentaire