Date Abstraction

  Hiding implementation is not just a matter of putting a layer of fucntions between the variables.Hiding implementation is about abstractions!A class does not simply push its varivables out through getters and setters.Rather it exposes abstract interfaces that allow its users to manipulate the essence of data, without having to know is implementation.

Data/Object Anti-Symmetry

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

  In any complex system there are going to be times when we want to add new data types rather than new functions.For these cases objects and OO are most appropriate.On the ohter hand,there will also be times when we'll want to add new functions as opposed to data types.In that case procedual code and data structures will be more appropriate.

The Law of Demeter

  There is a well-known heuristic called the Law of Demeter that says a module should not know about the innards of the objects it manipulates.As we saw in the last section,objects hide their data and expose operation.This means that an object should not expose its internal structure throught accessors because to do so is to expose,rather than to hide,its internal structure.

  More precisely, the Law of Demeter says that 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 of f

    An object held in an instance variable of C

  The method should not invoke methods on objects that are returned by any of the allowed functions.In other words,talk to friends,not to strangers.

Train Wrecks

  This kind of code is often called a train wreck because it looks like a bunch of coupled train cars.Chains of calls like this are generally considered to be sloppy style and should be avoided.

Hybrids

  The confusion sometimes leads to unfortune hybrid structures that are half object and half data structure.They have functions that do significant things,and they also have either public variables or public accessors and mutators that, for all intends and purpose,make the private variables public,tempting other external functions to use those variables the way a procedural program would use a date structure.

  Such hybrids male it hard to add new functions but also make it hard to add new data structures.They are the worst of both worlds.Avoid creating them.They are indicative of a muddled design whose authors are unsure of -or worse,ignorant of-whether they need protection from functions or types.

Hiding Structure

Data Transfer Objects

  The quintessential form of a data structure is a class with public variables and no functions.This is sometimes called a data transfer object, or DTO.DTOs are very useful structures,especialy when communicating with databases or parsing messages from sockets,and so on.They often become the first in a serious of translation stages that covert raw data in a database into objects in the application code.Beans have private variables manipulated by getters and setters.The quasi-encapsulation of beans seems to make some OO purists feel better but usually provides no other benefit.

Active Record

  Active Records are special forms of DTOs.They are data structures with public variables;but they typically have navigational methods like save and find.Typically these

Active Records are direct translations from database tables, or other data sources.

  Unfortunately we often find that developers try to treat these data structures as though they were objects by putting business rule methods in them.This is awkward because it creates a hybrid between a date structure and an object.

  The solution is to treat the Active Record as a data structure and to create separate objects that contain the business rules and that hide thier internal data.

Conclusion

  Objects expose behavior and hide data.This makes it easy to add new kinds of objects without changing existing behaviors.It also makes it hard to add new behaviors to existing objects.Data structures expose data and habe no significant behavior.This makes it easy to add new behaviors t existing data structures but makes it hard to add new data structures to existing functions.

Objects and Data Structures的更多相关文章

  1. Clean Code – Chapter 6 Objects and Data Structures

    Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...

  2. The Swiss Army Knife of Data Structures … in C#

    "I worked up a full implementation as well but I decided that it was too complicated to post in ...

  3. Python Tutorial 学习(五)--Data Structures

    5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...

  4. (转) Data structures

      Data structures A data structure is a group of data elements grouped together under one name. Thes ...

  5. 20162314 《Program Design & Data Structures》Learning Summary Of The Fifth Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Fifth Week ...

  6. 20162314 《Program Design & Data Structures》Learning Summary Of The Eleventh Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eleventh We ...

  7. [TypeScript] Custom data structures in TypeScript with iterators

    We usually think of types as something that can define a single layer of an object: with an interfac ...

  8. [译]The Python Tutorial#5. Data Structures

    [译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...

  9. Operating system management of address-translation-related data structures and hardware lookasides

    An approach is provided in a hypervised computer system where a page table request is at an operatin ...

随机推荐

  1. hdu----(1671)Phone List(Trie带标签)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu----(4686)Arc of Dream(矩阵快速幂)

    Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  3. 实验三 敏捷开发与XP实践(改)

    ---恢复内容开始--- 一.敏捷开发与XP 二.编码标准 1.编码标准中的版式就是一个很好的例子,版式虽然不会影响程序的功能,但会影响可读性.程序的版式追求清晰.美观,是程序风格的重要因素.单击Ec ...

  4. Go语言并发与并行学习笔记(二)

    转:http://blog.csdn.net/kjfcpua/article/details/18265461 Go语言的并发和并行 不知道你有没有注意到一个现象,还是这段代码,如果我跑在两个goro ...

  5. noip知识点总结之--线性筛法及其拓展

    一.线性筛法 众所周知...线性筛就是在O(n)的时间里找出所有素数的方法 code: void get_prime(int N){ int i, j, k; memset(Flag, ); ; i ...

  6. java使用dom4j解析xml文件

    关于xml的知识,及作用什么的就不说了,直接解释如何使用dom4j解析.假如有如下xml: dom4j解析xml其实很简单,只要你有点java基础,知道xml文件.结合下面的xml文件和java代码, ...

  7. ant非法字符:\65279 错误

    ant非法字符:\65279 错误前段时间用ant把项目打包,遇到一个问题:编译java文件的时候,有些java文件报非法字符 \65279错误,在网上找和很多方法,也试了很多方法,换JDK,网上说的 ...

  8. [Js]拖拽

    分析: 1.鼠标按下,拖拽开始,鼠标移动,拖拽进行,鼠标抬起,拖拽结束(三个事件) 2.被拖动元素与鼠标之间的位置在拖动过程中始终不变,利用这个原理,被拖动元素的位置就是鼠标的左(上)边距-鼠标与被拖 ...

  9. daemon

    关于daemon,其最简单的用法是: , ) == -) ; 将上面代码放置程序中,程序执行到这一行,就会自动进入后台运行,不再与终端交互,即终端再输入的参数无效,程序的输出(比如printf等)无效 ...

  10. 从ajax获取的数据无法通过Jquery选择器来调用事件

    如果标签是动态生成的,比如说div.tr.td等,若需通过Jquery来获取事件,那么需要用live来绑定相应的事件. 比如说绑定div的click事件 $("div").live ...