Code tuning, in effect, is to reduce the time and space cost of the program. Flyweight pattern allows the use of objects in different parts of the application, which can drastically reduce the number of new objects and reduce the time and space cost of a large number of objects. The flyweight pattern object can be divided into internal characteristics (regardless of where the ovject is used, the internal characteristics are unchanged) and the external features (not fixed, need to be calculated and changed on different occasions). This external feature guarantees that the same object can be applied in different situations, and that different situations require different object characteristics, so we can modify the characteristics of the external objects to show different characteristics of the same object.
Here we give an application of the robot object, the UML diagram is:
The main difference between the two implementations of the Ialien interface is that it has a different shape, which belongs to the internal property. At the same time, the invocation of Ialien in different locations shows that different states are determined by color. And what color is determined by the parameters passed in, this we can again different locations for Ialien to pass in different parameters, let it show a different state, so that it applies an object to complete the function of multiple objects. The factory class, which is used to store newly created objects, can be applied to the factory each time a call is made to the object, at which point the factory function is essentially the same as the object pool.
The code example is given below:
Class Largealien implements ialien{
Private String shape = "Large Shape";
Public String Getshape () {
Return shape:
}
Public Color getColor (int madlevel) {
if (madlevel==0)
return color.green;
else if (Madlevel = = 1)
return color.red;
Else
Return Color.Blue
}
}
Class Littlealien implements ialien{
Private String shape = "Little Shape";
Public String Getshape () {
Return shape:
}
Public Color getColor (int madlevel) {
if (madlevel==0)
return color.green;
else if (Madlevel = = 1)
return color.red;
Else
Return Color.Blue
}
}
public class Alienfactory {
Private map<string, ialien> list = new hashmap<> ();
public void Savealien (String index, Ialien alien) {
List.put (Index,alien);
}
Public Ialien Getalien (String index) {
Return List.get (index);
}
}
Alienfactory factory = new Alienfactory ();
Factory. Savealien ("Largealien", New Largealien ());
Factory. Savealien ("Littlealien", New Littlealien ());
Ialien a = factory. Getalien ("Largealien");
Ialien B = Factory. Getalien ("Littlealien");
System.out.println ("Showing intrinsic states ...");
System.out.println ("Alien of type Largealien is" + a.getshape ());
System.out.println ("Alien of type Littlealien is" + b.getshape ());
System.out.println ("Showing extrinsic states ...");
System.out.println ("Alien of type Largealien is" + a.getcolor (0). toString ());
System.out.println ("Alien of type Largealien is" + a.getcolor (1). ToString ());
System.out.println ("Alien of type Littlealien is" + b.getcolor (0). toString ());
System.out.println ("Alien of type Littlealien is" + b.getcolor (1). ToString ());
The screen output is:
Java flyweight pattern for code-optimized design patterns