工厂模式个人理解:基类工厂---->建立具体工厂---->生产具体产品---->产品拥有那些具体功能
BaseFactory
#import <Foundation/Foundation.h>
#import "BasePhone.h"
#import "BaseWatch.h"
@interface BaseFactory : NSObject
- (BasePhone *)createPhone;
- (BaseWatch *)createWatch;
@end
#import "BaseFactory.h"
@implementation BaseFactory
- (BasePhone *)createPhone {
return nil;
}
- (BaseWatch *)createWatch {
return nil;
}
@end
HuaWeiFactory
#import "BaseFactory.h"
#import "HuaWeiP40.h"
NS_ASSUME_NONNULL_BEGIN
@interface HuaWeiFactory : BaseFactory
@end
NS_ASSUME_NONNULL_END
#import "HuaWeiFactory.h"
#import "HuaWeiWatch.h"
@implementation HuaWeiFactory
- (BaseHuaWeiPhone *)createPhone{
return [[HuaWeiP40 alloc] init];
}
- (BaseWatch *)createWatch{
return [[HuaWeiWatch alloc] init];
}
@end
PhoneProtocol
#import <Foundation/Foundation.h>
@protocol PhoneProtocol <NSObject>
//打电话
- (void)phoneCall;
//发短信
- (void)sendMessage;
@end
BaseHuaWeiPhone
#import <Foundation/Foundation.h>
#import "PhoneProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface BaseHuaWeiPhone : NSObject<PhoneProtocol>
@end
NS_ASSUME_NONNULL_END
#import "BaseHuaWeiPhone.h"
@implementation BaseHuaWeiPhone
- (void)phoneCall {
}
- (void)sendMessage {
}
@end
HuaWeiP40
#import "BaseHuaWeiPhone.h"
NS_ASSUME_NONNULL_BEGIN
@interface HuaWeiP40 : BaseHuaWeiPhone
- (void)niubilityPhoto;
@end
NS_ASSUME_NONNULL_END
#import "HuaWeiP40.h"
@implementation HuaWeiP40
- (void)phoneCall{
NSLog(@"%s",__func__);
}
- (void)sendMessage{
NSLog(@"%s",__func__);
}
- (void)niubilityPhoto{
NSLog(@"%s",__func__);
}
HuaWeiWatch
#import "BaseWatch.h"
NS_ASSUME_NONNULL_BEGIN
@interface HuaWeiWatch : BaseWatch
@end
NS_ASSUME_NONNULL_END
#import "HuaWeiWatch.h"
@implementation HuaWeiWatch
@end
具体调用 FactoryManager
#import <Foundation/Foundation.h>
#import "AppleFactory.h"
#import "GoogleFactory.h"
#import "HuaWeiFactory.h"
typedef NS_ENUM(NSUInteger, KFactoryType) {
KApple,
KGoogle,
KHuaWei
};
@interface FactoryManager : NSObject
/**
获取工厂
@param factoryType 工厂类型
@return 创建出的工厂
*/
+ (BaseFactory *)factoryWithBrand:(KFactoryType)factoryType;
#import "FactoryManager.h"
@implementation FactoryManager
+ (BaseFactory *)factoryWithBrand:(KFactoryType)factoryType {
BaseFactory *factory = nil;
if (factoryType == KApple) {
factory = [[AppleFactory alloc] init];
} else if (factoryType == KGoogle) {
factory = [[GoogleFactory alloc] init];
}else if(factoryType == KHuaWei){
factory = [[HuaWeiFactory alloc] init];
}
return factory;
}
@end
调用FactoryManager 来生成具体的工厂类
// 获取工厂
BaseFactory* huaweiFactory = [FactoryManager factoryWithBrand:KHuaWei];
//创建商品
HuaWeiP40 *p40 = (HuaWeiP40 *)[huaweiFactory createPhone];
BaseWatch* huaweiWatch = [huaweiFactory createWatch];
NSLog(@"%@ --- %@", p40, huaweiWatch);
[p40 phoneCall];
[p40 niubilityPhoto];
输出日志:
2021-06-08 14:31:45.482387+0800 AbstractFactoryPattern[9789:465819] <HuaWeiP40: 0x60000183c1b0> --- <HuaWeiWatch: 0x60000183c2e0>
2021-06-08 14:31:45.482492+0800 AbstractFactoryPattern[9789:465819] -[HuaWeiP40 phoneCall]
2021-06-08 14:31:45.482574+0800 AbstractFactoryPattern[9789:465819] -[HuaWeiP40 niubilityPhoto]