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.

时间: 2024-07-29 09:25:18

Objects and Data Structures的相关文章

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l

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 operating system running in the hypervised computer system. The operating system determines whether the page table request requires the hypervisor to process.

Lock-Free Data Structures

By Andrei Alexandrescu, October 01, 2004 Post a Comment Lock-free data structures guarantee the progress of at least one thread when executing mutlithreaded procedures, thereby helping you avoid deadlock. Andrei Alexandrescu is a graduate student in

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 the blog. What I was really trying to get across was that immutable data structures were possible and not that hard; a full-on finger tree implementation

视频笔记 CppCon 2016 Chandler Carruth High Performance Code 201 Hybrid Data Structures

版权声明:转载请说明出处 http://www.cnblogs.com/eagledai/ https://www.youtube.com/watch?v=vElZc6zSIXM&t=1157s LLVM internal data structures LLVM 内部数据结构 2:24 SmallVector 下面是 SmallVector 的简化版本.这是 LLVM 里最基本的数据结构,到处被使用. 问题:为什么不直接使用 STL 的 vector? 因为在 STL 里,如果对 vector

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

20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eleventh Week Summary of Key Concepts In hashing, elements are stored in a hash table, with their location in thetable determined by a hashing function. The situation

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&

【DataStructure】Linked Data Structures

Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. But if you want to maintain an ordered list that allows quick insertions and deletions, you should use a linked data structure. so inserting an elemen

2014 UESTC Training for Data Structures H - Cookies Test

H - Cookies Test Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status As chief programmer at a cookie production plant you have many responsibilities, one of them being that the cookies produced and packag