设计模式第七堂课

1. OCP (Open Close Principle)

A. Software entities like classes, modules and functions should be open for extension but closed for modifications

B. The design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code

C. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged

1) usage

Being applied in those area which are most likely to be changed

2) liabilities

Making a flexible design involves additional time and effort spent for it and itintroduce new level of abstraction increasing the complexity of the code


Figure 1 A Design of Violating OCP


Figure 2 A Design of Following OCP


2. DIP (Dependency Inversion Principle) / Hollywood Principle
A. High-level modules should not depend on low-level modules. Both should 
depend on abstractions

B. Abstractions should not depend on details. Details should depend on abstractions

1) liabilities

Using this principle implies an increased effort, will result in more classes andinterfaces to maintain


Figure 3 A Design of Violating DIP


Figure 4 A Design of Following DIP


3. ISP (Interface Segregation Principle)
A. Clients should not be forced to depend upon interfaces that they don't use

1) liabilities

Requiring additional time and effort spent to apply it during the design timeand increase the complexity of code


Figure 5 A Design of Violating ISP


Figure 6 A Design of Following ISP


4. SRP (Single Responsibility Principle)
A. A class should have only one reason to change

1) liabilities 

Increasing the complexity of code


Figure 7 Sample SRP

5. LSP (Liskov’s Substitution Principle)
A. Derived types must be completely substitutable for their base types
B. This principle is just an extension of the Open Close Principle
C. Making sure that new derived classes are extending the base classes without 
changing their behavior


Figure 8 A Design of Violating LSP


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值