面向对象设计基本原则

来源:互联网 发布:淘宝裁决之镰 编辑:程序博客网 时间:2024/05/16 20:10
内容:
一、什么是设计模式
二、面向对象设计的基本原则

什么是设计模式?
》设计模式是为解决特定场景下的问题而定制的解决方案
》设计模式是经时间证明为有效的,对特定面向对象设计问题主要方法的抽象
》总之就是为解决一类面向对象的问题而总结的方案

设计原则
》单一职责原则(SRP,Single Responsibility Principle)
》开闭原则(OCP,Open Close Principle)
》里氏替换原则 (LSP,Liskov Substitution Principle)
》接口隔离原则(ISP,Interface Segregation Principle)
》依赖倒置原则(DIP,Dependence Inversion Principle)
》迪米特法则(LoF,Low of Demeter)

单一职责原则

》对于一个类,应该有且仅有一个引起它变化的原因
》There should never be more than one reason for a class to change.
反例:

- (void)viewDidLoad {      [superviewDidLoad];     inta =100;intb =200;NSString*result = [NSStringstringWithFormat:@"%d", a + b];
 self.resultLabel.text= result;

修改后:
将计算方法抽离到一个单独的类中,方便更改算法和显示方法

- (void)viewDidLoad {
      [superviewDidLoad];

inta =100;
intb =200;

Caculator*caculator = [[Caculatoralloc]init];

NSString*result = [NSStringstringWithFormat:@"%d", [caculator     caculateA:ab:b]];!
self.resultLabel.text= result;
}

里氏替换原则

》在软件里,把父类替换为它的子类,程序的行为没有变化
》Inheritance should ensure that any property proved about super type objects apse holds for subtype objects. ——Barbarn   Liskov
示例:

UIView*redView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];redView.backgroundColor= [UIColorredColor]; [self.viewaddSubview:redView]; 

                                      


UIView*redView = [[UIControlalloc]initWithFrame:CGRectMake(100,100,100,100)];redView.backgroundColor= [UIColorredColor]; [self.viewaddSubview:redView]; 

UML图示:



接口隔离原则

》客户端不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。也就是说接口里的方法应该尽量的少
》Clients should not be forced to depend upon interfaces that they don’t use.
》The dependency of one class to another one should depend on the smallest possible interface.

UITableView*tableView = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];tableView.dataSource=self; tableView.delegate=self;
[self.viewaddSubview:tableView]; 


UML图示:

依赖倒置原则

》上层模块不应该依赖于下层模块。两者都应该依赖于抽象。抽象不应该依赖于细节,细节应该依赖于抽象。
》High level modules should not depend upon low level modules.Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.
》面向接口编程。

反例:
》依赖于Dog类。

@class Dog;
@protocolBiteDelegate <NSObject>
- (void)bite;
 @end

@interfacePerson : NSObject!
@property(nonatomic,weak) Dog *delegate;
@end 

示例:
》依赖于BiteDelegate协议。
@class Dog;
@protocolBiteDelegate <NSObject]]>
- (void)bite;
@end

@interfacePerson :NSObject@property(nonatomic,weak)id<BiteDelegate> delegate;
@end 

UML图示:


迪米特法则

》如果两个类不必彼此直接通信,它们就不应当发生直接的相互作用。如果其中一个类需要调用另外一个类的方法,可以通过第三者转发该调用
》在类的结构设计上,每个类都应当尽量降低成员的访问权限
》也叫最少知识原则(LKP,Least Knowledge Principle)或者只和直接朋友交流(Only talk to your immediate friends)

反例:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath {
     staticNSString*identifier =@"cell";     UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
     if(cell ==nil) {
           cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
           UIImageView*imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,0,100,44)];
          imageView.tag=100;
          [cell.contentViewaddSubview:imageView];
     }

      UIImageView*imageView = (UIImageView*)[cell.contentViewviewWithTag:100];
     imageView.image= [UIImageimageNamed:@"hello.png"];     returncell;
} 



示例:

@interfaceDVITableViewCell :UITableViewCell
 @property(nonatomic,strong)UIImage*image;
@end 



- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath {
     staticNSString*identifier =@"cell”;
     DVITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
     if(cell ==nil) {
          cell = [[DVITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
     }
     cell.image= [UIImageimageNamed:@"hello.png”];
     returncell;



UML图示:


开闭原则

》软件系统中的各种组件,如模块、类和方法等,应该在不修改现有代码的基础上引入新功能。即对修改封闭(Close),而对扩展开放(Open)。
》Software entities should be open for extension, but closed for modification. —— Bertrand Meyer
0 0
原创粉丝点击