設計模式系列文章
《iOS設計模式(2)工廠模式》
《iOS設計模式(3)適配器模式》
《iOS設計模式(4)抽象工廠模式》
《iOS設計模式(5)策略模式》
《iOS設計模式(6)模板模式》
《iOS設計模式(7)建造者模式》
1.概念描述
“簡單工廠模式是屬于創建型模式,又叫做靜態工廠方法(Static Factory Method)模式,但不屬于23種GOF設計模式之一。簡單工廠模式是由一個工廠對象決定創建出哪一種產品類的實例。簡單工廠模式是工廠模式家族中最簡單實用的模式,可以理解為是不同工廠模式的一個特殊實現?!薄俣劝倏?/p>
2.場景舉例
舉個例子,小猿最近買了一輛車,新車到手要做的第一件事就是去車管所上牌照。車管所大廳里,人滿為患,大家先是根據自己的車輛提交相關的資料,然后車管所依次安排大家進行相關的搖號,拿到搖的號后交費就可以去外面的窗口等待拿車牌了。
車牌號都是一批一批的出的,大家的牌子都不一樣,有的藍牌,有的黃牌,大街上還見過軍牌和警牌的。那這個砸牌子的地方其實就是一個“牌照工廠”。
這個模式本身很簡單而且使用在業務較簡單的情況下。一般用于小項目或者具體產品很少擴展的情況(這樣工廠類才不用經常更改)。
它由三種角色組成:
工廠類角色:這是本模式的核心,含有一定的商業邏輯和判斷邏輯,根據邏輯不同,產生具體的工廠產品。如例子中的LHCarLicenseFactory類。
抽象產品角色:它一般是具體產品繼承的父類或者實現的接口。由接口或者抽象類來實現。如例中的LHCarLicense基類。
具體產品角色:工廠類所創建的對象就是此角色的實例。在oc中由一個具體類實現,如例子中的LHBlueCarLicense、LHYellowCarLicense類。引用
3代碼實現
實現車牌基類
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol LHCarLicenseProtocol <NSObject>
// 打印牌照
- (NSString *)printLicenseNumber;
@end
@interface LHCarLicense : NSObject<LHCarLicenseProtocol>
@property(nonatomic, copy)NSString *city; // 城市
@property(nonatomic, copy, readonly)NSString *licenseNumber; // 車牌號
@end
#import "LHCarLicense.h"
@implementation LHCarLicense
#pragma mark -
#pragma mark Public Method
- (NSString *)printLicenseNumber{
_licenseNumber = [self getLicenseNumber];
return _licenseNumber;
}
#pragma mark -
#pragma mark Private Method
// 獲取牌照
- (NSString *)getLicenseNumber
{
NSString *firstChar = [self getRandomChar];
NSString *lastNumber = @"";
for (NSInteger index = 0; index < 5; index++) {
NSInteger random = [self getRandomNumber:0 to:1];
NSString *newNumber = random == 0 ? [self getRandomChar] : [NSString stringWithFormat:@"%ld",(long)[self getRandomNumber:0 to:9]];
lastNumber = [NSString stringWithFormat:@"%@%@",lastNumber,newNumber];
}
return [NSString stringWithFormat:@"%@%@·%@",_city,firstChar,lastNumber];
}
// 獲取牌照號
- (NSString *)getRandomChar
{
int NUMBER_OF_CHARS = 1;
char data[NUMBER_OF_CHARS];
data[0] = (char)('A' + (arc4random_uniform(26)));
return [[NSString alloc] initWithBytes:data length:NUMBER_OF_CHARS encoding:NSUTF8StringEncoding];
}
//獲取一個隨機整數,范圍在[from,to),包括from,不包括to
-(NSInteger)getRandomNumber:(NSInteger)from to:(NSInteger)to
{
return (NSInteger)(from + (arc4random() % (to - from + 1)));
}
然后派生出兩個子類,藍色車牌類和黃色車牌類
#import "LHCarLicense.h"
@interface LHBlueCarLicense : LHCarLicense
@end
#import "LHBlueCarLicense.h"
@implementation LHBlueCarLicense
// 打印牌照號
- (NSString *)printLicenseNumber{
[super printLicenseNumber];
return [NSString stringWithFormat:@"藍色牌照: %@",self.licenseNumber];
}
@end
#import "LHCarLicense.h"
@interface LHYellowCarLicense : LHCarLicense
@end
#import "LHYellowCarLicense.h"
@implementation LHYellowCarLicense
// 打印牌照號
- (NSString *)printLicenseNumber{
[super printLicenseNumber];
return [NSString stringWithFormat:@"黃色牌照: %@",self.licenseNumber];
}
@end
關鍵的是創建工廠類
#import <Foundation/Foundation.h>
@class LHCarLicense;
typedef enum : NSUInteger {
ELicenseType_Blue,
ELicenseType_Yellow
} ELicenseType;
@interface LHCarLicenseFactory : NSObject
/**
* 獲取牌照工廠
*
* @param type 牌照類型
*
* @return 返回牌照對象
*/
+ (LHCarLicense *)createCarLicenseWithType:(ELicenseType)type;
@end
#import "LHCarLicenseFactory.h"
#import "LHCarLicense.h"
#import "LHBlueCarLicense.h"
#import "LHYellowCarLicense.h"
@implementation LHCarLicenseFactory
+ (LHCarLicense *)createCarLicenseWithType:(ELicenseType)type{
LHCarLicense *_license;
switch (type) {
case ELicenseType_Blue:
_license = [[LHBlueCarLicense alloc] init];
break;
case ELicenseType_Yellow:
_license = [[LHYellowCarLicense alloc] init];
break;
}
return _license;
}
@end
客戶端調用
#import "ViewController.h"
#import "LHCarLicenseFactory.h"
#import "LHCarLicense.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)createLicense:(ELicenseType)type
{
LHCarLicense *_license = [LHCarLicenseFactory createCarLicenseWithType:type];
_license.city = _txtCity.text ? _txtCity.text : @"京";
_lbLicenseNumber.text = [_license printLicenseNumber];
}
#pragma mark -
#pragma mark Button Event
// 生成藍色牌照
- (IBAction)btnBlueEvent:(UIButton *)sender {
[self createLicense:ELicenseType_Blue];
}
// 生成黃色牌照
- (IBAction)btnYellowEvent:(UIButton *)sender {
[self createLicense:ELicenseType_Yellow];
}
@end
這樣工作人員就可以批量生成不同類型的車牌號了,輸出結果如下:
優缺點
- 優點:客戶端調用簡單明了,不需要關注太多的邏輯。
- 缺點:也正是所謂的優點,導致了工廠類中揉雜了太多的業務邏輯,產品類本身是符合開閉原則的,對擴展開放對修改關閉,但是工廠類卻違反了開閉原則,因為每增加一個產品,工廠類都需要進行邏輯修改和判斷,導致耦合度太高
以上是個人的理解,有不同意見歡迎指正留言。