Objects and Data Structures

Source: Internet
Author: User

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 it varivables out through getters and setters. Rather it exposes abstract interfaces that allow its users to manipulate the essence of data, without have to know are IM Plementation.

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 is going to is times when we want to add new data types rather than new functions. For these cases objects and OO is most appropriate. On the Ohter Hand,there would also is times when we'll want to add new functions as opposed to data types. In this case procedual code and data structures would be is more appropriate.

The law of Demeter

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

More precisely, the law of Demeter says, 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 is returned by any of the allowed functions. In and Words,talk to Friends,not to strangers.

Train Wrecks

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

Hybrids

The confusion sometimes leads to unfortune hybrid structures, that is half object and half data structure. They has functions that does significant things,and they also has 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 Variab Les the procedural program would use a date structure.

Such hybrids Male it hard-add new functions but also make it hard to add new data structures. They is the worst of both worlds. Avoid creating them. They is indicative of a muddled design whose authors is unsure of-or worse,ignorant of-whether they need protection fro m 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 is 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 APPL Ication code. Beans has 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 is special forms of dtos.they is data structures with public variables;but they typically has navigation Al methods like Save and find. Typically these

Active Records is direct translations from the 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 Busi Ness rule methods in them. This was 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 this contain the business RU Les 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-add new data structures to Existin G functions.

Objects and Data Structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.