第07天OC语言(05):代理设计模式练习及规范

  • 不要等到明天,明天太遥远,今天就行动。
须读:看完该文章你能做什么?

熟悉代理的使用

学习前:你必须会什么?(在这里我已经默认你具备C语言的基础了)

如何声明一个协议,协议的注意点


一、本章笔记
/*
 协议的编写规范:
 1.一般情况下,当前协议属于谁,我们就将协议定义到谁的头文件中
 
 2.协议的名称 一般以它 属于的那个类的类名开头,后面跟上protocol或者delegate
 StudentProtocol / StudentDelegate
 3.协议中的方法名称 一般以协议的名称protocol之前的作为开头
 studentFindHourse
 4.一般情况下 协议中的方法 会将 触发该协议的对象传递出去
 - (void)studentFindHourse:(Student *)student;
 5.一般情况下 一个类的代理属性的名称 叫 delegate;
 6.当某一个类 要成为另外一个类的代理的时候,
 一般情况下在.h中 用  @protocol 协议名称; 告诉当前类,这是一个协议
 在.m中用 #import 真正的导入一个协议的声明
 @protocol StudentProtocol;
 */

二、code
main.m
#pragma mark 05-代理设计模式练习及规范
#pragma mark - 代码
#import <Foundation/Foundation.h>
#pragma mark 类

#import "Student.h"
#import "LinkHome.h"
#import "LoveHome.h"
#pragma mark - main函数
int main(int argc, const char * argv[])
{
    /*
     用代理实现学生找房子,不具备找房子的能力
     所以学生可以招另一个对象 来帮它找房子,那么另一个对象就是学生的代理
     */
    
    
    Student *stu = [Student new];
    LinkHome *lj = [LinkHome new];
    stu.delegate = lj;
    
//    LoveHome *lh = [LoveHome new];
//    stu.delegate = lh;

    [stu findHourse];
    
    return 0;
}

Student
>>>.h
#import <Foundation/Foundation.h>
//#import "StudentProtocol.h"

/*
 协议的编写规范:
 1.一般情况下,当前协议属于谁,我们就将协议定义到谁的头文件中
 
 2.协议的名称 一般以它 属于的那个类的类名开头,后面跟上protocol或者delegate
    StudentProtocol / StudentDelegate
 3.协议中的方法名称 一般以协议的名称protocol之前的作为开头
    studentFindHourse
 4.一般情况下 协议中的方法 会将 触发该协议的对象传递出去
    - (void)studentFindHourse:(Student *)student;
 5.一般情况下 一个类的代理属性的名称 叫 delegate;
 6.当某一个类 要成为另外一个类的代理的时候,
    一般情况下在.h中 用  @protocol 协议名称; 告诉当前类,这是一个协议
    在.m中用 #import 真正的导入一个协议的声明
    @protocol StudentProtocol;
 */
@class Student;
@protocol StudentProtocol <NSObject>

// 帮学生找房子
- (void)studentFindHourse:(Student *)student;

@end
@interface Student : NSObject

@property (nonatomic,strong) id<StudentProtocol> delegate;

- (void)findHourse;

@end

>>>.m
#import "Student.h"

@implementation Student
- (void)findHourse
{
    NSLog(@"学生想找房子");
    // 通知代理帮他找房子
    if ([self.delegate respondsToSelector:@selector(studentFindHourse:)]) {
        [self.delegate studentFindHourse:self];
    }
}
@end

LinkHome
>>>.h
#import <Foundation/Foundation.h>
//#import "Student.h"
// 声明
@protocol StudentProtocol;
@interface LinkHome : NSObject<StudentProtocol>

@end

>>>.m
#import "LinkHome.h"
#import "Student.h"

@implementation LinkHome

- (void)studentFindHourse:(Student *)student
{
    NSLog(@"%s",__func__);
}


@end

LoveHome
>>>.h
#import <Foundation/Foundation.h>

@protocol StudentProtocol;
@interface LoveHome : NSObject<StudentProtocol>

@end

>>>.m
#import "LoveHome.h"
#import "Student.h"

@implementation LoveHome

- (void)studentFindHourse:(Student *)student
{
     NSLog(@"%s",__func__);
}

@end

StudentProtocol
>>>.h
#import <Foundation/Foundation.h>

@protocol StudentProtocol <NSObject>

// 帮学生找房子
- (void)studentFindHourse;

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 不要等到明天,明天太遥远,今天就行动。 须读:看完该文章你能做什么? 代理设计模式的基本概念代理设计模式的使用场合...
    liyuhong阅读 1,621评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,819评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,341评论 18 399
  • 不要等到明天,明天太遥远,今天就行动。 须读:看完该文章你能做什么? 协议的一些注意点,required关键字,o...
    liyuhong阅读 1,594评论 0 0
  • 大环境不景气、工作的苦闷、生活的压力,让每个人的“快乐指数”降到最低点。我们的快乐,难道只能任由外在的环境、别人的...
    雨霏姑娘阅读 2,806评论 0 1

友情链接更多精彩内容