object-oriented
How about too many constructor parameters?
Use Builder mode, use in
1. Five or more member variables
2, the parameters are not many, but in the future, the parameters will increase
Builder mode:
Belongs to the creation mode of an object
- 1. Abstract Builder: Generally an interface that contains 1) build methods, ways of building parts (more than one)
2) the method of returning the product
- 2. Specific builder
- 3. The director, who calls the concrete builder, creates the product object
- 4. Products, complex objects to build
For the client, the director and concrete builder are created, and the concrete builder is handed over to the director, who is then instructed by the client to manipulate the builder to create the product.
In practical applications, the abstract builder and director are sometimes omitted.
Classes that do not need to be instantiated should have constructor private
For example, some utility classes provide static methods and should not provide concrete instances. Refer to the JDK
The Arrays.
Don’t create unnecessary objects
1. Avoid unintentionally created objects, such as automatic boxing
2. Use static when possible for member variables that can be reused between instances of a class.
However, remember that it is not creating unnecessary objects, but not not creating objects.
Object pooling should be used with caution unless creating objects is a very expensive operation, such as connecting to databases, giant objects, etc
And so on.
Avoid finalization methods
The Finalizer method, which the JDK does not guarantee when or will be executed. If there are resources that are actually to be released
You should use try/finally.
Minimize the accessibility of classes and members
One of the most important goals of programming and architecture is decoupling between modules. Make classes and members most accessible
Miniaturization is undoubtedly one of the effective ways.
Minimize variability
Try to make classes immutable. Immutable classes are easier to design, implement, and use than mutable classes, and they are harder to produce
No, it’s safer.
Commonly used means:
Does not provide any methods to modify the state of an object;
Make all fields final.
Make all fields private.
Use the copy-on-write mechanism. Problems: it will cause the system to produce a large number of objects, and the performance has a certain impact,
Careful tradeoffs are needed.
Composition takes precedence over inheritance
Inheritance tends to break encapsulation and makes the implementation of subclasses dependent on the parent class.
Composition is adding a private field to a class that refers to an instance of the class, thus avoiding dependency on the concrete reality of the class
Now.
Inheritance is appropriate only if the subclass is really a subtype of the parent class.
Interfaces are superior to abstract classes
Java is single-inherited, but classes allow you to implement multiple interfaces.
Therefore, when the service changes, a new interface is added and the class that needs to change the service needs a new interface. But the abstract
Classes can cause classes that do not need to change to have to implement new business methods.
A common design approach in the JDK is to define an interface and declare an abstract skeleton class that implements the interface, the skeleton
Class classes implement common methods, while actual business classes can implement both interfaces and inherit skeleton classes, or just interfaces
My mouth.
For example, HashSet implements the implements Set interface but extends the AbstractSet class, while
AbstractSet itself implements the Set interface. Other such as Map, List are designed in this way.
methods
Use variables with caution
Variable arguments are allowed to pass 0 arguments
If the number of parameters ranges from 1 to multiple, separate service control is required.
Look at the code
Returns a zero-length array or collection, not NULL
The result of the method returns NULL, which causes the caller’s case to be handled as NULL alone. Returns zero length, caller can
For example, use foreach.
The JDK also provides us with zero-length Collections like collections.empty_list
Standard exceptions are preferred
Strive for code reuse while reducing the number of class loads and improving the performance of class loads.
Common exceptions:
IlegalAraumentException – The argument passed by the caller is not appropriate
LllegalStateException – The received object is not in the right state,
NullPoint
UnsupportedOperationException – an unsupported operation
General programming
Use enumerations instead of int constants
A declared enumeration is essentially a class, and each concrete enumeration value is an instance of that enumeration class.
Enumeration for more, see the code.
Minimize the scope of local variables
- 1. Declare it where it was first used
- 2. Local variables must be initialized by themselves. If the initialization conditions are not met, do not declare them
Minimize the benefits of reducing the size of the local variable table, prompt performance; At the same time, avoid premature declaration of local variables resulting in errors
Be sure to use.
Calculate accurately, avoiding floats and doubles
You can use int or long and BigDecimal