Skip to content

Instantly share code, notes, and snippets.

@jpartogi
Last active March 26, 2017 17:14
Show Gist options
  • Save jpartogi/0d02fd683b06b39568ecdf0c1f90b30a to your computer and use it in GitHub Desktop.
Save jpartogi/0d02fd683b06b39568ecdf0c1f90b30a to your computer and use it in GitHub Desktop.
Bad OO Design in Objective C
#import <Foundation/Foundation.h>
@interface BankAccount : NSObject
@property NSString* accountNumber;
@property double accountBalance;
extern const double interestRate;
-(double) calculateInterest;
-(double) save:(double) amount;
-(double) withdraw:(double) amount;
@end
@implementation BankAccount
const double interestRate = 5.0;
-(double) calculateInterest {
return (interestRate * _accountBalance) / 100;
}
-(double) save:(double) amount {
_accountBalance += amount;
return _accountBalance;
}
-(double) withdraw:(double) amount {
_accountBalance -= amount;
return _accountBalance;
}
@end
@interface InterestCalculator : NSObject
@end
@implementation InterestCalculator
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
BankAccount* account = [BankAccount new];
double balance = [account save:10];
NSLog(@"%.2f", balance);
double interest = [account calculateInterest];
NSLog(@"%.2f", interest);
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Shape : NSObject
extern const int CIRCLE;
extern const int RECTANGLE;
-(double) area;
-(void) createCircle:(double) radius;
-(void) createRectangleWithWidth:(double) width Length:(double) length;
@end
@implementation Shape
const int CIRCLE = 1;
const int RECTANGLE = 2;
int TYPE = 0;
double _radius;
double _width;
double _length;
-(double) area {
switch(TYPE) {
case CIRCLE:
return M_PI * _radius;
case RECTANGLE:
return _width * _length;
default:
return 0;
}
}
-(void) createCircle:(double)radius {
_radius = radius;
TYPE = CIRCLE;
}
-(void) createRectangleWithWidth:(double)width Length:(double)length {
_width = width;
_length = length;
TYPE = RECTANGLE;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Shape* shape = [Shape new];
[shape createRectangleWithWidth:5 Length:4];
double area = [shape area];
NSLog(@"%.2f", area);
}
return 0;
}
#import <Foundation/Foundation.h>
@protocol Shape <NSObject>
-(double) area;
@end
@interface Rectangle : NSObject <Shape>
@property double width;
@property double length;
- (id) initWithWidth:(double) width AndLength:(double) length;
@end
@implementation Rectangle
-(id) initWithWidth:(double)width AndLength:(double)length {
self = [super init];
if( self = [super init] ){
_width = width;
_length = length;
}
return self;
}
-(double) area {
return _width * _length;
}
@end
@interface Square : Rectangle
@property double edge;
-(id) initWithEdge:(double)edge;
@end
@implementation Square
-(id) initWithEdge:(double)edge {
self = [super initWithWidth:edge AndLength:edge];
return self;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Square* square = [[Square alloc] initWithEdge:5];
double area = [square area];
NSLog(@"%.2f", area);
}
return 0;
}
#import <Foundation/Foundation.h>
@protocol Vehicle <NSObject>
-(void) fly;
-(void) drive;
@end
@interface Plane : NSObject <Vehicle>
@end
@implementation Plane
-(void) fly {
NSLog(@"Look ma I am flying");
}
-(void) drive {
[NSException raise:@"Exception" format:@"Uh oh. Better not do that"];
}
@end
@interface Car : NSObject <Vehicle>
@end
@implementation Car
-(void) fly {
[NSException raise:@"Exception" format:@"Can not fly"];
}
-(void) drive {
NSLog(@"Vroom vroom");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car* car = [Car new];
[car drive];
[car fly];
}
return 0;
}
#import <Foundation/Foundation.h>
@protocol Outputter <NSObject>
-(void) print:(NSString*) output;
@end
@interface ConsoleOutputter : NSObject <Outputter>
@end
@implementation ConsoleOutputter
-(void) print:(NSString *)output {
printf("%s", [output UTF8String]);
}
@end
@interface MockOutputter : NSObject <Outputter>
@end
@implementation MockOutputter
-(void) print:(NSString *)output {
NSLog(@"%@", output);
}
@end
@interface CommandInvoker : NSObject
-(void) invoke:(ConsoleOutputter*) outputter;
@end
@implementation CommandInvoker
-(void) invoke:(ConsoleOutputter* )outputter {
[outputter print:@"Hello World"];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
MockOutputter* outputter = [MockOutputter new];
CommandInvoker* invoker = [CommandInvoker new];
[invoker invoke:outputter];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment