Algorithm (4th edition) -1.2.5 design of data types

Source: Internet
Author: User
Tags instance method

Summary: This section describes important knowledge about designing data types, including encapsulation, design APIs, relationships between algorithms and abstract data types, interface inheritance, implementation inheritance, equivalence, memory management, immutability, exceptions, and errors. ("As a simple matter, many of the topics below are not related to the learning of algorithms, so you can skip this section and look back when you encounter specific problems in implementing abstract data types in the future.") ”)

Focus:

1. All the time spent designing an excellent API can be rewarded for the time saved by debugging and code reuse.

2. In this book, the algorithm is generally implemented as an instance method of an abstract data type.

3. In this book we avoid using subclass inheritance because he destroys encapsulation.

4. The Java Convention equals () must be an equivalence relationship. It must have:

· Reflexive, X.equals (x) is true;

· Symmetry, x.equals (y) returns True when and only if Y.equals (x) is true;

· Transitivity, if both x.equals (y) and y.equals (z) are True,x.equals (z), also true.

In addition, it must accept an object as a parameter and satisfy the following properties:

· Consistency, when two objects are not modified, repeated calls to X.equals (Y) always return the same value;

· Non-nullability, x.equals (NULL) always returns FALSE.

5. You can use the following implementation as a template for the Equals () method that implements any data type:

 Public classDate {Private Final intmonth; Private Final intDay ; Private Final intYear ;  PublicDate (intMintDinty) {month=m; Day=D; year=y; }     Public Booleanequals (Object x) {if( This= = x)return true; if(x = =NULL)return false; if( This. getclass ()! = X.getclass ())return false; Date that=(Date) x; if( This. Day! = That.day)return false; if( This. Month! = that.month)return false; if( This. Year! = that.year)return false; return true; }}

6. One of the most important features of Java is automatic memory management. It frees programmers from the responsibility of managing memory by recording orphan objects and releasing their memory into the memory pool. This way of reclaiming memory is called garbage collection.

7. Another disadvantage of immutability is that final is unfortunately only used to guarantee the immutability of instance variables of the original data type, but not for variables of reference types. If an instance variable of an application type contains the modifier final, the value of the instance variable (a reference to an object) can never be changed-it will always point to the same object, but the value of the object itself is still mutable. For example, this code does not implement an immutable data type:

 Public class Vector {    privatefinaldouble[] coords;      Public Vector (double[] a) {        = A;    }    ...}

A use case program can create a vector object from a given array and change the value of the elements in the vector after the build function executes (bypassing the API):

Double [] A = {3.0, 4.0new  Vector (a); a[//  bypass public API

8. Any data type should be designed with immutability in mind, and the data type is immutable and should be described in the API so that the user can know that the value in the object cannot be changed.

9. The default setting does not initiate assertions, and you can use the-enableassertions flag (abbreviated as-EA) at the command line to start the assertion. The purpose of the assertion is to debug: The program should not rely on assertions in normal operations, as they may be disabled. The system programming course learns to use assertions to ensure that the code is never terminated by a system error or into a dead loop. (? )

10. If you always complain about the nature of programming language, then you can only design your own programming language. Domineering )

Algorithm (4th edition) -1.2.5 design of data types

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.