视频教程-极客学院
ps:感觉打的一手好广告啊,因为自己不太爱看视频,但这类又必须看才能明白。粘贴源代码是为了以后查阅方便,也注释了自己的理解。
可以说,工厂生产的物件,是普通人(所要使用的类)无法涉及的,也就是说,工厂类所做的操作都是封装好了的。
同时,不同工厂生产不同的物件,比如手机,螺丝刀,衣服等,各个职能不同。
由于工厂生产不同的物件,可以拥有一个管理工厂类,来管理多个工厂。
DrivceProtocol.h
如果生产是手机设备,那么手机拥有“打电话”,“发短信”才配叫手机
#import <Foundation/Foundation.h>
@protocol DrivceProtocol <NSObject>
@required
-(void)callPhone; //打电话
-(void)sendMessage; //发短信
@end
Drivcefactory.h/m
制造手机设备的工厂,同时引用协议,生产的所有设备都必须遵守“打电话”,“发短信”
#import <Foundation/Foundation.h>
#import "BaseDrivce.h"
#import "DrivceProtocol.h"
typedef enum {
kiphone, //生产苹果手机
Kandroid, //生产安卓手机
} KDrivceType;
@interface Drivcefactory : NSObject
//生产手机的方法
+(BaseDrivce<DrivceProtocol> *)createDrivce:(KDrivceType)type;
@end
#import "Drivcefactory.h"
#import "iphoneDrivce.h"
#import "androidDrivce.h"
@implementation Drivcefactory
+(BaseDrivce<DrivceProtocol> *)createDrivce:(KDrivceType)type{
BaseDrivce *baseDrivce = nil;
if (type== kiphone) {
baseDrivce = [[iphoneDrivce alloc]init]; //实例化苹果设备
}else if (type == Kandroid){
baseDrivce = [[androidDrivce alloc]init]; //实例化安卓设备
}
return baseDrivce;
}
@end
BaseDrivce.h/m
基类设备,用来实现协议的
#import <Foundation/Foundation.h>
#import "DrivceProtocol.h"
@interface BaseDrivce : NSObject <DrivceProtocol>
@end
#import "BaseDrivce.h"
@implementation BaseDrivce
-(void)callPhone{
NSLog(@" call phone");
}
-(void)sendMessage{
NSLog(@"send message");
}
@end
iphoneDrivce.h/m
具体的设备,苹果设备
#import "BaseDrivce.h"
#import "DrivceProtocol.h"
@interface iphoneDrivce : BaseDrivce
-(void)finger; //iphone特有的方法
@end
#import "iphoneDrivce.h"
@implementation iphoneDrivce
-(void)callPhone{
NSLog(@"iphone callPhone");
}
-(void)sendMessage{
NSLog(@"iphone sendMessage");
}
-(void)finger{
NSLog(@"figer");
}
@end
androidDrivce.h/m
具体设备,安装设备
#import "BaseDrivce.h"
#import "DrivceProtocol.h"
@interface androidDrivce : BaseDrivce
-(void)thrme; //安卓设备特有的方法
@end
#import "androidDrivce.h"
@implementation androidDrivce
-(void)callPhone{
NSLog(@"android callphone");
}
-(void)sendMessage{
NSLog(@"android sendmessage");
}
-(void)thrme{
NSLog(@"android thrme");
}
@end
使用
- (void)viewDidLoad {
[super viewDidLoad];
iphoneDrivce *iphone = (iphoneDrivce*)[Drivcefactory createDrivce:kiphone];
[iphone finger];
androidDrivce *android = (androidDrivce*)[Drivcefactory createDrivce:Kandroid];
[android thrme];
}
带有工厂管理类
太多的工厂难免管理不是很方便
BasePhone.h/m
用来做一些协议或基本操作,如上面要实现的协议
#import <Foundation/Foundation.h>
@interface BasePhone : NSObject
@end
#import "BasePhone.h"
@implementation BasePhone
@end
IPhone.h/m
苹果设备,m文件可以来实现特定的方法
#import "BasePhone.h"
@interface Iphone : BasePhone
@end
#import "Iphone.h"
@implementation Iphone
@end
Android.h/m
#import "BasePhone.h"
@interface Android : BasePhone
@end
#import "Android.h"
@implementation Android
@end
BaseFactory.h/m
#import <Foundation/Foundation.h>
#import "BasePhone.h"
//#import "BaseWatch.h"
@interface BaseFactory : NSObject
-(BasePhone *)createPhone;
//-(BaseWatch *)createWatch;
@end
#import "BaseFactory.h"
@implementation BaseFactory
-(BasePhone *)createPhone{
return nil; //不实现由子类实现
}
//-(BaseWatch *)createWatch{
// return nil;
//}
@end
IphoneFactory.h/m
#import "BaseFactory.h"
@interface IphoneFactory : BaseFactory
@end
#import "IphoneFactory.h"
#import "Iphone.h"
//#import "IphoneWatch.h"
@implementation IphoneFactory
-(BasePhone *)createPhone{
return [[Iphone alloc]init];
}
//-(BaseWatch *)createWatch{
// return [[IphoneWatch alloc]init];
//}
@end
AndroidFactory.h/m
#import "BaseFactory.h"
@interface AndroidFactory : BaseFactory
@end
#import "AndroidFactory.h"
#import "Android.h"
//#import "AndroidWatch.h"
@implementation AndroidFactory
-(BasePhone *)createPhone{
return [[Android alloc]init];
}
//-(BaseWatch *)createWatch{
// return [[AndroidWatch alloc]init];
//}
factoryManager.h/m
#import <Foundation/Foundation.h>
#import "BaseFactory.h"
typedef enum{
google,
apple,
} KType;
@interface factoryManager : NSObject
+(BaseFactory *)factoryWithBrand:(KType)type;
@end
#import "factoryManager.h"
#import "BaseFactory.h"
#import "IphoneFactory.h"
#import "AndroidFactory.h"
@implementation factoryManager
+(BaseFactory *)factoryWithBrand:(KType)type{
BaseFactory *baseFactory= nil;
if (type==google) {
baseFactory = [[AndroidFactory alloc]init];
}else if (type==apple){
baseFactory = [[IphoneFactory alloc]init];
}
return baseFactory;
}
@end
使用
- (void)viewDidLoad {
[super viewDidLoad];
BaseFactory *iphoneFactory = [factoryManager factoryWithBrand:apple];
NSLog(@"%@",[iphoneFactory createPhone]);
BaseFactory *androidFactory = [factoryManager factoryWithBrand:google];
NSLog(@"%@",[androidFactory createPhone]);
}
网友评论