Some tips design category

Some concepts of objects and classes in object-oriented language which is similar, not much to do in this detail.

In the actual coding of them, there are some tricks designed to make us more OOP class professional standards.

Reference from "Java core technology Volume"

 

1. must ensure that private data

  That is not to destroy encapsulation. Sometimes the method or methods to access the update may be required, but not too troublesome, better to remain private instance fields. When data remain private, their representation of change will not affect on the user class. My understanding is that there is such a class used many times, but if you keep the package, access or updates or other operations has a unique method of operation, it would be very consistent to use, just use its own method of programming languages as easy and consistent.

 

2. Be sure to initialize the data

  Best not to rely on the system default value, but should explicitly initialize all data, specific initialization method may provide a default value, a default value may be provided for all the constructor.

 

3. Do not use too many basic types in the class

  It means meaningful class is used to replace a substantially more associated types. Note that not the basic data types. Such as:

private String street;
private String city;
private String state;
private int zip;

  Address of a class can be used to replace these basic types.

 

4. Not all fields need to access separate domains and domain changer

  In the first point, when it comes to keeping class encapsulation, but not all classes need access methods and methods change, because in the actual business needs, some data are not want to be seen or changed. This is according to the actual needs to design, just a reminder not to think about it in class when the package directly to access and change is all on the whole.

 

5. The duty class decompose excess

  Understand that is to say, a class of functions must be single, not what features are written in one place. Write separate benefit is obvious, I think the most important thing is that we can improve the reusability of code, and easy to maintain. In this way after the encounter require the same functionality needs before written directly call on the line.

 

6. class and method names to be able to reflect their responsibilities

  This is not good for beginners or students in English, is very easy to overlook that. Just start knocking some examples, very much with the type T1, a1 similar method name. But in order to develop good habits, and be able to let others can understand, we must now play the role of classes and methods close to the word as the name. For poor students in English, a translation software is essential, but also slowly accumulate vocabulary.

 

7. Priority immutable classes

  When multiple threads simultaneously update an object, concurrent change occurs, the result is unpredictable. If the class is immutable, it can safely share objects between multiple threads. In practice, however, many of the class is to be updated. Immutable class represented by the class common values, such as time.

Guess you like

Origin www.cnblogs.com/lbhym/p/11486437.html