Feature of Good Design (1) 优秀设计的特点(一)

Code reuse 代码复用

– Challenge: tight coupling between components, dependencies on concrete classes instead of interfaces, hardcoded operations

– Solution: design patterns

– 挑战:组件之间的紧密耦合、对具体类而不是接口的依赖、硬编码操作

– 解决方案:设计模式

• However, sometimes making components more complicated

然而,有时会使组件变得更加复杂

– Three levels of reuse: a piece of wisdom from Erich Gamma

– 三个层次的重用:Erich Gamma 的智慧

• Lowest: classes

最低:类

• Highest: frameworks

最高:框架

• Middle level: design patterns

中层:设计模式

Feature of Good Design (2) 优秀设计的特点(二)

Extensibility 可扩展性

Change is the only constant thing in a programmer’s life

– 变化是程序员一生中唯一不变的事情

• We understand the problem better once we start to solve it

一旦我们开始解决问题,我们就会更好地理解问题

• Something beyond your control has changed

你无法控制的事情已经发生了变化

• Objectives/requirements have changed

目标/要求已改变

– Prepare for possible future changes when designing an architecture

– 设计结构时为未来可能发生的变化做好准备

Good Design Principles (1) 良好的设计原则(1)

Encapsulate what varies 封装不同的内容

– Identify the aspects of your application that vary, and separate them from what stays the same

– 识别应用程序中不同的方面,并将它们与保持不变的部分区分开来

– Main goal: minimize the effect caused by changes

– 主要目标:最小化变化造成的影响

– Isolating program parts that vary in independent modules, protecting the rest

-隔离独立模块中不同的程序部分,保护其余部分

– Encapsulation on a method level

– 方法级别的封装

– Encapsulation on a class level

– 类级别的封装

Good Design Principles (2) 良好的设计原则(2)

Program to an interface, not an implementation 对接口编程,而不是实现

– In other words, depend on abstractions, not on concrete classes

– 换句话说,依赖于抽象,而不是具体的类

– The design is flexible enough if you can easily extend it without breaking existing code

– 如果您可以轻松地扩展它而无需破坏现有代码,那么该设计就足够灵活

– A possible approach

  1. Determine what exactly one object needs from the other: which methods does it execute?

    确定一个对象到底需要另一个对象什么:它执行哪些方法?
  2. Describe these methods in a new interface or abstract class.

    在新的接口或抽象类中描述这些方法。
  3. Make the class that is a dependency implement this interface.

    使作为依赖项的类实现此接口。
  4. Make the second class dependent on this interface rather than on the

    concrete class.

    使第二个类依赖于该接口而不是具体的类。

Good Design Principles (3) 良好的设计原则(3)

Favor composition over inheritance 优先考虑组合而不是继承

– Challenge of inheritance

– 继承的挑战

• A subclass can’t reduce the interface of the superclass

子类不能减少超类的接口

• When overriding methods you need to make sure that the new behavior is compatible with the base one

重写方法时,您需要确保新行为与基本行为兼容

• Inheritance breaks encapsulation of the superclass

继承破坏了超类的封装

• Subclasses are tightly coupled to superclasses

子类与超类紧密耦合

• Trying to reuse code through inheritance can lead to creating parallel inheritance hierarchies

尝试通过继承重用代码可能会导致创建并行继承层次结构

SOLID Principles

SOLID 1: Single Responsibility Principle 单一职责原则

A class should have just one reason to change

一个类应该只有一个改变的理由

– Try to make every class responsible for a single part of functionality,and make that responsibility entirely encapsulated

– 尝试让每个类负责一部分功能,并完全封装该职责

• Main goal: reducing complexity, and reducing risks

主要目标:降低复杂性并降低风险

SOLID 2: Open/Closed Principle 开闭原理

  • Classes should be open for extension but closed for modification

    类应该对扩展开放,但对修改关闭

    – Keep existing code from breaking when implementing new features

    – 在实现新功能时防止现有代码被破坏
  • A class is open if it can be extended by subclasses

    如果一个类可以通过子类扩展,则该类是开放的
  • A class is closed if it is 100% ready to be used by others

    如果一个类 100% 可供其他人使用,则该类已关闭
  • A class can be both open (for extension) and closed (for modification) at the same time

    一个类可以同时开放(用于扩展)和封闭(用于修改)

    – Not necessary to be applied for all changes

    – 无需应用所有变更

