Oh!Coder

Coding Life

Objective C--工厂方法模式

| Comments

今天和大家分享的设计模式是工厂方法模式。

首先给出工厂方法模式的一个简单的定义:

工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。

早在第一次和大家分享的关于简单工厂模式中就有提到过一次关于“工厂类”模式。为了帮助大家能够回忆一下简单工厂模式,在这里提一下简单工厂模式的优点,简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。其实,工厂方法模式是简单工厂模式的进一步抽象和推广。由于使用了多态性,工厂方法模式保持了简单工厂模式的优点,而且克服了它的缺点。但缺点是,由于每加一个产品,就需要加一个产品工厂的类,增加了额外的开发量。

下面还是以计算器为例子,详细介绍工厂方法模式,还是老样子,先向大家展示一下类结构图。

pic

上面这张图向大家展示了各个类之间的关系。其实和简单工厂模式不同的是,类图的右边抽象工厂接口是相比简单工厂模式多出来的抽象接口。

下面直接上代码吧,别的不多说了。

注意:本文所有代码均在ARC环境下编译通过。

  • Operation类接口
1
2
3
4
5
6
7
8
9
10
#import <Foundation/Foundation.h>

@interface Operation :NSObject{
    double numberA;
    double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
  • Operation类实现
1
2
3
4
5
6
7
8
#import "Operation.h"

@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
    return -1.0;
}
@end
  • OperationAdd类接口
1
2
3
4
#import "Operation.h"

@interface OperationAdd :Operation
@end
  • OperationAdd类实现
1
2
3
4
5
6
7
8
9
#import "OperationAdd.h"

@implementation OperationAdd
-(double) GetResult{
    double result =0;
    result = numberA+numberB;
    return result;
}
@end
  • OperationDiv类接口
1
2
3
4
#import "Operation.h"

@interface OperationDiv :Operation
@end
  • OperationDiv类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#import "OperationDiv.h"

@implementation OperationDiv
-(double)GetResult{
    double result =0;
    @try{
        result = numberA/numberB;
    }
    @catch(NSException *exception) {
        NSLog(@"除数不能为0");
    }
    return result;
}
@end
  • OperationMul类接口
1
2
3
4
#import "Operation.h"

@interface OperationMul :Operation
@end
  • OperationMul类实现
1
2
3
4
5
6
7
8
9
#import "OperationMul.h"

@implementation OperationMul
-(double)GetResult{
    double result =0;
    result = numberA*numberB;
    return result;
}
@end
  • OperationSub类接口
1
2
3
4
#import "Operation.h"

@interface OperationSub :Operation
@end
  • OperationSub类实现
1
2
3
4
5
6
7
8
9
#import "OperationSub.h"

@implementation OperationSub
-(double)GetResult{
    double result =0;
    result = numberA-numberB;
    return result;
}
@end
  • IFactory类接口
1
2
3
4
5
6
#import <Foundation/Foundation.h>

#import "Operation.h"
@interface IFactory :NSObject
-(Operation*)CreateOperation;
@end
  • IFactory类实现
1
2
3
4
5
6
7
#import "IFactory.h"

@implementation IFactory
-(Operation*)CreateOperation{
    return [[Operation alloc]init];
}
@end
  • AddFactory类接口
1
2
3
4
#import "IFactory.h"

@interface AddFactory :IFactory
@end
  • AddFactory类实现
1
2
3
4
5
6
7
8
#import "AddFactory.h"
#import "OperationAdd.h"

@implementation AddFactory
-(Operation*)CreateOperation{
    return [[OperationAdd alloc]init];
}
@end
  • SubFactory类接口
1
2
3
4
#import "IFactory.h"

@interface SubFactory :IFactory
@end
  • SubFactory类实现
1
2
3
4
5
6
7
8
#import "SubFactory.h"
#import "OperationSub.h"

@implementation SubFactory
-(Operation*)CreateOperation{
    return [[OperationSub alloc]init];
}
@end
  • MulFactory类接口
1
2
3
4
#import "IFactory.h"

@interface MulFactory :IFactory
@end
  • MulFactory类实现
1
2
3
4
5
6
7
8
#import "MulFactory.h"
#import "OperationMul.h"

@implementation MulFactory
-(Operation*)CreateOperation{
    return [[OperationMul alloc]init];
}
@end
  • DivFactory类接口
1
2
3
4
#import "IFactory.h"

@interfaceDiv Factory :IFactory
@end
  • DivFactory类实现
1
2
3
4
5
6
7
8
#import "DivFactory.h"
#import "OperationDiv.h"

@implementation DivFactory
-(Operation*)CreateOperation{
    return [[OperationDiv alloc]init];
}
@end
  • Main方法调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "AddFactory.h" //加法工厂,你可以根据需要添加其他运算工厂

int main (int argc,const char* argv[])
{
    @autoreleasepool{
        IFactory *operFactory = [[AddFactory alloc]init];
        Operation *oper = [operFactory CreateOperation];
        [oper setNumberA:1];
        [oper setNumberB:2];
        double result = [oper GetResult];
        NSLog(@"The result is %f", result);
    }
    return 0;
}

好啦,上面就是工厂方法模式的Objective C的类代码。

Comments