标签:str div 打电话 toc float 扩展 integer ret 技术
“专门定义一个类来负责创建其他类的实例,被创建的实例通常具有共同的父类。”
世界上就是由一个工厂类,根据传入的参数,动态地决定创建出哪一个产品类的实例。
简单工厂的生活场景,卖水果的小贩,他给你提供苹果,橘子等水果,小贩就是一个工厂,他为你提供苹果,橘子等水果
#import <Foundation/Foundation.h> #import "DeviceProtocol.h" #import "iPhoneDevice.h" #import "AndroidDevice.h" #import "WindowsDevice.h" typedef enum : NSUInteger { kAndroid, kiPhone, kWindows, } DeviceType; @interface DeviceCreator : NSObject /** * 根据标签创建手机 * * @param deviceType 手机标签 * * @return 对应的手机 */ + (BaseDevice *)deviceCreatorWithDeviceType:(DeviceType)deviceType; @end
#import "DeviceCreator.h" #import "BaseDevice.h" @implementation DeviceCreator + (BaseDevice *)deviceCreatorWithDeviceType:(DeviceType)deviceType { if (deviceType == kiPhone) { return [iPhoneDevice new]; } else if (deviceType == kAndroid) { return [AndroidDevice new]; } else if (deviceType == kWindows) { return [WindowsDevice new]; } else { return [BaseDevice new]; } } @end
#import <Foundation/Foundation.h> @protocol DeviceProtocol <NSObject> /** * 打电话 */ - (void)phoneCall; /** * 系统信息 * * @return 返回系统描述信息 */ - (NSString *)systemInfomation; @end
#import <Foundation/Foundation.h> #import "DeviceProtocol.h" @interface BaseDevice : NSObject <DeviceProtocol> @end
#import "BaseDevice.h" @implementation BaseDevice - (void)phoneCall { NSLog(@"... BaseDevice ..."); } - (NSString *)systemInfomation { return @"BaseDevice"; } @end
#import "BaseDevice.h" @interface iPhoneDevice : BaseDevice @end
#import "iPhoneDevice.h" @implementation iPhoneDevice - (void)phoneCall { NSLog(@"... iPhone ..."); } - (NSString *)systemInfomation { return @"iPhone"; } @end
#import "BaseDevice.h" @interface AndroidDevice : BaseDevice @end
#import "AndroidDevice.h" @implementation AndroidDevice - (void)phoneCall { NSLog(@"... Android ..."); } - (NSString *)systemInfomation { return @"Android"; } @end
#import "BaseDevice.h" @interface WindowsDevice : BaseDevice @end
#import "WindowsDevice.h" @implementation WindowsDevice - (void)phoneCall { NSLog(@"... Windows ..."); } - (NSString *)systemInfomation { return @"Windows"; } @end
- (void)viewDidLoad { [super viewDidLoad]; BaseDevice *iPhone = [DeviceCreator deviceCreatorWithDeviceType:kiPhone]; [iPhone phoneCall]; NSLog(@"%@", [iPhone systemInfomation]); BaseDevice *android = [DeviceCreator deviceCreatorWithDeviceType:kAndroid]; [android phoneCall]; NSLog(@"%@", [android systemInfomation]); BaseDevice *windows = [DeviceCreator deviceCreatorWithDeviceType:kWindows]; [windows phoneCall]; NSLog(@"%@", [windows systemInfomation]); }
2019-09-07 19:52:38.880148+0800 FactoryPattern[17028:6661564] ... IOSDevice ... 2019-09-07 19:52:38.880303+0800 FactoryPattern[17028:6661564] IOSDevice 2019-09-07 19:52:38.880424+0800 FactoryPattern[17028:6661564] ... AndriodDevice ... 2019-09-07 19:52:38.880518+0800 FactoryPattern[17028:6661564] AndriodDevice 2019-09-07 19:52:38.880611+0800 FactoryPattern[17028:6661564] ... WXDevice ... 2019-09-07 19:52:38.880690+0800 FactoryPattern[17028:6661564] WXDevice
从上面的介绍可以看出,简单工厂模式的
五,demo
工厂模式
标签:str div 打电话 toc float 扩展 integer ret 技术
原文地址:https://www.cnblogs.com/lxlx1798/p/11482802.html