Last active
December 15, 2015 18:39
-
-
Save b-adams/5305279 to your computer and use it in GitHub Desktop.
Sample Java -> Objective-C translation for CS132 pages 109-118
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Duck* duck = [[MallardDuck alloc] init]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Duck* duck; | |
if(picnic) { | |
duck = [[MallardDuck alloc] init]; | |
} else if(hunting) { | |
duck = [[DecoyDuck alloc] init]; | |
} else if(inBathTub) { | |
duck = [[RubberDuck alloc] init]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(Pizza*) orderPizza | |
{ | |
Pizza* pizza = [[Pizza alloc] init]; | |
[pizza prepare]; | |
[pizza bake]; | |
[pizza cut]; | |
[pizza box]; | |
return pizza; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(Pizza*) orderPizza:(NSString* type) | |
{ | |
Pizza* pizza = [[Pizza alloc] init]; | |
if([type isEqualToString:@"cheese"]) { | |
pizza = [[CheesePizza alloc] init]; | |
} else if([type isEqualToString:@"greek"]) { | |
pizza = [[GreekPizza alloc] init]; | |
} else if([type isEqualToString:@"pepperoni"]) { | |
pizza = [[PepperoniPizza alloc] init]; | |
} | |
[pizza prepare]; | |
[pizza bake]; | |
[pizza cut]; | |
[pizza box]; | |
return pizza; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(Pizza*) orderPizza:(NSString* type) | |
{ | |
Pizza* pizza; | |
if([type isEqualToString:@"cheese"]) { | |
pizza = [[CheesePizza alloc] init]; | |
// } else if([type isEqualToString:@"greek"]) { | |
// pizza = [[GreekPizza alloc] init]; | |
} else if([type isEqualToString:@"pepperoni"]) { | |
pizza = [[PepperoniPizza alloc] init]; | |
} else if([type isEqualToString:@"clam"]) { | |
pizza = [[ClamPizza alloc] init]; | |
} else if([type isEqualToString:@"veggie"]) { | |
pizza = [[VeggiePizza alloc] init]; | |
} | |
[pizza prepare]; | |
[pizza bake]; | |
[pizza cut]; | |
[pizza box]; | |
return pizza; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation SimplePizzaFactory | |
-(Pizza*) createPizza(NSString* type) | |
{ | |
Pizza* pizza = nil; | |
if([type isEqualToString:@"cheese"]) { | |
pizza = [[CheesePizza alloc] init]; | |
} else if([type isEqualToString:@"pepperoni"]) { | |
pizza = [[PepperoniPizza alloc] init]; | |
} else if([type isEqualToString:@"clam"]) { | |
pizza = [[ClamPizza alloc] init]; | |
} else if([type isEqualToString:@"veggie"]) { | |
pizza = [[VeggiePizza alloc] init]; | |
} | |
return pizza; | |
} | |
@end | |
//More idiomatic Objective-C | |
@implementation SimplePizzaFactory | |
-(Pizza*) createPizza(NSString* type) | |
{ | |
NSDictionary* pizzaTypes = @{ @"cheese": [CheesePizza class], | |
@"pepperoni": [PepperoniPizza class], | |
@"clam": [ClamPizza class], | |
@"veggie": [VeggiePizza class] }; | |
Class pizzaClass = [pizzaTypes objectForKey:type]; | |
Pizza* pizza = [[pizzaClass alloc] init]; | |
return pizza; | |
} | |
@end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface PizzaStore : NSObject | |
@property (strong, readwrite) SimplePizzaFactory* factory; | |
@end | |
@implementation PizzaStore | |
-(id)initWithFactory:(SimplePizzaFactory* factory) | |
{ | |
self = [super init]; | |
if(self) | |
{ | |
[self setFactory:factory]; | |
} | |
return self; | |
} | |
-(Pizza*) orderPizza:(NSString* type) | |
{ | |
Pizza* pizza; | |
pizza = [factory createPizza:type]; | |
[pizza prepare]; | |
[pizza bake]; | |
[pizza cut]; | |
[pizza box]; | |
return pizza; | |
} | |
// other methods here | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment