設計模式系列文章
《iOS設計模式(1)簡單工廠模式》
《iOS設計模式(2)工廠模式》
《iOS設計模式(3)適配器模式》
《iOS設計模式(5)策略模式》
《iOS設計模式(6)模板模式》
《iOS設計模式(7)建造者模式》
1.概念描述
今天我們學習一下抽象工廠模式,照例先把百度百科上的正規解釋給大家展示出來看看。
抽象工廠模式是所有形態的工廠模式中最為抽象和最具一般性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式可以向客戶端提供一個接口,使客戶端在不必指定產品的具體的情況下,創建多個產品族中的產品對象。根據里氏替換原則,任何接受父類型的地方,都應當能夠接受子類型。因此,實際上系統所需要的,僅僅是類型與這些抽象產品角色相同的一些實例,而不是這些抽象產品的實例。換言之,也就是這些抽象產品的具體子類的實例。工廠類負責創建抽象產品的具體子類的實例。---百度百科
在上面抽象工廠模式的定義中涉及到了一個名詞:產品族。他的意思是有多類產品,每一類產品中又分多種相似的產品。下面是百度百科的解釋:
是指位于不同產品等級結構中,功能相關聯的產品組成的家族。一般是位于不同的等級結構中的相同位置上。顯然,每一個產品族中含有產品的數目,與產品等級結構的數目是相等的,形成一個二維的坐標系,水平坐標是產品等級結構,縱坐標是產品族。叫做相圖。---百度百科
2.實例場景
看過《iOS設計模式(1)簡單工廠模式》和《iOS設計模式(2)工廠模式》的同學知道在前兩篇文章中我們舉了一個汽車牌照的例子,本文繼續承接上兩篇文章中的例子。
在現實生活中的汽車牌照沒有咱們例子中那么簡單。除了國家規定不同類型的車輛所上的牌照不同意外,不同省份的牌照生成規則也不一樣,最明顯的就是牌照號中那唯一的漢字,比如北京市的牌照就是“京X·XXXXX”,而河北省的牌照就是“冀X·XXXXX”,當然同一省份不同地區的牌照在漢字后面的那個字母也不一樣。
今天咱們只看不同省份牌照不一樣的需求怎么解決。這就需要我們在之前定義的藍色車牌和黃色車牌(LHBlueCarLicense和LHYellowCarLicense)中再派生出不同地區的藍色車牌和黃色車牌類。這里我們就舉北京和河北兩個例子:LHBeijingBlueCarLicense、LHBeijingYellowCarLicense和LHHebeiBlueCarLicense、LHHebeiYellowCarLicense。
3.代碼實現
兩個車牌基類:藍色車牌和黃色車牌
#import "LHCarLicenseProtocol.h"
@interface LHBlueCarLicense : NSObject<LHCarLicenseProtocol>
@property(nonatomic, copy)NSString *city; // 城市
@end
#import "LHBlueCarLicense.h"
@implementation LHBlueCarLicense
// 打印車牌號
- (NSString *)printLicenseNumber{
return nil;
}
@end
#import "LHCarLicenseProtocol.h"
@interface LHYellowCarLicense : NSObject<LHCarLicenseProtocol>
@property(nonatomic, copy)NSString *city; // 城市
@end
#import "LHYellowCarLicense.h"
@implementation LHYellowCarLicense
// 打印牌照號
- (NSString *)printLicenseNumber{
return nil;
}
@end
下面是北京藍色車牌和黃色車牌類實現(河北車牌代碼一樣,這里就不再貼代碼了)
#import "LHBlueCarLicense.h"
@interface LHBeijingBlueCarLicense : LHBlueCarLicense
@property(nonatomic, copy, readonly)NSString *licenseNumber; // 車牌號
@end
#import "LHBeijingBlueCarLicense.h"
#import "LHCommonClass.h"
@implementation LHBeijingBlueCarLicense
// 打印牌照號
- (NSString *)printLicenseNumber{
self.city = @"京";
_licenseNumber = [LHCommonClass getLicenseNumberWithCity:self.city];
return [NSString stringWithFormat:@"北京藍色車牌號:%@",_licenseNumber];
}
@end
#import "LHYellowCarLicense.h"
@interface LHBeijingYellowCarLicense : LHYellowCarLicense
@property(nonatomic, copy, readonly)NSString *licenseNumber; // 車牌號
@end
#import "LHBeijingYellowCarLicense.h"
#import "LHCommonClass.h"
@implementation LHBeijingYellowCarLicense
// 打印牌照號
- (NSString *)printLicenseNumber{
self.city = @"京";
_licenseNumber = [LHCommonClass getLicenseNumberWithCity:self.city];
return [NSString stringWithFormat:@"北京黃色車牌號:%@",_licenseNumber];
}
@end
產品族類定義完成后,下面我們看工廠類的實現。之前我們的工廠類定義的是藍色車牌工廠類和黃色車牌工廠類,在抽象工廠模式中我們要定義的是北京車牌工廠類和河北車牌工廠類。我們先看工廠基類的定義
#import <Foundation/Foundation.h>
@class LHBlueCarLicense;
@class LHYellowCarLicense;
@interface LHCarLicenseFactory : NSObject
/**
* 獲取藍色牌照工廠
*
* @return 返回牌照對象
*/
+ (LHBlueCarLicense *)createBlueCarLicense;
/**
* 獲取黃色牌照工廠
*
* @return 返回牌照對象
*/
+ (LHYellowCarLicense *)createYellowCarLicense;
@end
#import "LHCarLicenseFactory.h"
#import "LHBlueCarLicense.h"
#import "LHYellowCarLicense.h"
@implementation LHCarLicenseFactory
// 獲取藍色牌照工廠
+ (LHBlueCarLicense *)createBlueCarLicense
{
return nil;
}
// 獲取黃色牌照工廠
+ (LHYellowCarLicense *)createYellowCarLicense
{
return nil;
}
@end
下面看北京工廠類的實現(河北工廠類代碼實現一樣,不在這里貼了)
#import "LHCarLicenseFactory.h"
@interface LHBeijingCarLicenseFactory : LHCarLicenseFactory
@end
#import "LHBeijingCarLicenseFactory.h"
#import "LHBlueCarLicense.h"
#import "LHYellowCarLicense.h"
#import "LHBeijingBlueCarLicense.h"
#import "LHBeijingYellowCarLicense.h"
@implementation LHBeijingCarLicenseFactory
// 北京藍色牌照
+ (LHBlueCarLicense *)createBlueCarLicense
{
LHBlueCarLicense *_license = [[LHBeijingBlueCarLicense alloc] init];
return _license;
}
// 北京黃色牌照
+ (LHYellowCarLicense *)createYellowCarLicense
{
LHYellowCarLicense *_license = [[LHBeijingYellowCarLicense alloc] init];
return _license;
}
@end
那么客戶端的調用就簡單多了
// 北京藍色牌照點擊事件
- (IBAction)btnBeijingBlueEvent:(UIButton *)sender {
LHBlueCarLicense *_license = [LHBeijingCarLicenseFactory createBlueCarLicense];
_lbText.text = [NSString stringWithFormat:@"%@\n%@",_lbText.text,_license.printLicenseNumber];
}
// 北京黃色牌照點擊事件
- (IBAction)btnBeijingYellowEvent:(UIButton *)sender {
LHYellowCarLicense *_license = [LHBeijingCarLicenseFactory createYellowCarLicense];
_lbText.text = [NSString stringWithFormat:@"%@\n%@",_lbText.text,_license.printLicenseNumber];
}
輸出結果:
這樣,抽象工廠模式代碼實現就完成了。不過個人覺得抽象模式還是比較復雜的,而且擴展擴展起來也不是很靈活,如果產品族非常多的話會生成很多產品類出來,是一件恐怖的事情。所以有人結合簡單工廠模式和工廠模式,利用反射(java)機制對抽象工廠模式進行優化,避免生成太多的產品類。感興趣的同學可以看看這篇文章
引用
配圖引用