non-variable class
First, the popular Science 2 concepts, variable classes and immutable classes.
1), the immutable class means that when an instance of the class is created, the instance variable of the instance is immutable. Java provides 8 wrapper classes and string classes that are immutable classes that, when they are created, instance variables of their instance are not
That can be changed.
2), the variable class corresponds to the immutable class, and the meaning of the mutable class is that the instance variable of the class is variable. Most of the time, the classes you create are mutable classes, especially JavaBean, because they always provide the setter and
Getter method.
Look at the following code:
Double d = new double (6.5);
String linkin = "Linkinpark";
The above program creates a double object and a string object and passes in 6.5 and "Linkinpark" strings as arguments, so the double and string classes certainly need to provide instance variables to hold the two
parameter, but the program cannot modify the values of these two instance variables, so the double and string classes do not provide a way to modify them.
If you need to create a custom immutable class, observe the following rules:
1), use private and final to modify the member variables of the class
2), provides a constructor with parameters to initialize the class's member variables based on the passed parameters
3), only provide the getter method for this class, do not provide setter method, because the normal method can not change the properties of this class
4), if necessary, rewrite the Equals and Hashcode methods.
/** *
Non-variable class
* *
@author linkinpark
* * <pre>
* 1, properties using private final decoration
* 2, the constructor assigns a value of
* 3 to the property , provides only the Get method, does not provide the Set method
* 4, overrides the Equals and hashcode method if necessary
* </pre >
* Public
class Linkinpark
{
private final String name;
Private final Integer age;
Public Linkinpark (String name, Integer age)
{
super ();
this.name = name;
This.age = age;
}
Public String getName ()
{return
name;
}
Public Integer getage ()
{return age
;
}
public static void Main (string[] args)
{
new Linkinpark ("Linkinpark");
}
Compared to mutable classes, instances of immutable classes are always in the initialization phase throughout the lifecycle, and their instance variables are immutable, so it is simpler to control instances of immutable classes.
As mentioned in the previous final keyword, when you use the final modifier to refer to a type variable, only that reference type variable cannot be assignable, but the object to which the reference type variable is pointing can still be changed.
This creates the problem that when an immutable class is created, if it contains a variable type of member variable, the value of the member variable of its object is still variable, and that immutable class is actually a failure.
Look at the following example:
/** * Reference type variable causes immutable class failure * * @author Linkinpark */public class Linkinpark {private final String name;
Private final Linkin Linkin;
Public Linkinpark (String name, linkin Linkin) {super ();
THIS.name = name;
This.linkin = linkin;
Public String GetName () {return name;
Public Linkin Getlinkin () {return linkin; @Override public String toString () {return getclass (). Getsimplename () + "[name=" + name + ", linkin=" + linkin +
"]";
public static void Main (string[] args) {linkin linkin = new Linkin ();
Linkin.setname ("NightWish1");
Linkin.setage (25);
Linkinpark Linkinpark = new Linkinpark ("Linkinpark", Linkin);
System.out.println (Linkinpark);
Linkin.setage (24);
Linkin.setname ("NightWish2");
System.out.println (Linkinpark); Linkinpark [Name=linkinpark, Linkin=linkin [Name=nightwish1, age=25]]//Linkinpark [Name=linkinpark, Linkin=Linkin [
NAME=NIGHTWISH2, age=24]]}} class Linkin {private String name; Private IntegerAge
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public Integer Getage () {return age;
public void Setage (Integer age) {this.age = age;
@Override public String toString () {return getclass (). Getsimplename () + "[name=" + name + ", age=" + Age + "]"; }
}
Running the above code, we also see that reference type variables cause me to create a failed immutable class. How should we do that? Look at the following code:
/** * Reference type variable causes immutable class to fail * So to do special processing for reference type variables * * <pre> * 1, the constructor does not directly use the incoming reference type variable, the value and then new again * 2, reference type of variable to store just that
An initialized object * 3, preventing the Get method from returning directly to that variable to change the referenced object, the same way processing * </pre> * @author Linkinpark/public class Linkinpark {
Private final String name;
Private final Linkin Linkin;
/** * @param name * @param linkin/Public Linkinpark (String name, linkin Linkin) {super ();
THIS.name = name;
This.linkin = new Linkin (Linkin.getname (), Linkin.getage ());
Public String GetName () {return name;
Public Linkin Getlinkin () {return new Linkin (Linkin.getname (), Linkin.getage ()); @Override public String toString () {return getclass (). Getsimplename () + "[name=" + name + ", linkin=" + linkin +
"]";
public static void Main (string[] args) {linkin linkin = new Linkin ("NightWish1", 25);
Linkinpark Linkinpark = new Linkinpark ("Linkinpark", Linkin);
System.out.println (Linkinpark);
Linkin.setage (24); Linkin.setname ("Nightwish2 ");
System.out.println (Linkinpark); Linkinpark [Name=linkinpark, Linkin=linkin [Name=nightwish1, age=25]]//Linkinpark [Name=linkinpark, Linkin=Linkin [
NAME=NIGHTWISH2, age=24]]}} class Linkin {private String name;
Private Integer age;
Public Linkin (String name, Integer age) {super ();
THIS.name = name;
This.age = age;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public Integer Getage () {return age;
public void Setage (Integer age) {this.age = age;
@Override public String toString () {return getclass (). Getsimplename () + "[name=" + name + ", age=" + Age + "]"; }
}
immutable classes of cached instances
The instance state of an immutable class is not changeable and can be easily shared by multiple objects. If your program often needs to use the same immutable instance, you should consider caching instances of this immutable class. After all, repeatedly creating the same objects without
Has a lot of meaning, and it increases system overhead. If possible, you should cache an instance of an immutable class that you have created.
Caching is a very useful model in software design, there are many ways to implement the cache, different implementations may have a large performance difference, about caching performance issues and implementation I will be in the back of the blog to organize a
Classification, do not repeat here.
OK, I've used the immutable class Linkinpark, and now I'm using an array to write a cache pool that implements a cache pool of cached Linkinpark instances.
Of course, you can write caching directly in the Linkinpark class, which implements an immutable class that caches its own instance.
public class Linkinparkcache {//define an array + a subscript + array maximum capacity private static int pos_index = 0;
private static final int max_size = 10;
private static final linkinpark[] cache = new Linkinpark[max_size];
Define a name mark to override the Hashcode method private final String name;
Private Linkinparkcache (String name) {this.name = name;
Public String GetName () {return name; public static Linkinpark valueof (String name) {//1, loop get cached instance for (int i = 0; i < cache.length; i++) {i
F (Cache[i]!= null && cache[i].getname (). Equals (name)) {return cache[i]; }//2, if no instance is found after the loop is finished, add if (Pos_index = = max_size) {cache[0] = new Linkinpark (name, new Linkin ("Linkinpar") to the cache
K ", 25));
Pos_index = 1;
else {cache[pos_index++] = new Linkinpark (name, new Linkin ("Linkinpark", 25);
return cache[pos_index-1];
@Override public boolean equals (Object obj) {if (this = = obj) {return true; } if (obj!= null && obj.getclass () = = This.getclass ()) {Linkinpark Linkinpark = (linkinpark) obj;
Return Linkinpark.getname (). Equals (This.getname ());
return false;
@Override public int hashcode () {return name.hashcode ();
public static void Main (string[] args) {Linkinpark linkin = linkinparkcache.valueof ("The Cache Pool of Lincoln");
Linkinpark Linkinpark = linkinparkcache.valueof ("The buffer pool of Lincoln");
The following code outputs True, using the cache System.out.println (linkin = = Linkinpark);
}/** * Immutable class * * @author Linkinpark */class Linkinpark {private final String name;
Private final Linkin Linkin;
Public Linkinpark (String name, linkin Linkin) {super ();
THIS.name = name;
This.linkin = new Linkin (Linkin.getname (), Linkin.getage ());
Public String GetName () {return name;
Public Linkin Getlinkin () {return new Linkin (Linkin.getname (), Linkin.getage ()); @Override public String toString () {return getclass (). Getsimplename () + "[name=" + name + ", linkin=" + linkin +
"]"; } public static VoiD main (string[] args) {linkin linkin = new Linkin ();
Linkin.setname ("NightWish1");
Linkin.setage (25);
Linkinpark Linkinpark = new Linkinpark ("Linkinpark", Linkin);
System.out.println (Linkinpark);
Linkin.setage (24);
Linkin.setname ("NightWish2");
System.out.println (Linkinpark); Linkinpark [Name=linkinpark, Linkin=linkin [Name=nightwish1, age=25]]//Linkinpark [Name=linkinpark, Linkin=Linkin [
NAME=NIGHTWISH1, age=25]]}/** * Definition of reference type variable in immutable class * * @author Linkinpark/class Linkin {private String name;
Private Integer age;
Public Linkin () {super ();
Public Linkin (String name, Integer age) {super ();
THIS.name = name;
This.age = age;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public Integer Getage () {return age;
public void Setage (Integer age) {this.age = age; @Override public String toString () {return getclass (). Getsimplename () + [Name= +Name + ", age=" + Age + "]"; }
}