OC中的设计模式(代理设计模式)(类目)
来源:互联网 发布:国外论文一手数据 编辑:程序博客网 时间:2024/05/19 03:45
注意点:类目里面无法添加性的实例变量
在类目中覆盖现有类的方法,会引起super消息的断链。
类目的使用方式有多种
1、直接写在类中。
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
NSString *_name;
}
@property int age;
@property NSString *name;
- (void)show;
@end
@interface Person (Life)
- (void)eat;
- (void)run;
- (void)sleep;
@end
@interface Person (Create)
- (id)personWithName:(NSString *)name;
+ (id)personWithName:(NSString *)name withAge:(int)age;
- (id)initWithName:(NSString *)name;
- (id)initWithName:(NSString *)name withAge:(int)age;
---------------------------------------------------------------------------------------------
#import "Person.h"
@implementation Person
- (void)show{
NSLog(@"我是对象的方法,不是类目中的方法。%@,%d",_name,_age);
}
@end
@implementation Person(Life)
- (void)eat{
NSLog(@"开始吃饭");
}
- (void)run{
NSLog(@"开始跑了");
}
- (void)sleep{
NSLog(@"睡觉了");
}
@end
@implementation Person (Create)
- (id)personWithName:(NSString *)name{
Person *p = [[Person alloc]init];
p.name = name;
return p;
}
+ (id)personWithName:(NSString *)name withAge:(int)age{
Person *p = [[Person alloc]init];
p.name = name;
p.age = age;
return p;
}
- (id)initWithName:(NSString *)name{
if(self = [super init]){
self.name = name;
}
return self;
}
- (id)initWithName:(NSString *)name withAge:(int)age{
if(self = [super init]){
self.name = name;
self.age =age;
}
return self;
}
@end
----------------------------------------------------------------------------------#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
Person *p = [[Person alloc] initWithName:@"merray" withAge:15];
[p show];
[p eat];
[p run];
return 0;
}
-------------------------------------------------------------------------------------2、定义成单独的文件
#import "Person.h"
@interface Person (PersonCategory)
- (id)personWithName:(NSString *)name;
+ (id)personWithName:(NSString *)name andAge:(int)age;
- (id)initWithAge:(int)age andName:(NSString *)name;
-(id)initWithName:(NSString *)name;
@end
#import "Person+PersonCategory.h"
@implementation Person (PersonCategory)
- (id)personWithName:(NSString *)name{
Person *p =[Person new];
p.name =name;
return p;
}
+ (id)personWithName:(NSString *)name andAge:(int)age{
Person *p = [Person new];
p.name = name;
p.age = age;
return p;
}
- (id)initWithAge:(int)age andName:(NSString *)name{
if(self = [super init]){
self.name = name;
self.age = age;
}
return self;
}
-(id)initWithName:(NSString *)name{
if(self = [super init]){
self.name =name;
}
return self;
}
@end
---------------------------------------------------------------------------#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
NSString *_name;
}
@property int age;
@property NSString *name;
- (void)show;
@end
@interface Person (Life)
- (void)eat;
- (void)run;
- (void)sleep;
@end
----------------------------------------------------------------#import "Person.h"
@implementation Person
- (void)show{
NSLog(@"我是对象的方法,不是类目中的方法。%@,%d",_name,_age);
}
@end
@implementation Person(Life)
- (void)eat{
NSLog(@"开始吃饭");
}
- (void)run{
NSLog(@"开始跑了");
}
- (void)sleep{
NSLog(@"睡觉了");
}
@end
--------------------------------------------------------------#import <Foundation/Foundation.h>
#import "Person.h"
#import "Person+PersonCategory.h"
int main(int argc, const char * argv[]) {
Person *p = [[Person alloc] initWithAge:18 andName:@"dedede"];
[p show];
[p eat];
[p run];
return 0;
}
很简单,实质就是方法的添加。
代理设计模式
代理指的是一个对象提供机会对另一个对象的行为发生变化时做出的反应。
实质就是两个对象协调起来一起解决问题。
#import <Foundation/Foundation.h>
@protocol helloProtocol <NSObject>
- (void)requiredMethod;
@optional
- (void)optionalMethod1;
- (void)optionalMethod2;
@陈大哥end
----------------------------------------------------------------
#import <Foundation/Foundation.h>
#import "helloProtocol.h"
@interface Person : NSObject<helloProtocol>
- (void)requiredMethod;
- (void)optionalMethod1;
- (void)optionalMethod2;
@end
----------------------------------------------------------------
#import "Person.h"
@implementation Person
- (void)requiredMethod{
NSLog(@"这个是一个必须实现的方法");
}
- (void)optionalMethod1{
NSLog(@"这个是一个可选实现的方法1");
}
- (void)optionalMethod2{
NSLog(@"这个是一个可选实现的方法2");
}
@end
----------------------------------------------------------------
int main(int argc, const char * argv[]) {
Person *p = [Person new];
[p requiredMethod];
[p optionalMethod1];
[p optionalMethod2];
return 0;
}
-----------------------------------------------------------------
#import <Foundation/Foundation.h>
@class Person;
typedef enum _CZHouseRent{
CZHouseRentHighRent,
CZHouseRentMidRent,
CZHouseRentLowRent
}CZHouseRent;
@protocol FindApartMent <NSObject>
- (CZHouseRent)FindApartMent:(Person *)person;
@陈大哥end
----------------------------------------------------------------
#import <Foundation/Foundation.h>
#import "FindApartMent.h"
@interface Agent : NSObject<FindApartMent>
- (CZHouseRent)FindApartMent:(Person *)person;
@end
----------------------------------------------------------------
#import "Agent.h"
@implementation Agent
static int count =1;
- (CZHouseRent)FindApartMent:(Person *)person{
NSLog(@"find");
CZHouseRent rent;
if(count == 1){
NSLog(@"价格有点高");
rent = CZHouseRentHighRent;
}else if(count == 2){
NSLog(@"价格还好");
rent = CZHouseRentMidRent;
}else{
NSLog(@"价格比较低");
rent = CZHouseRentLowRent;
}
count++;
return rent;
}
@end
---------------------------------------------------
#import <Foundation/Foundation.h>
#import "FindApartMent.h"
@interface Person : NSObject
{
@private
NSString *_name;
id<FindApartMent> _delegate;//这个成员变量具备协议里面的方法
int _houseRent;
}
@property NSString *name;
@property id<FindApartMent> delegate;
- (id)initWithName:(NSString *)name andDelegate:(id<FindApartMent>)delegate;
- (void)wantoFindApartment;
@end
----------------------------------------------------
#import "Person.h"
@interface Person()
- (void)startFindApartment:(NSTimer *)time;//定义私有方法
@end
@implementation Person
- (id)initWithName:(NSString *)name andDelegate:(id<FindApartMent>)delegate{
if(self = [super init]){
self.name = name;
self.delegate =delegate;//设置代理
}
return self;
}
- (void)wantoFindApartment{
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:nil repeats:YES];
//通过指定定时器,感觉跟跳转差不错。跟java中线程的那个版块一样。每隔多少秒执行一次,指定的函数,并且指定调用函数的对象,指定被调的函数。
}
- (void)startFindApartment:(NSTimer *)time{
CZHouseRent rent;
if([self.delegate respondsToSelector:@selector(FindApartMent:)]){
rent = [self.delegate FindApartMent:self];
}
if(rent == CZHouseRentHighRent){
NSLog(@"太贵了");
}else if(rent == CZHouseRentMidRent){
NSLog(@"我再看看");
}else{
NSLog(@"感觉还行");
[time invalidate];
}
}
@end
---------------------------------------------------------------
#import <Foundation/Foundation.h>
#import "Agent.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
Agent *a = [Agent new];
Person *p = [[Person alloc]initWithName:@"jack" andDelegate:a];
[p wantoFindApartment];
// [[NSRunLoop currentRunLoop]run];
NSDate *date = [NSDate new];
[[NSRunLoop currentRunLoop] runUntilDate:[date dateByAddingTimeInterval:6]];//
return 0;
}
- OC中的设计模式(代理设计模式)(类目)
- OC中的代理设计模式
- OC-代理设计模式
- OC设计模式之代理(委托)设计模式
- oc中的设计模式
- OC---protocol代理设计模式
- OC中代理设计模式
- OC设计模式--代理模式
- 设计模式中的代理模式
- OC学习——OC中的@protocol(@required、@optional)、代理设计模式
- oc中的工厂设计模式
- OC基础—代理模式设计
- OC-中介找房-代理设计模式
- OC视频笔记-代理设计模式
- OC-中介找房-代理设计模式
- [黑马程序员][OC]代理设计模式
- OC基础—代理设计模式
- OC循环渐进:代理设计模式
- Power Designer怎样批量修改数据类型
- Object-C学习记录(个人学习日志)
- ZOJ_3791 An Easy Game[ 二维dp ]
- AndroidManifest.xml文件解析
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
- OC中的设计模式(代理设计模式)(类目)
- JAVA设计模式之 迭代器模式【Iterator Pattern】
- OC中(简单)自定义时间|操作文件部分一(文件的拷贝|归档与解归档)
- Android中图像变换Matrix的原理
- GCD的简单实用
- 操作xml|json|以及框架的简单介绍
- ch5.jsp基本语法 -- 用户登录页面
- UITableView的重用机制原理
- poj 2376 区间覆盖