SOLID 3:Liskov Substitution Principle 里氏替换原理

  • When extending a class, ensure the capability of passing objects of the subclass in place of objects of the parent class without breaking the client code

    扩展类时,保证能够通过子类的对象代替父类的对象,而不破坏客户端代码

  • The subclass should remain compatible with the behavior of the superclass

    子类应该与超类的行为保持兼容

    – When overriding a method, extend the base behavior rather than replacing it with something else entirely

    重写方法时,扩展基本行为而不是完全用其他东西替换它

  • Especially critical when developing libraries and frameworks

    在开发库和框架时尤其重要

  • A set of checks

    一组检查

    • (a) Parameter types in a method of a subclass should match or be more abstract than parameter types in the method of the superclass.

      子类方法中的参数类型应与超类方法中的参数类型匹配或更抽象。
    • (b) The return type in a method of a subclass should match or be a subtype of the return type in the method of the superclass.

      子类方法中的返回类型应该与超类方法中的返回类型匹配或者是其子类型。
    • (c) Types of exceptions should match or be subtypes of the ones that the base method is already able to throw.

      异常类型应该与基方法已经能够抛出的异常类型匹配或者是其子类型。
    • (d) A subclass shouldn’t strengthen pre-conditions.

      子类不应强化先决条件
    • (e) A subclass shouldn’t weaken post-conditions.

      子类不应削弱后置条件
    • (f) Invariants of a superclass must be preserved (least formal rule of all)

      必须保留超类的不变量(至少所有正式规则)
    • (g) A subclass shouldn’t change values of private fields of the superclass.

      子类不应更改超类的私有字段的值。

SOLID 4: Interface Segregation Principle 接口隔离原则

Clients shouldn’t be forced to depend on methods they do not use

不应强迫实现代码依赖他们不使用的方法

– Make interfaces narrow enough that client classes don’t have to implement

behaviors they don’t need

– 使接口足够窄,以便实现代码类不必实现它们不需要的行为

– Break down “fat” interfaces into more granular and specific ones

– 将“胖”接口分解为更细粒度和更具体的接口

SOLID 5: Dependency Inversion Principle 依赖倒置原则

  • High-level classes shouldn’t depend on low-level classes, and both should depend on abstractions

    高级别的类不应该依赖于低级别的类,并且两者都应该依赖于抽象
  • Problem: business logic classes tend to become dependent on primitive low-level classes

    问题:业务逻辑类往往会依赖于原始的低级类
  • Suggestion: changing the direction of dependency

    建议:改变依赖方向
  • Approach
    • Describe interfaces for low-level operations that high-level classes rely on, preferably in business terms

      描述高级类所依赖的低级操作的接口,最好用业务术语
    • Make high-level classes dependent on the interfaces, resulting in a softer dependency

      使高级类依赖于接口,从而产生更软的依赖关系
    • Low-level classes implement the interfaces, dependent on the business logic level

      低级类实现接口,依赖于业务逻辑级别

