其他面向对象设计原则2
   能用组合的地方,不要用继承 Favor object composition over class inheritance
6.1 代码重用的两种方式

   能用组合的地方不要用继承 Favor object composition over class inheritance

   这句话是说,在OO技术里,有两种基本类型的代码重用: 组合、继承

    What this statement says is that there are basically two ways to reuse code in OO
6.2 继承的特点

   类的继承 Class inheritance

     子类获得父系“全部功能”,“稍微”调整一下,比如覆盖实现几个方法

      You get the “whole packet” and “tweak a bit” by overriding a single or few methods

     既快又容易 Fast and easy

     代码实现时明确展示,由编程语言加以支持

      Explicit in the code, supported by language

         (you can directly write “extends”)

   继承存在的副作用...

     “继承打破了封装性 inheritance breaks encapsulation”

       导致父类与子类之间高度耦合 leading to high coupling between super-and subclass 

     1) 继承的代码是静态/编译-时绑定的 Inheritance is a static/compile-time binding

       今后改变行为的唯一方法是 edit-compile-debug-edit-compile-debug
     2) 客户需要购买整个软件包 You have to buy the full package !

       you cannot access only part of the behaviour of the superclass – you get it all

       For example

         A nice C++ String implementation in a Library was compiled to have dependencies

          with 2MB other parts of the library

     3)父类定义了许多硬性的规定 Superclasses define (part) of physical representation 例如

       父类用了列表数据结构,而子类最好是用树结构

        superclass uses a List data structure, but subclass would be much more efficient using a

        tree structure

       子类经常直接使用Public和Protected数据成员,一旦父类修改了这些数据,将会影 响全体子类

        subclasses usually simple refer to public and protected data members, which means, changing implementation details

         in superclass effect all subclasses !!!

           yes – we know :

           review all subclasses

           retest all subclasses

       不得不了解父类的代码以求真正理解(相比组合,只要了解接口)
   关于类的继承

     可以使用继承,但设计师 必须确信使用继承是很好 的 解 决 方 案 时 才 使 用

      Inheritance is OK but you must use it for what it handles really nice

     以泛化层次的方式,对概 念进行建模 Modelling of concepts in a generalisation hierarchy

       不是角色

       不是行为

       不能随意
6.3 组合的特点

   对象组合 Object composition

     没有打破封装性 No breaking encapsulation !!!

   A) 对象组合是一种动态/运行时绑定 object composition is a dynamic/run-time binding

     在运行时切换对象引用,就可以改变行为 you may change behaviour at run-time by “switching” an object reference

   B) 整体与部分之间只有接口边界关联,耦合较低 Loose coupling by respecting the interface borders

     对私有数据不存在“窥视与拨弄”No peeking and poking in semiprivate data members!

     不存在大量无用代码一类的负担 No heavy luggage in the form of data structures that are useless
   C) 各部分的职责明确 Clear division of responsibilities !!!

     每个对象清晰地集中在少量的任务上 each object is clearly focused on a single/few tasks

     容易理解 easier to understand、容易维护 easier to maintain

     只要阅读接口,就可以了解系统(至少理论上是如此)understanding by reading interfaces only

     容易独立测试 easier to test in isolation  使用得当,将导致更可靠的设计

     每个类依然“苗条”each class remains small

       避免的“巨无霸”这种反模式的现象,即一个类似乎完成了几乎全部的功能…

        avoiding the “Blob” anti-pattern where one class seems to “suck in” all functionality...

     重用的概率大增!larger potential for reuse in other contexts

转载于:https://www.cnblogs.com/mayZhou/p/10550058.html

