#工廠模式(常常使用) 工廠模式是常見的設計模式之一,這種模式的設計模式屬於建立型模式,它提供了一種建立對象的方式。設計模式
什麼時候使用:咱們明確地計劃不一樣條件下建立不一樣實例時(筆者認爲3種類型爲一個臨界點)bash
優勢:ide
缺點: 每增長一個產品,都要增長一個產品類,使得工程中類的個數成倍增長,同時也使文件結構變複雜了ui
#抽象工廠模式(筆者通常不多用到了) 抽象工程模式圍繞一個超級大廠建立其餘小廠,這種模式的設計模式屬於建立型模式,它提供了一種建立對象的方式。spa
注:「超級大廠建立其餘小廠」(能夠這麼理解,好比手機電子廠包括:主板,電池,屏幕,等產品線,電子廠就是大廠,主板,電池,屏幕就是一個部分(你也能夠理解爲一個小廠))設計
什麼時候使用:系統產品族裏面有多於一個的產品族,而系統某時候只須要某一種產品code
優勢:同上對象
缺點:同上string
#抽象工廠模式和工廠模式區別:產品
因此,抽象工廠就像工廠,而工廠方法就像工廠中的某一條產品線。
#工廠模式
好比說一個視圖裏面要建立多種不一樣的UICollectionViewCell的狀況時
----------------SJJFactoryPatternCellFactory 工廠類----------------
@class SJJFactoryPatternCell;
typedef NS_ENUM (NSInteger, SJJFactoryPatternCellType) {
SJJFactoryPatternCellTypeCell,
SJJFactoryPatternCellTypeCircleCell,
SJJFactoryPatternCellTypeSpuareCell,
SJJFactoryPatternCellTypeRectangleCell,
};
NS_ASSUME_NONNULL_BEGIN
@interface SJJFactoryPatternCellFactory : NSObject
- (SJJFactoryPatternCell *)cellInCollection:(UICollectionView *)collectionView
forIndexPath:(NSIndexPath *)indexPath
forMineMode:(SJJFactoryPatternCellType)type;
@end
複製代碼
#import "SJJFactoryPatternCellFactory.h"
#import <UIKit/UIKit.h>
static NSString *const kMenuCellIdentifier = @"menuCell";
static NSString *const kSubjectCellIdentifier = @"mesubjectCell";
static NSString *const kTopViewIdentifier = @"meTopViewIdentifier";
static NSString *const kSectionHeaderIdentifier = @"meSectionHeader";
static NSString *const kSettingIdentifier = @"kSettingIdentifier";
static NSString *const kCustomIdentifier = @"kCustomIdentifier";
static NSString *const kAdIdentifier = @"kAdIdentifier";
@implementation SJJFactoryPatternCellFactory
- (SJJFactoryPatternCell *)cellInCollection:(UICollectionView *)collectionView
forIndexPath:(NSIndexPath *)indexPath
forMineMode:(SJJFactoryPatternCellType)type {
SJJFactoryPatternCell *cell = nil;
if (type == SJJFactoryPatternCellTypeRectangleCell) {
cell = [self cellInStudyCollectionView:collectionView forIndexPath:indexPath];
} else if (type == SJJFactoryPatternCellTypeCircleCell) {
cell = [self cellInMenuCollectionView:collectionView forIndexPath:indexPath];
} else {
cell = [self cellInSettingCollectionView:collectionView forIndexPath:indexPath];
}
return cell;
}
- (SJJFactoryPatternRectangleCell *)cellInStudyCollectionView:(UICollectionView *)collectionView
forIndexPath:(NSIndexPath *)indexPath {
NSString *identity = kSubjectCellIdentifier;
SJJFactoryPatternRectangleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identity forIndexPath:indexPath];
return cell;
}
- (SJJFactoryPatternSpuareCell *)cellInMenuCollectionView:(UICollectionView *)collectionView
forIndexPath:(NSIndexPath *)indexPath {
NSString *identity = kMenuCellIdentifier;
SJJFactoryPatternSpuareCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identity forIndexPath:indexPath];
return cell;
}
- (SJJFactoryPatternCircleCell *)cellInSettingCollectionView:(UICollectionView *)collectionView
forIndexPath:(NSIndexPath *)indexPath {
NSString *identity = kSettingIdentifier;
SJJFactoryPatternCircleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identity forIndexPath:indexPath];
return cell;
}
@end
複製代碼
-------------------------------實體類--------------------------
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SJJFactoryPatternCell : UICollectionViewCell
- (void)refreshData;
@end
@interface SJJFactoryPatternCircleCell : SJJFactoryPatternCell
@end
@interface SJJFactoryPatternSpuareCell : SJJFactoryPatternCell
@end
@interface SJJFactoryPatternRectangleCell : SJJFactoryPatternCell
@end
NS_ASSUME_NONNULL_END
複製代碼
#import "SJJFactoryPatternCell.h"
@implementation SJJFactoryPatternCell
- (instancetype)init {
if (self = [super init]) {
[self setupUI];
}
return self;
}
- (void)setupUI {
}
- (void)refreshData {
}
@end
@implementation SJJFactoryPatternCircleCell
- (void)setupUI {
}
- (void)refreshData {
}
@end
@implementation SJJFactoryPatternSpuareCell
- (void)setupUI {
}
- (void)refreshData {
}
@end
@implementation SJJFactoryPatternRectangleCell
- (void)setupUI {
}
- (void)refreshData {
}
@end
複製代碼
#抽象工廠
好比說一個界面有UITableView和UICollectionView,UITableView有不一樣中cell,UICollectionView裏有不一樣中cell。
重點貼幾處不同的代碼
SJJAbstractFactory 抽象工廠類
#import <UIKit/UIKit.h>
@class SSJAFPCollectionViewCell;
@class SSJAFPTableViewCell;
typedef NS_ENUM (NSInteger, SSJAFPCollectionViewCellType) {
SSJAFPCollectionViewCellTypeCell,
SSJAFPCollectionViewCellTypeCircleCell,
SSJAFPCollectionViewCellTypeSpuareCell,
SSJAFPCollectionViewCellTypeRectangleCell,
};
typedef NS_ENUM (NSInteger, SSJAFPCellType) {
SSJAFPTypeCell,
SSJAFPTypeCircleCell,
SSJAFPTypeSpuareCell,
SSJAFPTypeRectangleCell,
};
NS_ASSUME_NONNULL_BEGIN
@interface SJJAbstractFactory : NSObject
- (SSJAFPCollectionViewCell *)cellInCollection:(UICollectionView *)collectionView
forIndexPath:(NSIndexPath *)indexPath
forMineMode:(SSJAFPCollectionViewCellType)type;
- (SSJAFPTableViewCell *)cellInTableView:(UITableView *)tableView
forIndexPath:(NSIndexPath *)indexPath
forMineMode:(SSJAFPCellType)type;
@end
NS_ASSUME_NONNULL_END
複製代碼
SSJAFPCollectionViewCellFactory CollectionViewCell工廠類
#import "SJJAbstractFactory.h"
NS_ASSUME_NONNULL_BEGIN
@interface SSJAFPCollectionViewCellFactory : SJJAbstractFactory(重點看這裏)
- (SSJAFPCollectionViewCell *)cellInCollection:(UICollectionView *)collectionView
forIndexPath:(NSIndexPath *)indexPath
forMineMode:(SSJAFPCollectionViewCellType)type;
@end
NS_ASSUME_NONNULL_END
複製代碼
SSJAFPTableViewCellFactory TableViewCell工廠類
#import "SJJAbstractFactory.h"
NS_ASSUME_NONNULL_BEGIN
@interface SSJAFPTableViewCellFactory : SJJAbstractFactory(重點看這裏)
- (SSJAFPTableViewCell *)cellInTableView:(UITableView *)tableView
forIndexPath:(NSIndexPath *)indexPath
forMineMode:(SSJAFPCellType)type;
@end
NS_ASSUME_NONNULL_END
複製代碼