The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.
//
// main.m
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MobileClass.h"
#import "FactoryClass.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Factory*foxconn=[Factory new];
Mobile*mySamsung=[foxconn Produce_Phone:@"Samsung"];
[mySamsung Show];
}
return 0;
}
//
// mobile.m
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MobileClass.h"
@implementation Mobile
@synthesize Inch;
@synthesize Weight;
@synthesize Cost;
@synthesize Performance;
@synthesize Brand;
-(void)Call{
printf("Mobile Phone is calling!...\n");
return;
}
-(void)Gprs{
printf("Mobile Phone is connecting GPRS!...\n");
return;
}
-(void)Wifi{
printf("Mobile Phone is connecting WIFI!...\n");
return;
}
-(void)Show{
printf("Mobile Phone's brand is ");NSLog(@"%@\n",Brand);
return;
}
@end
//
// MobileClassH.h
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#ifndef app1_27_MobileClassH_h
#define app1_27_MobileClassH_h
#endif
@interface Mobile : NSObject
@property float Inch;
@property float Weight;
@property int Cost;
@property int Performance;
@property NSString*Brand;
-(void)Call;
-(void)Gprs;
-(void)Wifi;
-(void)Show;
@end
//
// SanxingClass.m
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SamsungClass.h"
@implementation Samsung
@synthesize Color;
-(void)Wifi{
printf("Samsung has lower Wifi signal!\n");
return;
}
-(void)Show{
printf("hahahaha I have a Samsung and it's perfect!\nOtherwise,the color is \n");
NSLog(@"%@",Color);
return;
}
@end
//
// SanxingClassH.h
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#ifndef app1_27_SanxingClassH_h
#define app1_27_SanxingClassH_h
#endif
#import "MobileClass.h"
@interface Samsung : Mobile
@property NSString*Color;//black white pink yellow
-(void)Wifi;
-(void)Show;
@end
//
// FactoryClass.m
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "FactoryClass.h"
@implementation Factory
@synthesize Name;
-(Mobile*)Produce_Phone:(NSString*)Brand{
if([Brand isEqualToString:@"Samsung"]){
Samsung*this=[Samsung new];
this.Color=@"white";
return this;
}else if([Brand isEqualToString:@"Mi"]){
Mi*this=[Mi new];
return this;
}return nil;
}
@end
//
// FactoryClassH.h
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#ifndef app1_27_FactoryClassH_h
#define app1_27_FactoryClassH_h
#endif
#import "MobileClass.h"
#import "SamsungClass.h"
#import "MiClass.h"
@interface Factory : NSObject
@property NSString*Name;
-(Mobile*)Produce_Phone:(NSString*)Brand;
@end
//
// MiClass.m
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MiClass.h"
@implementation Mi
-(void)Show{
printf("It's Mi's round,hahahaha,it can break bricks!\n");
return;
}
@end
//
// MiClassH.h
// app1.27
//
// Created by qhfz on 16/1/27.
// Copyright (c) 2016年 ssdfz. All rights reserved.
//
#ifndef app1_27_MiClassH_h
#define app1_27_MiClassH_h
#endif
#import "MobileClass.h"
@interface Mi : Mobile
-(void)Show;
@end