代理模式:为其他对象提供一种代理以控制对这个对象的访问
优点:
1、 代理模式能够协调调用者和被调用者,在一定程度上降低了系统的耦合度。
2、 代理对象可以在客户端和目标对象之间起到中介的作用
缺点:
1、由于在客户端和真实主题之间增加了代理对象,因此有些类型的代理模式可能会造成请求的处理速度变慢。
2、 实现代理模式需要额外的工作,有些代理模式的实现非常复杂。
项目地址:https://github.com/GameRisker/Study-Design-Patterns.git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// // Pursuit.h // // // Created by GameRisker on 15/12/27. // // #import "IGiveGift.h" #import <Foundation/Foundation.h> @interface Pursuit : NSObject <IGiveGift> @property(strong, nonatomic) Girl *girl; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// // Pursuit.m // // // Created by GameRisker on 15/12/27. // // #import "Pursuit.h" @implementation Pursuit - (instancetype)initWithGirl:(Girl *)girl { if (self = [super init]) { self.girl = girl; } return self; } - (void)GiveChocolate { NSLog(@"%@ Give you chocolate.", self.girl.name); } - (void)GiveDolls { NSLog(@"%@ Give you dolls.", self.girl.name); } - (void)GiveFlowers { NSLog(@"%@ Give you flower.", self.girl.name); } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // Girl.h // // // Created by GameRisker on 15/12/27. // // #import <Foundation/Foundation.h> @interface Girl : NSObject @property(nonatomic, strong) NSString *name; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// // Girl.m // // // Created by GameRisker on 15/12/27. // // #import "Girl.h" @implementation Girl @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // Proxy.h // // // Created by GameRisker on 15/12/27. // // #import "IGiveGift.h" #import "Pursuit.h" #import <Foundation/Foundation.h> @interface Proxy : NSObject <IGiveGift> @property(strong, nonatomic) Pursuit *pursuit; - (instancetype)initWithGirl:(Girl *)girl; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// // Proxy.m // // // Created by GameRisker on 15/12/27. // // #import "Proxy.h" @implementation Proxy - (instancetype)initWithGirl:(Girl *)girl { if (self = [super init]) { self.pursuit = [[Pursuit alloc] initWithGirl:girl]; } return self; } - (void)GiveFlowers { [self.pursuit GiveFlowers]; } - (void)GiveChocolate { [self.pursuit GiveChocolate]; } - (void)GiveDolls { [self.pursuit GiveDolls]; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // IGiveGift.h // // // Created by GameRisker on 15/12/27. // // #import "Girl.h" #import <Foundation/Foundation.h> @protocol IGiveGift <NSObject> - (instancetype)initWithGirl:(Girl *)girl; - (void)GiveDolls; - (void)GiveFlowers; - (void)GiveChocolate; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// // main.m // Proxy // // Created by GameRisker on 15/12/27. // Copyright (c) 2015年 GameRisker. All rights reserved. // #import "Girl.h" #import "Proxy.h" #import "Pursuit.h" #import <Foundation/Foundation.h> int main(int argc, const char *argv[]) { @autoreleasepool { Girl *girl = [[Girl alloc] init]; girl.name = @"Girl"; Proxy *proxy = [[Proxy alloc] initWithGirl:girl]; [proxy GiveDolls]; [proxy GiveChocolate]; [proxy GiveFlowers]; } return 0; } |