7.11 其他面向对象设计原则2:能用组合的地方,不要用继承相关推荐

  1. 面向对象设计原则 (快速记忆11个原则)

    (1)单一责任原则(Single Responsily Pinciple, SRP). 就个类而言, 应该仅有个引起它变化的原因.即,当需要修改某个类的时候原因有且只有一个,让一个类只做种类型责任. ...

  2. uml图中的各种箭头_设计模式学习笔记(二):UML与面向对象设计原则

    1 UML 1.1 UML UML(Unified Modeling Language)是统一建模语言,1997年11月UML1.1版本提交给OMG并正式通过,成为建模语言的个那个也标准.2003年6 ...

  3. 技术图文:01 面向对象设计原则

    01 面向对象设计原则 知识结构: 一碟开胃的小菜 小菜今年计算机专业大四了,学了不少软件开发方面的东西,也学着编了些小程序,踌躇满志,一心要找一个好单位.当投递了无数简历后,终于收到了一个单位的面试 ...

  4. 面向对象设计原则_面向对象的设计原则

    面向对象设计原则 Programming is fun until you have to incorporate a new requirement that changes the whole d ...

  5. 《设计模式:可复用面向对象软件的基础》——面向对象设计原则、创建型模式(笔记)

    文章目录 二.面向对象设计原则(补充) 2.1 重新认识面向对象 2.2 面向对象设计原则 2.2.1 依赖倒置原则(DIP) 2.2.2 开放封闭原则(OCP) 2.2.3 单一职责原则(SRP) ...

  6. 01.面向对象设计原则实验

    面向对象设计原则实验 题目1:某绘图软件提供了多种大小不同的笔(Pen),并且可以给画笔指定不同颜色,某设计人员针对画笔的结构设计了如图1-1所示的初始类图.通过仔细分析,设计人员发现该类图存在非严重 ...

  7. 设计模式之实验一:面向对象设计原则实验

    实验项目一:面向对象设计原则实验 (1)实验目的与原理 ①深入理解和掌握面向对象设计原则. ②能运用面向对象设计原则对系统进行重构. ③能绘制系统结构图(类图) (2)实验内容与步骤 ①某绘图软件提供 ...

  8. 面向对象设计原则与23种设计模式

    面向对象概述 1.万物皆对象(Java反射原理-都是借助java.lang.class这个对象的属性.定义等实现) 2.面向对象的特征:封装(对象之间的隔离性.对象内部的属性封装).继承(类的重用,耦 ...

  9. 7.12 其他面向对象设计原则3: 依赖倒置原则DIP

    其他面向对象设计原则3: 依赖倒置原则DIP  The Dependency Inversion Principle 7.1 依赖倒置原则DIP The Dependency Inversion P ...

最新文章

  1. ZJU-java进阶笔记 第三周(对象容器)
  2. 再有人问你Java内存模型是什么,就把这篇文章发给他
  3. 汇总 Linux下获取详细硬件信息的工具:Dmidecode命令详解
  4. navicat for mysql注册码:NAVN-LNXG-XHHX-5NOO
  5. python 内置模块:collections
  6. java无法实例化类型_java – 无法实例化泛型中的类型
  7. 记一次Weblogic连接池泄露的修复过程
  8. java main函数_都知道Java程序的入口方法是main,那你知道为什么是main方法吗?
  9. 计算机d盘无法格式化,电脑D盘无法格式化怎么办 D盘无法格式化问题解决办法...
  10. SAP Spartacus ProductConnector和ProductService实现
  11. 线性查找法java代码_Java线性查找和二分查找
  12. SAP MM ‘Pricing Date Control‘字段的理解
  13. webpack4开始使用
  14. 剑指offer答案python_剑指offer(python)(未完)
  15. Minitab正态能力分析算法资源合集
  16. 一份好的商业计划书该怎么写
  17. 常用的 T-SQL 语言
  18. 一次 Kafka 导致的 Sentry 无法处理 MiniDump 问题分析
  19. 【波导】——理解群速度和相速度
  20. enum ordinal java_Java Enum ordinal()方法

热门文章

  1. SpringBoot+Vue.js实现大文件分片上传、断点续传与极速秒传
  2. js判断时间是早上还是下午_测血糖别在下午,测空腹血糖早上几点最准确?
  3. properties加载的几种方式
  4. 计算机科学在地理信息科学中的作用,地理信息科学的孕育和发展
  5. 初赛中的CCF广告题
  6. NOI2018网络同步赛爆零记
  7. JAVA→JDBCJava DataBase Connectivity、存储过程Stored Procedure、事务Transaction、连接池DBCP C3P0、JDBC升级替代框架
  8. 快捷键截屏_win7截图快捷键是什么 win7截图快捷键怎么按
  9. python全栈构图_Python全栈-magedu-2018-笔记5
  10. Python入门--字典元素的操作,key的判断(in not in),字典元素删除(del),字典元素的增加,清空(clear()),修改