23种设计模式(三)
版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.04.26 |
前言
前面讲了23种设计模式中的前几个,下面我们继续,先看前两篇文章。
1. 23种设计模式(一)
2. 23种设计模式(二)
详述
四、工厂方法模式——Factory method
定义一个拥有创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类,先看一下代码结构。
工厂方法模式下面直接看代码
1. ViewController.m
#import "ViewController.h"
#import "HCDCalculate.h"
#import "HCDfactory.h"
#import "HCDCalculateAdd.h"
#import "HCDfactoryAdd.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDfactory *factory = [[HCDfactoryAdd alloc]init];
HCDfactory *calculate = [factory createFactory];
calculate.numberA = 10;
calculate.numberB = 15;
NSLog(@"结果是%f",[calculate calculate]);
}
@end
2. HCDfactoryDivide.h
#import "HCDfactory.h"
@interface HCDfactoryDivide : HCDfactory
@end
3. HCDfactoryDivide.m
#import "HCDfactoryDivide.h"
#import "HCDCalculateDivide.h"
@implementation HCDfactoryDivide
- (id<HCDCalculate>)createFactory
{
return [[HCDCalculateDivide alloc]init];
}
@end
4. HCDfactoryMultiply.h
#import "HCDfactory.h"
@interface HCDfactoryMultiply : HCDfactory
@end
5. HCDfactoryMultiply.m
#import "HCDfactoryMultiply.h"
#import "HCDCalcuteMultiply.h"
@implementation HCDfactoryMultiply
- (id<HCDCalculate>)createFactory
{
return [[HCDCalcuteMultiply alloc]init];
}
@end
6. HCDCalculate.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol HCDCalculate <NSObject>
@property (nonatomic, assign) CGFloat numberA;
@property (nonatomic, assign) CGFloat numberB;
- (CGFloat)calculate;
@end
7. HCDCalculateAdd.h
#import <Foundation/Foundation.h>
#import "HCDCalculate.h"
@interface HCDCalculateAdd : NSObject <HCDCalculate>
@end
8. HCDCalculateAdd.m
#import "HCDCalculateAdd.h"
@implementation HCDCalculateAdd
@synthesize numberA = _numberA;
@synthesize numberB = _numberB;
- (CGFloat)calculate
{
return self.numberA + self.numberB;
}
@end
9. HCDCalculateDivide.h
#import <Foundation/Foundation.h>
#import "HCDCalculate.h"
@interface HCDCalculateDivide : NSObject<HCDCalculate>
@end
10. HCDCalculateDivide.m
#import "HCDCalculateDivide.h"
@implementation HCDCalculateDivide
@synthesize numberA = _numberA;
@synthesize numberB = _numberB;
- (CGFloat)calculate
{
if (self.numberB == 0) {
assert(self.numberB);
}
return self.numberA/self.numberB;
}
@end
11. HCDCalculateMinus.h
#import <Foundation/Foundation.h>
#import "HCDCalculate.h"
@interface HCDCalculateMinus : NSObject <HCDCalculate>
@end
12. HCDCalculateMinus.m
#import "HCDCalculateMinus.h"
@implementation HCDCalculateMinus
@synthesize numberA = _numberA;
@synthesize numberB = _numberB;
- (CGFloat)calculate
{
return self.numberA - self.numberB;
}
@end
13. HCDCalcuteMultiply.h
#import <Foundation/Foundation.h>
#import "HCDCalculate.h"
@interface HCDCalcuteMultiply : NSObject <HCDCalculate>
@end
14. HCDCalcuteMultiply.m
#import "HCDCalcuteMultiply.h"
@implementation HCDCalcuteMultiply
@synthesize numberA = _numberA;
@synthesize numberB = _numberB;
- (CGFloat)calculate
{
return self.numberA * self.numberB;
}
@end
15. HCDfactory.h
#import <Foundation/Foundation.h>
#import "HCDCalculate.h"
@interface HCDfactory : NSObject <HCDCalculate>
- (id<HCDCalculate>)createFactory;
@end
16. HCDfactory.m
#import "HCDfactory.h"
@implementation HCDfactory
@end
17. HCDfactoryAdd.h
#import "HCDfactory.h"
@interface HCDfactoryAdd : HCDfactory
@end
18. HCDfactoryAdd.m
#import "HCDfactoryAdd.h"
#import "HCDCalculateAdd.h"
@implementation HCDfactoryAdd
- (id<HCDCalculate>)createFactory
{
return [[HCDCalculateAdd alloc]init];
}
@end
19. HCDfactoryMinus.h
#import "HCDfactory.h"
@interface HCDfactoryMinus : HCDfactory
@end
20. HCDfactoryMinus.m
#import "HCDfactoryMinus.h"
#import "HCDCalculateMinus.h"
@implementation HCDfactoryMinus
- (id<HCDCalculate>)createFactory
{
return [[HCDCalculateMinus alloc]init];
}
@end
看一下结果。
2017-04-26 00:44:24.890 5工厂方法模式[2352:100440] 结果是25.000000
五、原型模式——Prototype
“使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象”。最初的定义出现于《设计模式》(Addison-Wesley,1994)。
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。它在多数情况下可被理解为一种深复制的行为。在Objective-C中使用原型模式, 首先要遵循NSCoping协议(OC中一些内置类遵循该协议, 例如NSArray, NSMutableArray等)。但是自定义类就要遵循NSCopying协议,否则复制会crash。
简单来理解就是根据这个原型创建新的对象,而且不需要知道任何创建的细节。打个比方,以前生物课上面,有一个知识点叫细胞分裂,细胞在一定条件下,由一个分裂成2个,再由2个分裂成4个……,分裂出来的细胞基于原始的细胞(原型),这个原始的细胞决定了分裂出来的细胞的组成结构。这种分裂过程,可以理解为原型模式。
看一下原理图。
原型模式从上图可以看到,Prototype类中包括一个clone方法,Client调用其拷贝方法clone即可得到实例,不需要手工去创建实例。ConcretePrototype1和ConcretePrototype2为Prototype的子类,实现自身的clone方法,如果Client调用ConcretePrototype1的clone方法,将返回ConcretePrototype1的实例。
我们ios中的深浅拷贝就是原型模式的一种应用,深浅复制这里就不多说了,可以看我前几篇中的ios深浅复制。
后记
风景图今天这篇先写这几种,后续,拿一张风景图镇楼,谢谢大家对我的支持。