- // TravelProtocol.h
- #import <Foundation/Foundation.h>
- @protocol TravelProtocol <NSObject>
- - (void)buyTicket;
- - (void)tanslate;
- - (void)exchangeMoney;
- - (void)hangout;
- @end
复制代码- // AmericaProtocol.h
- #import <Foundation/Foundation.h>
- #import "TravelProtocol.h"
- @protocol AmericaProtocol <TravelProtocol>
- //美国协议AmericaProtocol遵守旅游协议TravelProtocol
- @end
复制代码- // EuropeProtocol.h
- #import <Foundation/Foundation.h>
- #import "TravelProtocol.h"
- @protocol EuropeProtocol <TravelProtocol>
- //欧洲协议EuropeProtocol遵守旅游协议TravelProtocol
- @end
复制代码- // JapanProtocol.h
- #import <Foundation/Foundation.h>
- #import "TravelProtocol.h"
- @protocol JapanProtocol <TravelProtocol>
- //日本协议JapanProtocol遵守旅游协议TravelProtocol
- @end
复制代码- // AmericaTravleAgency.h
- #import <Foundation/Foundation.h>
- #import "AmericaProtocol.h"
- @interface AmericaTravelAgency : NSObject <AmericaProtocol>
- //美国代理AmericaTravelAgency遵守美国协议AmericaProtocol
- @end
复制代码- // AmericaTravleAgency.m
- #import "AmericaTravelAgency.h"
- @implementation AmericaTravelAgency
- - (void)buyTicket
- {
- NSLog(@"美国代理买机票飞往美国");
- }
- - (void)tanslate
- {
- NSLog(@"美国代理把当前语言翻译成美式英语");
- }
- - (void)exchangeMoney
- {
- NSLog(@"美国代理把当前货币兑换成美元");
- }
- - (void)hangout
- {
- NSLog(@"美国代理带路到拉斯维加斯赌场试试手气");
- }
- - (void)dealloc
- {
- NSLog(@"美国代理被回收");
- }
- @end
复制代码- // EuropeTravelAgency.h
- #import <Foundation/Foundation.h>
- #import "EuropeProtocol.h"
- @interface EuropeTravelAgency : NSObject <EuropeProtocol>
- //欧洲代理EuropeTravelAgency遵守欧洲协议EuropeProtocol
- @end
复制代码- // EuropeTravelAgency.m
- #import "EuropeTravelAgency.h"
- @implementation EuropeTravelAgency
- - (void)buyTicket
- {
- NSLog(@"欧洲代理买机票飞往欧洲");
- }
- - (void)tanslate
- {
- NSLog(@"欧洲代理把当前语言翻译成欧洲语言");
- }
- - (void)exchangeMoney
- {
- NSLog(@"欧洲代理把当前货币兑换成欧元");
- }
- - (void)hangout
- {
- NSLog(@"欧洲代理带路参观德国保时捷工厂");
- }
- - (void)dealloc
- {
- NSLog(@"欧洲代理被回收");
- }
- @end
复制代码- // JapanTravelAgency.h
- #import <Foundation/Foundation.h>
- #import "JapanProtocol.h"
- @interface JapanTravelAgency : NSObject <JapanProtocol>
- //日本代理JapanTravelAgency遵守日本协议JapanProtocol
- @end
复制代码- // JapanTravelAgency.m
- #import "JapanTravelAgency.h"
- @implementation JapanTravelAgency
- - (void)buyTicket
- {
- NSLog(@"日本代理买机票飞往日本");
- }
- - (void)tanslate
- {
- NSLog(@"日本代理把当前语言翻译成日语");
- }
- - (void)exchangeMoney
- {
- NSLog(@"日本代理把当前货币兑换成日元");
- }
- - (void)hangout
- {
- NSLog(@"日本代理带路到咖啡馆体验女仆服务");
- }
- - (void)dealloc
- {
- NSLog(@"日本代理被回收");
- }
- @end
复制代码- // Tourist.h
- #import <Foundation/Foundation.h>
- #import "TravelProtocol.h"
- @interface Tourist : NSObject
- @property(nonatomic , strong) id<TravelProtocol> delegate; //要求代理属性必须是遵守TravelProtocol协议的对象
- + (id)tourist;
- //游客要做这些事
- - (void)buyTicket;
- - (void)tanslate;
- - (void)exchangeMoney;
- - (void)hangout;
- @end
复制代码- // Tourist.m
- #import "Tourist.h"
- @implementation Tourist
- + (id)tourist
- {
- return [[Tourist alloc] init];
- }
- //这些事都交给代理去做,自己不亲自出马
- - (void)buyTicket
- {
- [_delegate buyTicket];
- }
- - (void)tanslate
- {
- [_delegate tanslate];
- }
- - (void)exchangeMoney
- {
- [_delegate exchangeMoney];
- }
- - (void)hangout
- {
- [_delegate hangout];
- }
- - (void)dealloc
- {
- NSLog(@"游客代理被回收");
- }
- @end
复制代码- // main.m
- /*
- 代理设计模式:
- * 定义一个代理协议,声明各种动作
- * 声明一个代理类,必须遵守代理协议,并实现其中声明的动作
- * 声明主类,拥有一个代理属性,该属性必须遵守代理协议
- * 主类对象要进行的动作,都交给代理属性去执行
-
- 好处:
- * 不同的代理类可以差异化实现代理协议中声明的动作,例如:各国代理买机票的目的地不同,翻译的语言不同,兑换的货币不同,带路游览的目的地不同
- * 更改代理类代码时无需更改主类代码,只需限制代理属性的类型即可(即限制该属性必须遵守代理协议),例如:某天,美国代理良心发现,不带游客去赌场了,改去华尔街游览了,只需将自己的hangout方法实现改为去华尔街游览,Tourist类的hangout方法无需改变。
- */
- #import <Foundation/Foundation.h>
- #import "Tourist.h"
- #import "AmericaTravelAgency.h"
- #import "EuropeTravelAgency.h"
- #import "JapanTravelAgency.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- Tourist *t = [Tourist tourist];
- AmericaTravelAgency *a = [[AmericaTravelAgency alloc] init];
- EuropeTravelAgency *e = [[EuropeTravelAgency alloc] init];
- JapanTravelAgency *j = [[JapanTravelAgency alloc] init];
-
- t.delegate = a;
- NSLog(@"游客找了一个代理");
- [t buyTicket];
- [t tanslate];
- [t exchangeMoney];
- [t hangout];
-
- t.delegate = e;
- NSLog(@"游客找了一个代理");
- [t buyTicket];
- [t tanslate];
- [t exchangeMoney];
- [t hangout];
-
- t.delegate = j;
- NSLog(@"游客找了一个代理");
- [t buyTicket];
- [t tanslate];
- [t exchangeMoney];
- [t hangout];
- }
- return 0;
- }
复制代码
|