Extract from "clean code"-object and Data Structure

Source: Internet
Author: User

 

6.1 data abstraction

Hiding is not just as simple as placing a function layer between variables. The class does not simply use getter and setter to push variables out, but exposes abstract interfaces so that users can operate on the Data ontology without understanding the data implementation.

As shown in the following figure, a reader needs to obtain the current reading progress. The interface is defined as follows:

Public interface paperreader

{

Double gettotalpages ();

Double getcurrentpageindex ();

}

 

The better way is:

Public interface paperreader

{

Double getcurrentprogress ();

}

 

Adding Values and assigning values is strictly required.

 

 

6.2 anti-symmetry of data and objects

Procedural Code (code using the data structure) allows you to add new functions without modifying the existing data structure. Object-oriented Code allows you to add new classes without modifying existing functions.

 

6.3 The Law of Demeter Class C method F should only call the method of the following object :. class C. object created by F. the object passed to F as a parameter. objects held by object variables of class C. 6.4 data transmission objects (DTO, data transfer objects) have only public variables and have no function classes, which are called DTO.

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.