定义

简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

模式结构

简单工厂模式包含如下角色:

  • 工厂角色(Factory):负责实现创建所有实例的内部逻辑
  • 抽象产品角色(Product):所创建的所有对象的父类,负责描述所有实例所共有的公共接口
  • 具体产品角色(ConcreteProduct):创建目标,所有创建的对象都充当这个角色的某个具体类的实例。

简单工厂模式 - 图1

代码实现

抽象产品

  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. @protocol LHCarLicenseProtocol <NSObject>
  4. // 打印牌照
  5. - (NSString *)printLicenseNumber;
  6. @end
  7. @interface LHCarLicense : NSObject<LHCarLicenseProtocol>
  8. @property(nonatomic, copy)NSString *city; // 城市
  9. @property(nonatomic, copy, readonly)NSString *licenseNumber; // 车牌号
  10. @end
  1. #import "LHCarLicense.h"
  2. @implementation LHCarLicense
  3. #pragma mark -
  4. #pragma mark Public Method
  5. - (NSString *)printLicenseNumber{
  6. _licenseNumber = [self getLicenseNumber];
  7. return _licenseNumber;
  8. }
  9. #pragma mark -
  10. #pragma mark Private Method
  11. // 获取牌照
  12. - (NSString *)getLicenseNumber
  13. {
  14. NSString *firstChar = [self getRandomChar];
  15. NSString *lastNumber = @"";
  16. for (NSInteger index = 0; index < 5; index++) {
  17. NSInteger random = [self getRandomNumber:0 to:1];
  18. NSString *newNumber = random == 0 ? [self getRandomChar] : [NSString stringWithFormat:@"%ld",(long)[self getRandomNumber:0 to:9]];
  19. lastNumber = [NSString stringWithFormat:@"%@%@",lastNumber,newNumber];
  20. }
  21. return [NSString stringWithFormat:@"%@%@·%@",_city,firstChar,lastNumber];
  22. }
  23. // 获取牌照号
  24. - (NSString *)getRandomChar
  25. {
  26. int NUMBER_OF_CHARS = 1;
  27. char data[NUMBER_OF_CHARS];
  28. data[0] = (char)('A' + (arc4random_uniform(26)));
  29. return [[NSString alloc] initWithBytes:data length:NUMBER_OF_CHARS encoding:NSUTF8StringEncoding];
  30. }
  31. //获取一个随机整数,范围在[from,to),包括from,不包括to
  32. -(NSInteger)getRandomNumber:(NSInteger)from to:(NSInteger)to
  33. {
  34. return (NSInteger)(from + (arc4random() % (to - from + 1)));
  35. }

具体产品

  1. #import "LHCarLicense.h"
  2. @interface LHBlueCarLicense : LHCarLicense
  3. @end
  4. #import "LHBlueCarLicense.h"
  5. @implementation LHBlueCarLicense
  6. // 打印牌照号
  7. - (NSString *)printLicenseNumber{
  8. [super printLicenseNumber];
  9. return [NSString stringWithFormat:@"蓝色牌照: %@",self.licenseNumber];
  10. }
  11. @end
  1. #import "LHCarLicense.h"
  2. @interface LHYellowCarLicense : LHCarLicense
  3. @end
  4. #import "LHYellowCarLicense.h"
  5. @implementation LHYellowCarLicense
  6. // 打印牌照号
  7. - (NSString *)printLicenseNumber{
  8. [super printLicenseNumber];
  9. return [NSString stringWithFormat:@"黄色牌照: %@",self.licenseNumber];
  10. }
  11. @end

工厂角色

  1. #import <Foundation/Foundation.h>
  2. @class LHCarLicense;
  3. typedef enum : NSUInteger {
  4. ELicenseType_Blue,
  5. ELicenseType_Yellow
  6. } ELicenseType;
  7. @interface LHCarLicenseFactory : NSObject
  8. /**
  9. * 获取牌照工厂
  10. * @param type 牌照类型
  11. * @return 返回牌照对象
  12. */
  13. + (LHCarLicense *)createCarLicenseWithType:(ELicenseType)type;
  14. @end
  15. #import "LHCarLicenseFactory.h"
  16. #import "LHCarLicense.h"
  17. #import "LHBlueCarLicense.h"
  18. #import "LHYellowCarLicense.h"
  19. @implementation LHCarLicenseFactory
  20. + (LHCarLicense *)createCarLicenseWithType:(ELicenseType)type{
  21. LHCarLicense *_license;
  22. switch (type) {
  23. case ELicenseType_Blue:
  24. _license = [[LHBlueCarLicense alloc] init];
  25. break;
  26. case ELicenseType_Yellow:
  27. _license = [[LHYellowCarLicense alloc] init];
  28. break;
  29. }
  30. return _license;
  31. }
  32. @end

客户端调用

  1. #import "ViewController.h"
  2. #import "LHCarLicenseFactory.h"
  3. #import "LHCarLicense.h"
  4. @interface ViewController ()
  5. @end
  6. @implementation ViewController
  7. - (void)viewDidLoad {
  8. [super viewDidLoad];
  9. }
  10. - (void)didReceiveMemoryWarning {
  11. [super didReceiveMemoryWarning];
  12. }
  13. - (void)createLicense:(ELicenseType)type
  14. {
  15. LHCarLicense *_license = [LHCarLicenseFactory createCarLicenseWithType:type];
  16. _license.city = _txtCity.text ? _txtCity.text : @"京";
  17. _lbLicenseNumber.text = [_license printLicenseNumber];
  18. }
  19. #pragma mark -
  20. #pragma mark Button Event
  21. // 生成蓝色牌照
  22. - (IBAction)btnBlueEvent:(UIButton *)sender {
  23. [self createLicense:ELicenseType_Blue];
  24. }
  25. // 生成黄色牌照
  26. - (IBAction)btnYellowEvent:(UIButton *)sender {
  27. [self createLicense:ELicenseType_Yellow];
  28. }
  29. @end

这样工作人员就可以批量生成不同类型的车牌号了,输出结果如下:
image.png

优点

  • 工厂类含有必要的判断逻辑,可以决定在什么时候创建哪一个产品类的实例,客户端可以免除直接创建产品对象的责任,通过这种做法实现了对责任的分割,它提供了专门的工厂类用于创建对象。
  • 客户端无须知道所创建的具体产品类的类名,只需要知道具体产品类所对应的参数即可,对于一些复杂的类名,通过简单工厂模式可以减少使用者的记忆量。
  • 通过引入配置文件,可以在不修改任何客户端代码的情况下更换和增加新的具体产品类,在一定程度上提高了系统的灵活性。

    缺点

  • 由于工厂类集中了所有产品创建逻辑,一旦不能正常工作,整个系统都要受到影响。

  • 产品类本身是符合开闭原则的,对扩展开放对修改关闭,但是工厂类却违反了开闭原则,
  • 系统扩展困难,一旦添加新产品就不得不修改工厂逻辑,在产品类型较多时,有可能造成工厂逻辑过于复杂,不利于系统的扩展和维护。
  • 简单工厂模式由于使用了静态工厂方法,造成工厂角色无法形成基于继承的等级结构。