zoukankan      html  css  js  c++  java
  • Head First ObjectOriented Design and Analysis 学习笔记(八)

    第八章

    Design Principles

    Originality is Overrated

    前言:

    这一章主要是给我们介绍了几个常用而且有效的design principle,前面我们已经学到(1)把多变的部分封装起来(2)面向接口编程而不是实现(3)每个类有且仅有一个原因去改变(4)类是关于行为和功能的,而不是属性上的差异

    案例分析:

    案例描述:

    问题提出:

    1 design principle

    问题解决:

    1 The Open-Closed Principle(OCP—老师说过)

    最经典的说法就是open for extension, and closed for modification,新的功能不是通过对原来的代码的修改获得的,而是通过拓展。

    前面的例子中,InstrumentSpec作为父类,然后被其他类GuitarSpec继承,拓展了父类的match方法,这就是OCP的一个应用,OCP最常见的应用就是在继承上,子类继承了父类的特性,然后拓展了父类的方法。OCP在这种层面上看是封装和抽象的组合,首先close for modification,就是使用了抽象的方法,将共性抽象出来写到一个基类里面,然后open for extension,就是使用了封装的方法,将一些非共性的,多变的东西封装起来,拓展基类。

    但是对于OCP的应用远远不只在继承上,这也是一种思想,让你的代码很好的隐藏自己的实现,不让外界有修改的机会,但是过于限制访问可能会带来拓展的麻烦,所有也要考虑好拓展的接口,才能做好OCP

    2 The Don’t Repeat Yourself Principle(DRY—最基本的了这个)

    这种方法是我们经常使用的,从教函数的时候就应该会这种方法了,avoid duplicate code by abstracting out things that are common and placing those things in a single location,在之前的例子中也使用过这种方法,把重复使用的代码放到某个独立的地方,然后调用就行了,其实也有点像封装。。

    值得注意的一点是,就算你懂得抽象出重复的代码,也要主要到,you only implement each feature and requirement in your application one single time,也就是说就算要抽出重复代码也要考虑好抽出后,把这个整体放在哪里,要放在一个合适的地方上

    Dry is not just removing duplication, it’s also about making good decisions about how to break up your system’s functionality

    3 The Single Responsibility Principle(SRP--- -0 – 我们学校student research project的简称,这个老师其实也讲过)

    这种方法是文章始终强调的,每个类只有好好的负责自己要做的事,并且只做一件事,才能做到松散耦合,every object in your system should have a single responsibility, and all object’s services should be focused on carrying out that single responsibility

    其实也跟我们前面说的“一个类只有一个理由去改变”的道理一样。当老师提出这种说法时我也觉得这样会使得类很小,但是事实反而是这样使得一个类很大,因为类是高度内聚的。

    文章使用以下这种方法来判定某个类是否很好的完成了自己的事

    The 类名 方法名 itself

    如果这条语句成立的话,那么则说明这个方法是类的一个应该完成的方法。

    4 The Liskov Substitiution Principle (LSP—没听说过)

    Subtypes must be substitutable for their base type,the lsp is all about well-designed inheritance. When you inherit from a base class , you must be able to substitute your subclass for that base class without things going terribly wrong . Otherwise, you are use inheritance incorrectly!也就是说lsp让我们能够分析我们的继承是否运用正确,基类类型引用变量创建派生类对象,然后使用基类方法时,会不会和派生类方法冲突或者有歧义。文中给了一个例子,这里就不多说,最后通过组合的方法来解决这个问题(这种解决方法也很常见,而判断是聚合还是组合,就要看两个对象之间的关联关系了,这里不多说聚合和组合的问题了。)

    Important Point:

    1 a design principle is a basic tool for technique that can be applied to designing or writing code to make that code more maintainable, flexible and extensible

    2 delegation is one of the alternatives to inheritance

    3 If you need to use functionality in another class, but you don’t want to change that functionality, consider using delegation instead of inheritance.

    4 Use composition to assemble behaviors from other classes.

    5 composition allows you to use behavior from a family of other classes, and to change that behavior at runtime (只要被组合的对象有多个派生子类,那么就可以使用接口的方式,完成动态绑定。)compositon lets you choose a behavior from a family of behaviors, often via several implementations of an interface

    6 尽量避免使用继承,而应该多使用delegation,composition,aggregation来代替

    小结:

    这一章介绍的几个在编码时用到的oo principle很实用。但是说来说去就那么几种,其实我们在日常编程中已经不知不觉的用上了,但是如果能知道什么时候用什么不用,那才叫炉火纯青啊。

  • 相关阅读:
    Oracle EBS—PL/SQL环境初始化之 fnd_global.apps_initialize
    fnd_profile.value的用法
    FND_MESSAGE 消息提示详解
    FORM触发器执行顺序
    大数据实战精英+架构师班 ④ 期
    .Net Core3.0 WebApi 十五:使用Serilog替换掉Log4j
    .Net Core3.0 WebApi 十四:基于AOP的切面redis缓存
    .Net Core3.0 WebApi 十三:自定义返回Json大小写格式
    .Net Core3.0 WebApi 十二:自定义全局消息返回过滤中间件
    .Net Core3.0 WebApi 十一:基于Log4j的全局异常处理
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1790945.html
Copyright © 2011-2022 走看看