Java basics-object creation, basic java objects
I have been using constructors to generate or initialize objects. Recently, I found that some objects do not actually work in projects. Sometimes I only need some of its functions. To do this, is creating an object that consumes resources and affects program performance. Today, I saw some materials and gave an answer to this question.
Note:
First of all, it is certain that we sometimes do not need all the functions of a class. Perhaps we only need one or two methods, and there is no need to create a class for this. The static method solves this problem. Static methods should be a required course for every object-oriented design programmer. First, we will learn how to create objects using static methods.
The benefits of using a static factory to produce objects are as follows: "1. Static methods have names so that you can better understand what you are doing." 2: using static methods does not need to create objects each time, saving memory. "3: more flexible, you can return any child-type objects of the original return type." "4: the code looks more concise "; of course, it also has its disadvantages. One of the main disadvantages is that if you use static methods to generate objects, it seems that the public or protected constructors can be excluded, in this case, subclass inheritance is impossible. This problem can be solved through compound avoidance inheritance.
The following describes some well-known static constructors.
Example 1: multi-parameter classes
To consider this requirement, I need to create a class. This class is used to describe the product description of the product, so this class will have a lot of parameters, and each object has different parameter requirements for this class. If you use the overlapped constructor, you need to write a lot of constructor, which requires a maximum of 2 n constructor. Of course I won't write it like this. I will use the javaBeans mode to generate get and set methods. This method has some disadvantages. To be honest, I still don't see the harm of this writing. It seems that it is not safe in multithreading. Here we recommend a safer and more perfect construction method:
Use builder mode to generate objects. The general idea is this: build an internal builder class inside this multi-parameter class, use the internal class to receive passing parameters, and finally update the parameters of the external class. The advantage of doing so is that the legitimacy of data can be checked beforehand, and illegal data can be processed in advance to ensure data security. Here is an example (to write and use such a class, you need to know the syntax of the internal class ),
Public class BuiltTest {private String name1, name2, name3, name4, name5, name6, name7;/** internal class builder */public static class Built {private String name1, name2, name3, name4, name5, name6, name7; public Built setname1 (String name1) {this. name1 = name1; return this;} public Built setname2 (String name2) {this. name2 = name2; return this;} public Built setname3 (String name3) {this. name3 = name3; return this;} public Built setname4 (String name4) {this. name4 = name4; return this;} public Built setname5 (String name5) {this. name5 = name5; return this;} public Built setname6 (String name6) {this. name6 = name6; return this;} public Built setname7 (String name7) {this. name7 = name7; return this;} public BuiltTest built () {return new BuiltTest (this) ;}// constructor private BuiltTest (Built built) {this. name1 = built. name1; this. name2 = built. name2; this. name3 = built. name3; this. name4 = built. name4; this. name5 = built. name5; this. name6 = built. name6; this. name7 = built. name7;} // rewrite the toSting method @ Override public String toString () {String str = "My name is" + name1 + "," + name2 + ", "+ name3 +", "+ name4 +", "+ name5 +", "+ name6 +", "+ name7; return str ;}}
Call code:
// Create a BuiltTest object BuiltTest a = new BuiltTest. built (). setname1 ("xiaohua "). setname2 (" "). setname3 ("Anhui "). built ();
Objects Created using this builder method have better performance when the parameter is greater than or equal to 4. Considering the scalability of the program, it is best to use the builder in the first place.
Example 2: Enhancing Singleton attributes
In some programs, we only need one object for data security. In this case, we need the Singleton mode. Singleton can be written in three ways. The first method is to define a constant of Singleton, the second method is to return a Singleton object using the factory method, and the third method is to return an enumerated object. The first three methods are the most secure in multithreading security performance.
// The first method generates a final object. The private constructor is public class Elvis {public static final Elvis INSTANC_ELVIS = new Elvis (); private Elvis (){}}
// Chapter 2 method. The constructor generates a Singleton object public class Elvis {private static final Elvis INSTANC_ELVIS = new Elvis (); private Elvis () {} public Elvis getElvis () {return INSTANC_ELVIS ;}}
// Method 3: compile an enum class public enum Elvis {INSTANC_ELVIS ;}