学习高校课程-软件设计模式-软件设计原则(lec2)的更多相关文章

  1. 敏捷软件开发_设计原则<三>

    敏捷软件开发_设计原则 单一职责原则(single responsibilities principle,SRP) 原理:一个类应该只有一个变化 分离职责:如果不耦合的职责那么很简单,如果两个职责耦合 ...

  2. 设计模式学习(二):面向对象设计原则与UML类图

    一.UML类图和面向对象设计原则简介 在学习设计模式之前,需要找我一些预备知识,主要包括UML类图和面向对象设计原则. UML类图可用于描述每一个设计模式的结构以及对模式实例进行说明,而模式结构又是设 ...

  3. JavaScript设计模式之设计原则

    何为设计 即按照哪一种思路或者标准来实现功能,功能相同,可以有不同的设计方案来实现 伴随着需求的增加,设计的作用就会体现出来,一般的APP每天都在变化,更新很快,需求不断在增加,如果设计的不好,后面很 ...

  4. Java设计模式 —— 面向对象设计原则

    1 设计模式概述 1.1 设计模式的定义与分类 设计模式的定义 Design patterns are descriptions of communicating objects and classe ...

  5. java设计模式和设计原则

    一.创建型模式 1.抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类.2.生成器模式(Builder patter ...

  6. 设计模式之设计原则 C#

    成为一名资深架构师首先要懂设计模式,在懂之前,要清楚设计原则,原来我就吃过这个亏,很久以前有人问我设计原则,我是一头茫然,不是只有设计模式吗?且不知设计原则就像是写书法一样,楷体就是方正,竖道有垂露等 ...

  7. 设计模式——<面向对象设计原则以及23种设计模式分类>

    一.面向对象八大设计原则: 1.依赖倒置原则(DIP) 高层模块(稳定)不应该依赖于低层模块(变化),二者都应该依赖于抽象(稳定) . 抽象(稳定)不应该依赖于实现细节(变化) ,实现细节应该依赖于抽 ...

  8. 设计模式学习系列(一)——IOC设计原则

    参考转载自IoC 之 2.1 IoC基础 ——跟我学Spring3

  9. JAVA设计模式及其设计原则

    设计模式: 设计模式是一套被反复使用的.多数人知晓的.经过分类编目的.代码设计经验的总结. 单例模式:在一个jvm虚拟机,要创建的对象控制成独一份:举例:统计单台虚拟机内的用户在线数 package ...

  10. 年终知识分享——UML、设计模式、设计原则

                                                                                                        ...

随机推荐

  1. 用户数据报协议UDP

    UDP的首部格式如下: (1) 源端口,源端口号.在需要对方回信时选用.不需要时可用全0. ⑵目的端口,目的端口号.这在终点交付报文时必须使用. ⑶长度,UDP用户数据报的长度,其最小值是8(仅有首部 ...

  2. oeasy教您玩转vim - 89 - # 高亮细节Highlight

    ​ 高亮细节 highight 回忆 这个自动命令 autocmd 还是很方便的 打开时.保存时就会有自动执行的操作 自动命令有这么几大元素 {event} 触发事件 {pattern} 文件模式 { ...

  3. 学习笔记--Java构造方法

    Java构造方法 关于构造方法 构造方法又被称作:构造函数/构造器/Constructor 语法结构: [修饰符列表] 构造方法名(形式参数列表){ 构造方法体; } 对比普通方法语法结构 [修饰符列 ...

  4. Known框架实战演练——进销存基础数据

    本文介绍如何实现进销存管理系统的基础数据模块,基础数据模块包括商品信息.供应商管理和客户管理3个菜单页面.供应商和客户字段相同,因此可共用一个页面组件类. 项目代码:JxcLite 开源地址: htt ...

  5. 假期小结2hadoop环境配置

     记录一下hadoop配置 安装Java Development Kit(JDK):Hadoop是用Java编写的,所以首先需要安装JDK.可以从Oracle下载JDK,并按照安装说明进行安装. 下载 ...

  6. 【DataBase】MySQL 28 流程控制

    一.分支结构 1.IF函数 语法: IF(表达式1, 表达式2, 表达式3) 类似三元运算符,表达式1返回True Or False True执行表达式2,False执行表达式3 IF实现多分枝结构 ...

  7. 【Vue】Re05 操作数组的API

    一.响应式处理的操作: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  8. 【Hibernate】Re03 注解方式实现

    使用JPA规范提供的注解即可实现,这样的好处是不需要配置Entity.hbm.xml文件了 但是考虑到多表查询的情况,还是会有xml配置的需要. 一.常用的JPA注解: 1.public @inter ...

  9. mujoco安装报错:mujoco_py/gl/eglplatform.h:99:10: fatal error: X11/Xlib.h: 没有那个文件或目录

    安装mujoco报错: mujoco_py/gl/eglplatform.h:99:10: fatal error: X11/Xlib.h: 没有那个文件或目录 修复方法: sudo apt inst ...

  10. pycuda学习过程中的一些发现,cuda函数的初始化要在cuda内存空间初始化之后,否则会报错

    参考: https://www.cnblogs.com/devilmaycry812839668/p/15348610.html 最近在看WarpDrive的代码,其中cuda上运行的代码是使用pyc ...