iOS design mode-Simple factory
Schematic diagram
Description
1. The factory class and its instantiated subclasses are not abstract classes, so it is called a simple factory (the manufacturer and the product are concrete implementation classes)
2. Simple factory flexibility is poor, but its simple and straightforward features, suitable for simple hierarchical structure of the scene
Source
Https://github.com/YouXianMing/FactoryPattern
////DeviceCreator.h//Factorypattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import "DeviceProtocol.h"#import "iPhoneDevice.h"#import "AndroidDevice.h"#import "WindowsDevice.h"typedefenum: Nsuinteger {kandroid, Kiphone, Kwindows,} DeviceType;@interfaceDevicecreator:nsobject/** * Create phone according to tag * * @param devicetype phone tag * * @return corresponding phone*/+ (Basedevice *) Devicecreatorwithdevicetype: (DeviceType) DeviceType;@end
////DEVICECREATOR.M//Factorypattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import "DeviceCreator.h"#import "BaseDevice.h"@implementationDevicecreator+ (Basedevice *) Devicecreatorwithdevicetype: (DeviceType) DeviceType {if(DeviceType = =Kiphone) { return[IphonedeviceNew]; } Else if(DeviceType = =kandroid) { return[AndroiddeviceNew]; } Else if(DeviceType = =kwindows) { return[WindowsdeviceNew]; } Else { return[BasedeviceNew]; }}@end
// // BaseDevice.h// factorypattern//// Created by Youxianming on 15/7/29. // Copyright (c) 2015 youxianming. All rights reserved. // #import <Foundation/Foundation.h>#import"DeviceProtocol.h " @interface Basedevice:nsobject <DeviceProtocol>@end
////BASEDEVICE.M//Factorypattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import "BaseDevice.h"@implementationBasedevice- (void) PhoneCall {NSLog (@"... Basedevice ...");}-(NSString *) systeminfomation {return @"Basedevice";}@end
Analysis
Specific producers and specific subclasses
Relationship Ethics Chart
iOS design mode-Simple factory