• 设计模式:代理


    1.通过中介公司找房子 

    //1:协议声明:
    #import <Foundation/Foundation.h>  
    @protocol FindApartment <NSObject> 
      -(void)findApartment;
    @end
    
    //2:代理角色声明(Agent.h)头文件声明
    #import <Foundation/Foundation.h> 
    #import "FindApartment.h" 
        @interface Agent : NSObject <FindApartment> 
    @end
    
    //3:代理角色实现(Agent.m)实现文件
    #import "Agent.h" 
    @implementation Agent 
    -(void)findApartment{     
        NSLog(@"findApartment"); 
    } 
    @end
    
    //4:真实角色声明(Person.h)头文件声明
    #import <Foundation/Foundation.h>
    #import "FindApartment.h" 
    @interface Person : NSObject {   
      @private     NSString *_name;    
      id<FindApartment> _delegate; //委托人(具备中介找房子的协议) 
    }
    @property(nonatomic,copy) NSString *name;
    @property(nonatomic,assign)id<FindApartment> delegate; 
    -(id)initWithName:(NSString *)name withDelegate:(id<FindApartment>) delegate;
    -(void)wantToFindApartment;
    @end
    
    //5:真实角色实现(Person.m)实现
    #import "Person.h" 
    //定义私有方法 
    @interface Person()
     -(void)startFindApartment:(NSTimer *)timer; 
    @end 
    @implementation Person
     @synthesize name=_name; 
     @synthesize delegate=_delegate;
     -(id)initWithName:(NSString *)name withDelegate:(id<FindApartment>) delegate{     
        self=[super init];     
        if(self){         
            self.name=name;         
            self.delegate=delegate;     
        }    
        return self; 
     } 
     -(void)wantToFindApartment{     
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:@"Hello" repeats:YES];     
     }  
     -(void)startFindApartment:(NSTimer *)timer{    
         //NSString *userInfo=[timer userInfo];     
        [self.delegate findApartment];
     }
    @end
    
    //6:测试代理main.m方法
    #import <Foundation/Foundation.h> 
    #import "Person.h" 
    #import "Agent.h"
    int main(int argc, const char * argv[]) {     
     @autoreleasepool {                  
        // insert code here...         
        NSLog(@"Hello, World!");         
        Agent *agent=[[Agent alloc]init];         
        Person *jack=[[Person alloc]initWithName:@"jack" withDelegate:agent];         
        [jack wantToFindApartment];         
        [[NSRunLoop currentRunLoop]run];         
        [jack autorelease];     
     }     
     return 0; 
    }
    View Code

    2.代理设计模式的编码规范:

    (1)一帮情况下,当协议属于谁,就将协议定义到谁的头文件中
    (2)协议的名称一般以它属于那个类的类名开头,后面跟上protocol或delegate
    (3)协议中的方法名称一般以协议的名称protocol之前的作为开头
    (4)一般情况下,协议中的方法会将触发该协议的对象传递出去
    (5)一般情况下,一个类中的代理的名称叫做 delegate;
    (6)当一个类要成为另外一个类的代理的时候,一般情况下:
             .h中用   @protocol 协议名称;   来告诉当前类,这是一个协议。
             .m中 用   #import 类名  来真正导入协议的声明

     例子:学生找房子

    Student.h
    #import <Foundation/Foundation.h>
    @class Student;
    
    //通过代理来找房子,所以首先定义一个协议
    @protocol StudentProtocol <NSObject>
    
    -(void)studentFindHourse:(Student *)student;
    
    @end
    
    
    @interface Student : NSObject
    
    //代理
    @property(nonatomic, strong) id<StudentProtocol> delegate;
    
    //找房子
    -(void)findHourse;
    
    @end
    
    ----------------------------------------------------------------------------------------
    Student.m
    #import "Student.h"
    
    @implementation Student
    
    //学生通过代理找房子
    -(void)findHourse{
        NSLog(@"学生想找房子");
        if ([self.delegate respondsToSelector:@selector(studentFindHourse:)]) {
            [self.delegate studentFindHourse:self];
        }
    }
    
    @end
    
    ----------------------------------------------------------------------------------------
    LoveHome.h
    #import <Foundation/Foundation.h>
    @protocol StudentProtocol;//标明StudentProtocol是一个协议
    
    //爱家,专做销售房子的代理
    @interface LoveHome : NSObject<StudentProtocol>
    
    @end
    
    ----------------------------------------------------------------------------------------
    LoveHome.m
    #import "LoveHome.h"
    #import "Student.h"
    
    @implementation LoveHome
    
    -(void)studentFindHourse:(Student *)student{
        NSLog(@"%s 帮忙找房子",__func__);
    }
    
    @end
    
    ----------------------------------------------------------------------------------------
    main.m
    #import <Foundation/Foundation.h>
    #import "Student.h"
    #import "LoveHome.h"
    
    int main(int argc, const char * argv[]) {
    
        Student *stu = [[Student alloc] init];
        LoveHome *lh = [[LoveHome alloc] init];
        stu.delegate = lh;
        [stu findHourse];
    
        return 0;
    }
    View Code

    根据规范,重写1:

    //Person.h
    #import <Foundation/Foundation.h>
    @class Person;
    
    @protocol PersonProtocol <NSObject>
    
    -(void)personWantToFindApartment:(Person *)person;
    
    @end
    
    //Person要找房子
    @interface Person : NSObject
    
    @property (nonatomic, assign) NSString *name;
    
    //找房子的代理
    @property (nonatomic, strong) id<PersonProtocol>delegate;
    
    //构造方法
    -(instancetype)initWithName:(NSString *)name withDelegate:(id<PersonProtocol>)delegate;
    
    //找房子
    -(void)wantToFindApartment;
    
    @end
    
    
    
    //Person.m
    #import "Person.h"
    
    //定义私有方法
    @interface Person()
    
    -(void)startFindApartment:(NSTimer *)timer;
    
    @end
    
    //实现Person
    @implementation Person
    
    -(instancetype) initWithName:(NSString *)name withDelegate:(id<PersonProtocol>)delegate{
        self = [super init];
        if (self) {
            self.name = name;
            self.delegate = delegate;
        }
        return self;
    }
    
    -(void) wantToFindApartment{
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:@"代理找房子" repeats:YES];
    }
    
    //实现私有方法
    -(void) startFindApartment:(NSTimer *)timer{
        NSString *info = [timer userInfo];
        NSLog(@"1");
        if ([self.delegate respondsToSelector:@selector(personWantToFindApartment:)]) {
            NSLog(@"%@",info);
            [self.delegate personWantToFindApartment:self];
        }
    }
    
    @end
    
    
    
    //Agent.h
    //代理
    #import <Foundation/Foundation.h>
    @protocol PersonProtocol;
    
    @interface Agent : NSObject<PersonProtocol>
    
    @end
    
    
    //Agent.m
    #import "Agent.h"
    #import "Person.h"
    
    @implementation Agent
    
    -(void)personWantToFindApartment:(Person *)person{
        NSLog(@"%s 代理帮忙找房子",__func__);
    }
    
    @end
    
    
    
    //main.m
    #import <Foundation/Foundation.h>
    #import "Person.h"
    #import "Agent.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Agent *a = [[Agent alloc] init];
            Person *p = [[Person alloc] initWithName:@"keen" withDelegate:a];
    
            [p wantToFindApartment];
    
            [[NSRunLoop currentRunLoop] run];
        }
    
    
        return 0;
    }
    View Code
  • 相关阅读:
    【转】编写高质量代码改善C#程序的157个建议——建议27:在查询中使用Lambda表达式
    python的reduce()函数
    SpringBoot中的配置文件
    23种设计模式概况性应用场景
    设计模式---合成模式
    tmpfs(转)
    Nginx配置文件(nginx.conf)配置详解
    Java设计模式------策略模式
    ubuntu下操作端口的方法
    ubuntu下安装ssh服务器方法
  • 原文地址:https://www.cnblogs.com/KeenLeung/p/5016895.html
Copyright © 2020-2023  润新知