ios - Abstract Factory method pattern -
i have gone through couple of video , blog tuts online. felt understood everything, still struggling implement abstract factory pattern. here requirement:
- i have user class should give user object.
- there 2 types of users in application e.g service provider (provider) , service receiver (consumer).
- there common properties between these 2 types of users name, email id , mobile number etc. provider type there properties.
- provider types of e.g. taxidriver or restaurant etc.
i want implement abstract factory , factory method pattern user class application can decoupled user model , whenever application wants user of type provider or consumer should right object.
what tried 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 { [nsexception raise:nsinternalinconsistencyexception format:@"you have not implemented %@ in %@", nsstringfromselector(_cmd), nsstringfromclass([self class])]; } -(void)retrievuserdata { } @end
now created 2 subclasses consumer , providerfactory abstractuser class.
consumer.h
@interface consumer : abstractuser @property (nonatomic, strong) nsstring *address; @end
providerfactory.h
@interface providerfactory : abstractuser -(id)initwithservicetype:(servicetype)srvtype; @property(nonatomic, strong) nsstring *ownerdetails; @end
so whenever in future if application want support business user taxi , restaurant type have create class , init through providerfactory class.
is approach correct abstract factory pattern? appreciate guidance.
based on follow question, edited answer.
i'm not entirely sure need use abstractfactory
trying accomplish. basics of abstractfactory
allows
provide interface creating families of related or dependent objects without specifying concrete classes (gamma et al.)
take example design patterns: elements of reusable object-oriented software gamma et al. let's creating toolkit build user interfaces document editor. may have bunch of widget objects scrollers, buttons, toolbars, etc. may want later add different look-and-feel document editor. can use abstractfactory
provide interface create of widget products (i.e. createscrollbar
, createbuttons
, etc.) change look-and-feel, subclass abstract class , override methods say, createscrollbar
returns scrollbar has 3-d effect. in case, subclass abstract class create pink scrollbar. options endless, , since client code doesn't care scrollbar looks (all client cares whether scrolls text or not), can add future looks-and-feels without touching client code.
in case, client cares kind of abstractuser
getting because in cases needs customer
, in cases provider
. either way, client code have changed if in future added new kind of user.
that said, think best approach create abstract base class of user , subclass , add user-specific properties subclass. here example of mean.
abstractuser.h
#import <foundation/foundation.h> @interface abstractuser : nsobject @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; @end
consumer.h
#import "abstractuser.h" @interface consumer : abstractuser @property (strong, nonatomic) nsstring *address; @end
viewcontroller.m
#import "viewcontroller.h" #import "consumer.h" #import "provider.h" @interface viewcontroller () @property (strong, nonatomic) consumer *consumer; @property (strong, nonatomic) provider *provider; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; _consumer = [[consumer alloc] init]; _provider = [[provider alloc] init]; self.consumer.name = @"jason"; self.consumer.address = @"some address"; self.provider.name = @"stack overflow"; @end
Comments
Post a Comment