I've been using constructors to generate or initialize objects in a way. Recently found that some objects in the project does not actually work, sometimes I just need some of its functions, to create a new object is not to compare the consumption of resources, affect the performance of the program. Today, I have seen some information to give an answer to this question.
Description
The first thing to be sure is that we don't sometimes need the full functionality of a class, maybe we just need one or two of them, and there's no need to create a new class for this. The static method solves the problem. Static methods should be a required course for each object-oriented design program. First we learn the static method to create the object.
The benefits of producing objects using a static factory have several advantages: "One: static methods have names that make you more aware of what you're doing"; "Two: Using static methods saves memory when creating objects every time"; "Three: more flexible, can return objects of any subtype of the original return type" " Four: The code looks more concise "; Of course it also has drawbacks, one of the main drawbacks is that if you use static methods to produce objects, then it seems that you can not include the public or protected constructors, so that the subclass would like to inherit is impossible. This can be solved by compound avoidance of inheritance.
Here are some examples of some of the more famous static constructors.
First example: Multi-parameter class
Considering this one requirement, I need to build a class. This class is used to describe the product description of the product, then the class will have a lot of parameters, and in each object to the parameter requirements of this class is different. If you use the overlay constructor, you need to write a lot of constructors, up to a maximum of 2 n constructors. Of course I will not write this, I will use the JavaBeans mode to generate get, set mode. There are drawbacks to this approach, and frankly, I don't see the harm of writing this way, which seems to be unsafe in multiple threads. It is recommended to give you a safer and more complete construction method:
Use the builder mode to build the object. The general idea is this: building a builder's inner class inside this multi-parameter class, using an inner class to receive the arguments, and finally updating the parameters of the external class. The advantage of this can be the legitimacy of the pre-test data, the illegal data can be processed in advance, to ensure the security of the data. Here's an example (to write and use such a class, you need to know the syntax of the inner class),
Public classBuilttest {PrivateString name1, name2, Name3, Name4, Name5, Name6, Name7; /** Internal Class builder*/ Public Static classBuilt {PrivateString name1, name2, Name3, Name4, Name5, Name6, Name7; PublicBuilt setname1 (String name1) { This. name1 =name1; return This; } PublicBuilt setname2 (String name2) { This. name2 =name2; return This; } PublicBuilt Setname3 (String name3) { This. Name3 =Name3; return This; } PublicBuilt setname4 (String name4) { This. Name4 =Name4; return This; } PublicBuilt setname5 (String name5) { This. Name5 =Name5; return This; } PublicBuilt setname6 (String name6) { This. Name6 =Name6; return This; } PublicBuilt Setname7 (String name7) { This. Name7 =Name7; return This; } PublicBuilttest built () {return NewBuilttest ( This); } } //Construction Method Privatebuilttest (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; } //overriding the Tosting method@Override Publicstring toString () {string str= "My name is" +name1+ "," +name2+ "," +name3+ "," +name4+ "," +name5+ "," +name6+ "," +Name7; returnstr; }}
Code to invoke:
// Create a Builttest object Builttest a=New builttest.built (). SETNAME1 ("Xiaohua"). Setname2 ("small"). Setname3 ("Anhui"). Built ();
This builder method is built on the object, tested at a parameter greater than or equal to 4 o'clock, the performance is more excellent. Given the extensibility of the program, it's a good idea to start with builder's constructor.
Second example: Hardening Singleton properties
In some programs, for data security, we need only one object, and then we need singleton mode. Singleton has three ways of writing, the first defines a singleton constant, the second method returns a singleton object using the factory, and the third Way is to return an object of an enumerated type. The third approach to multithreading security is the safest.
// The first way to produce a final object, the privatization of the constructor method public class Elvis { public static final Elvis instanc_elvis = new Elvis (); private Elvis () {}}
// Chapter II method, and the constructor produces a singleton object public class Elvis { private static final Elvis instanc_elvis = new Elvis (); private Elvis () {} public Elvis Getelvis () { r Eturn Instanc_elvis; }}
// the third method, write an enum class Public enum Elvis { instanc_elvis; }
Java Foundation Improvement--object creation