• Clean Code – Chapter 6 Objects and Data Structures


    • Data Abstraction

      Hiding implementation

    • Data/Object Anti-Symmetry

      Objects hide their data behind abstractions and expose function that operate on that data. Data structure expose their data and hava no meaningful functions.

      Procedural code(code using data structure) makes it easy to add new functions without changing the existing data structures. OO code, on the other hand, makes it easy to add new classes without changing existing functions.

      Procedural code makes it hard to add new data structures because all functions must change. OO code makes it hard to add new functions because all the classes must change.

    • The Law of Demeter

      A module should not know about the innards of the objects it manipulates.

      A method f of a class C should only call the methods of these:

      • C
      • An object created by f
      • An object passed as an argument to f
      • An object held in an instance variable of C

      推荐一篇博文:笛米特法则详解,通过一个简单的代码示例解释了上述概念。

      • Train Wrecks

        Bad code:

        final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();

        Or

        Options opts = ctxt.getOptions();
        File scratchDir = opts.getScratchDir();
        final String outputDir = scratchDir.getAbsolutePath();

        Only used for data structures

        final String outputDir = ctxt.options.scratchDir.absolutePath;
      • Hybrids

        Avoid hybrid structures that are half object and half data structure.

      • Hiding Structure

        Tell an object to do something, not ask it about its internals.

    • Data Transfer Objects (DTO)

      The quintessential form of a data structure is a class with public variables and no functions.

      • Active Record

        Special form of DTO. Hava navigational methods like save and find.

        Typically direct translations from database tables, or other data sources.

  • 相关阅读:
    狄利克雷卷积
    洛谷P2044 [NOI2012]随机数生成器
    Miller Rabin算法详解
    BZOJ3667: Rabin-Miller算法
    洛谷P3383 【模板】线性筛素数(Miller_Rabin)
    洛谷P3806 【模板】点分治1
    BZOJ1468: Tree
    Android Camera调用过程分析
    安卓开发37:自定义的HorizontalScrollView类,使其pageScroll的时候焦点不选中
    Android抖动动画
  • 原文地址:https://www.cnblogs.com/gumuyueying/p/cleancode-ch6.html
Copyright © 2020-2023  润新知