工厂模式(经常使用)
工厂模式是常见的设计模式之一,这种模式的设计模式属于创建型模式,它提供了一种创建对象的方式。
何时使用:我们明确地计划不同条件下创建不同实例时(笔者认为3种类型为一个临界点)
优点:
- 一个调用者想创建一个对象,知道名称就可以了
- 扩展性高,如果想增加一个产品,增加一个工厂类就可以了
- 屏蔽了产品实现的细节
缺点:
每增加一个产品,都要增加一个产品类,使得工程中类的个数成倍增加,同时也使文件结构变复杂了
抽象工厂模式(笔者一般很少用到了)
抽象工程模式围绕一个超级大厂创建其他小厂,这种模式的设计模式属于创建型模式,它提供了一种创建对象的方式。
注:“超级大厂创建其他小厂”(可以这么理解,比如电子厂包括:主板,电池,屏幕,等产品线,电子厂就是大厂,主板,电池,屏幕就是一个部分(你也可以理解为一个小厂))
何时使用:系统产品族里面有多于一个的产品族,而系统某时候只需要某一种产品
优点:同上
缺点:同上
抽象工厂模式和工厂模式区别:
- 工厂方法模式只有一个抽象的产品类,而抽象工厂模式有多个
- 工厂方法模式的具体工厂类只能创建一个具体的产品类的实例,抽象工厂可以创建多个
- 工厂方法创建“一种产品”他们着中重点于“怎么创建”,抽象工厂可以创建一系列长跑,着重点在于“创建哪些产品上”
所以,抽象工厂就像工厂,而工厂方法就像工厂中的某一条产品线。
工厂模式
比如说一个视图里面要创建多种不同的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
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
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
@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