Objects and Data Structures
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的更多相关文章
- Clean Code – Chapter 6 Objects and Data Structures
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...
- 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 ...
- Python Tutorial 学习(五)--Data Structures
5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...
- (转) Data structures
Data structures A data structure is a group of data elements grouped together under one name. Thes ...
- 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 ...
- 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 ...
- [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 ...
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
- 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 ...
随机推荐
- Core Data系列文章(一)Core Data基础
在iOS开发数据库SQLite的使用介绍了iOS中使用SQLite对数据进行持久化存储,实际上是对数据库直接进行操作,而苹果专门有一套API来间接的对数据进行持久化存储,而且主要针对用户创建的对象 - ...
- SQL Server数据库(SQL Sever语言 存储过程及触发器)
存储过程:就像函数一样的会保存在数据库中-->可编程性-->存储过程 创建存储过程: 保存在数据库表,可编程性,存储过程create proc jiafa --需要的参数@a int,@b ...
- lucene字典实现原理——FST
转自:http://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找到 ...
- Word文档增加快捷键
- tar 排除指定目录 –exclude
假设 test目录下有 1 2 3 4 5 这5个目录, 1下有6 7两个目录, 现在要将3 4 5 6目录tar打包,2和1下的6这两个目录不要.命令如下: Example[www]#cd test ...
- Web API系列
ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web API 是一种用于在 .NET Framework 上构 ...
- mybatis中的mapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- AFNetworking框架使用
本文是由 iOS Tutorial 小组成员 Scott Sherwood撰写,他是一个基于位置动态加载(Dynamically Loaded)的软件公司(专业的混合定位)的共同创办人. 网络 — 你 ...
- 如何在Hadoop的MapReduce程序中处理JSON文件
简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...
- [示例]创建Student类,输入学生信息并存入字典,将3个存有学生信息的字典存入数组,并计算
代码: main: #import <Foundation/Foundation.h> #import "Student.h" int main(int argc, c ...