Member
Join Date: May 2009
|
I decided to try to (vaguely) implement (the Pizza stuff) what I was reading in the following link to see if I could manage it yet. Yes It works!!! - but I think I have missed the point somewhere along the line, if I understand what ive been reading properly, my Factory method isn't as clever as it should be.
http://en.wikipedia.org/wiki/Factory_method_pattern I have managed to create a Singleton Factory Class that creates 2 different types of pizzas subclassed from a PizzaBase class that holds their price, but I am using the following method in the factory. Code:
-(id)createPizza:(NSString *)typeOfPizza
{
if (typeOfPizza==@"Cheese") {
return [[CheesePizza alloc] init];
}
if (typeOfPizza==@"Ham") {
return [[HamPizza alloc] init];
}
return nil;
} I am calling this method like this. ('first' is the pointer to my factory). I will try next to put this into a loop with an array holding the types to make.Code:
PizzaBase *newPizza = [first createPizza:@"Cheese"];
NSLog(@"RETURN VALUE = %@",[newPizza showPrice]);
PizzaBase *newPizza2 = [first createPizza:@"Ham"];
NSLog(@"RETURN VALUE = %@",[newPizza2 showPrice]); While it works --- Im not sure, but I dont think I should be doing it like that with the NSStrings for the parameters - If I understand correctly, shouldn't I be passing in a pointer id or something, so that the Factory implicitly knows what kind of pizza to make? I believe I should be able to eliminate the 'if' statements if I do it correctly, I have tried but I cant get it to work any other way at the moment.Last edited by !Marc! : 2010-03-21 at 16:38. |
quote |
Member
Join Date: May 2009
|
ok, I have changed my crude single calls into an array like this : but it still works on the principle of passing a string into the factory as above
Code:
#import <Foundation/Foundation.h>
#import "Singleton.h";
#import "PizzaBase.h";
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MRSingleton *first = [MRSingleton createSingleton];
NSArray *types = [[NSArray alloc] initWithObjects:@"Cheese",@"Ham",@"Cheese",nil];
NSMutableArray *pizzaArray = [[NSMutableArray alloc] init];
int countTypes = [types count];
for (int i =0 ; i<countTypes ; i++)
{
[pizzaArray addObject:[first createPizza:[types objectAtIndex:i]]];
NSLog(@"I cost %.2f", [[pizzaArray objectAtIndex:i] price]);
}
[pool drain];
return 0;
} Its all experimental stuff at the moment, I was just wondering if as I said above, the createPizza method is the orthodox way of using a factory class.???Last edited by !Marc! : 2010-03-21 at 15:12. |
quote |
25 chars of wasted space.
|
I don't know what you are doing with your singleton object but it seems like it isn't necessarily a singleton that you want, but a convenience method on a Pizza object. Also, perhaps you should be doing that, but it seems silly to have a new class for every possible type of pizza when it would make more sense to have a Pizza object with a toppings member variable. Think how nice it would be able to go…
Quote:
HamPineappleCheesePizza, HamSausagePepperoniCheesePizza, etc. Edit: I'm just learning Cocoa/Obj-C too, so I am certainly not saying I am the person who should be answering this. Anyone with a better knowledge please feel free to correct me Edit2: You may also want to look into NSClassFromString() if you plan on implementing the factory pattern. You should check out the book "Cocoa Design Patterns" to help give you an idea of not just how you implement some of the design patterns in Cocoa/Obj-C, but also to give you an idea of any others that you don't know about. Last edited by ast3r3x : 2010-03-21 at 19:32. |
|
quote |
‽
|
You may also want to look into class clusters.
|
quote |
Member
Join Date: May 2009
|
From the link at wikipedia - essentially I guess what I am asking is how would I say this in Ob-C
Code:
if (class_exists($targetClass) && is_subclass_of($targetClass, $baseClass))
return new $targetClass; From what I understand, from reading the books and on the net, Ob-C has a dynamic runtime that allows flexibility a bit like php. In the php statement above, clearly a new kind of object is being created from the variable $targetClass. $targetClass isn't the name of the class at all, its just a layer of indirection. I am just wondering how to make that statement in ob-c so that I dont have to ask a load of dumb 'if' statements.From what I understand, a factory class should be a singleton, because it makes no sense to have multiple factories producing the same objects. Also, please lets not get hung up on the pizza element of the task and whether its best to make a pizza this way or that way - all I am interested in regarding this is having a factory class create objects that are variations of a common base class. Could be pizzas, could be html tables, could be graphics objects. - I chose pizza because that was what I was reading at the time. |
quote |
‽
|
Haven't tried this, but:
Code:
if (NSClassFromString(targetClass) != nil /* returns nil if no class by that name exists */
&& [NSClassFromString(targetClass) isSubclassOfClass:NSClassFromString(baseClass)])
return [[NSClassFromString(targetClass) alloc] init]; |
quote |
‽
|
Quote:
Again, I recommend reading up on class clusters. |
|
quote |
Posting Rules | Navigation |
|
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Sorting a Javascript object of objects by a value | noleli2 | Programmer's Nook | 4 | 2009-05-15 13:59 |
MS Word & Acrobat Document Objects | drewprops | Genius Bar | 0 | 2007-06-22 00:37 |
China iPod factory work conditions | Doxxic | General Discussion | 1 | 2006-06-15 16:32 |
The end of the World is nigh. Tie down your metal objects...... | scratt | General Discussion | 22 | 2006-01-06 21:03 |