两种设计模式(解藕)

来源:互联网 发布:辩证看待人工智能 编辑:程序博客网 时间:2024/05/17 17:43

1. target/action 设计模式

   这种设计模式 是为了降低 程序之间的耦合程度

   比如 :我创建了3个轻拍View 当我轻拍 不同的视图的时候(一个视图改变颜色, 一个视图改变位置, 一个视图改变大小)此时想到的第一种做法就是 在tapView 中 通过 tag进行if判断 如果tag=100 执行 改变颜色的方法 如果tag=101 就执行其他方法 等等   使用这一种方法 当我要再添加视图的时候 发现我要在控制器中创建视图,为它添加tag,然后在转回到 tapView中添加 判断与方法。     这样的形式就回是程序的耦合度很低 

   下面使用 target/action的方法 来降低耦合度

  TapView.h中

#import <UIKit/UIKit.h>


@interface TapView :UIView

{

    id _target;//执行 action的对象

    SEL _action;//target要执行的行为

}


// target action赋值

- (void)setTarget:(id)target action:(SEL)action;


@end



TapView.m中

#import "TapView.h"


@implementation TapView



- (void)dealloc

{

    [superdealloc];

}


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

    }

    return self;

}


//当触摸事件触发时 让target 执行 action 方法

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    //点击事件结束时随机改变背景颜色

    [selfchangeColor];

    [_target performSelector:_actionwithObject:nil ];

    //这个方法中 withObject:表示 _action需要传递的参数一般为当前对象 self

    //_ action这个方法不需要参数 withObject:设置为 self时这个 self并没有传递给 action

}


// target action赋值

//不带星号的 一般情况下只需要直接赋值 不需要 retain或者 copy

- (void)setTarget:(id)target action:(SEL)action

{

   _target=target;

   _action=action;

}


//随机改变当前视图的颜色

//考虑这个功能用的很多的时候可以创建一个 UIColor的分类实现获取随机颜色的UIColor对象

//这样就不用在使用此功能时都要写这么多代码

- (void)changeColor

{

    //随机获取 RGB色值

   CGFloat redValue=arc4random()%256/255.0;

   CGFloat greenValue=arc4random()%256/255.0;

   CGFloat blueValue=arc4random()%256/255.0;

    //设置当前视图的背景颜色

    self.backgroundColor =[UIColorcolorWithRed:redValuegreen:greenValueblue:blueValuealpha:1];

    

}


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/

@end



RootViewController.m中


#import "RootViewController.h"

#import "TapView.h"


@interface RootViewController ()


@end


@implementation RootViewController



- (void)dealloc

{

    [superdealloc];

}


- (void)aa

{

    NSLog(@"Hello World!");

    

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    //创建tapView类的对象作为 控制器的子视图

    TapView *tapView=[[TapViewalloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    tapView.backgroundColor =[UIColorpurpleColor];

    [tapViewsetTarget:selfaction:@selector(aa)];//这里就相当于 给TapView的对象 赋了一个 target 和 action的初值

    

    [self.viewaddSubview:tapView];

    [tapViewrelease];

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end




2. 代理 delegate 设计模式

使用 delegate模式与 targt/action设计模式的区别在于 delegate 模式将代理要实现的方法固定了. target/action 完全将要进行的操作交给 target来定义

任何一个代理方法 都至少有一个参数(即对象本身)

两种模式的区别

1.delegate是一堆事件  target/action 是需要几个事件,就写几个

2.调用方式不同

3.原理相同



TapView.h

#import <UIKit/UIKit.h>

@class TapView;


//使用 delegate 模式与 targt/action 设计模式的区别在于 delegate模式将代理要实现的方法固定了. target/action 完全将要进行的操作交给 target来定义

//任何一个代理方法都至少有一个参数(即对象本身)

//两种模式的区别

//1.delegate 是一堆事件  target/action 是需要几个事件,就写几个

//2.调用方式不同

//3.原理相同



//设置代理协议

@protocol TapViewDelegate <NSObject>


//tapView开始点击

- (void)tapViewBeganTouch:(TapView *)tapView;

//tapView 结束点击

- (void)tapVIewEndedTouch:(TapView *)tapView;


@optional

//tapView 正在移动

- (void)tapViewMoved:(TapView *)tapView;


@end


@interface TapView :UIView


//设置代理对象

@property (nonatomic,assign)id<TapViewDelegate> delegate;

//@property (nonatomic,assign) SEL action;


@end



TapView.m

#import "TapView.h"


@implementation TapView


- (void)dealloc

{

    [superdealloc];

}


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

    }

    return self;

}


//在触发事件中让代理去 执行代理的实现方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [_delegate tapViewBeganTouch:self];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    [_delegate tapViewMoved:self];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    [_delegate tapVIewEndedTouch:self];

   // [_delegate performSelector:_action withObject:self];

}



/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/


@end



RootViewController.h文件(这里作为代理)

#import <UIKit/UIKit.h>

#import "TapView.h"


//代理要实现代理协议

@interface RootViewController :UIViewController<TapViewDelegate>


@end


#import "RootViewController.h"

#import "TapView.h"


@interface RootViewController ()


@end


RootViewController.m


@implementation RootViewController


- (void)dealloc

{

    [superdealloc];

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    TapView *tapView=[[TapViewalloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    tapView.backgroundColor=[UIColorblueColor];

    tapView.delegate=self;  //初始化时为 tapView的对象指定代理对象

   // tapView.action=@selector(aa:);


    

    [self.viewaddSubview:tapView];

    [tapViewrelease];

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




- (void)aa:(TapView *)tapView

{

    tapView.backgroundColor=[UIColorredColor];

    NSLog(@"我是 delegate设计模式");


}

//实现代理方法

- (void)tapViewBeganTouch:(TapView *)tapView

{

   NSLog(@"开始");

}


- (void)tapViewMoved:(TapView *)tapView

{

   NSLog(@"移动 ");

}


- (void)tapVIewEndedTouch:(TapView *)tapView

{

   NSLog(@"结束");

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





0 0