Reference from: https://blog.csdn.net/justloveyou_/article/details/55045638
The meaning of existence: object-oriented technology can solve some flexibility or scalability problems, but in many cases, it is necessary to increase the number of classes and objects in the system. When the number of objects is too large, the cost of running is too high, which brings about the performance degradation and so on. The pattern of the privilege was born to solve this kind of problem.
Internal state: The same content that can be shared in the intrinsic mode
External status (extrinsic state): Content that cannot be shared that needs to be set by the external environment
In practice, the internal state that can be shared is limited, so the object is generally designed as a smaller object, which contains less internal state, which is also called a fine-grained object.
Because the pattern requires that the object that can be shared must be a fine-grained object, it is also known as lightweight mode, which is an object-structured pattern.
Pattern structure:
Exclusive mode: In the exclusive element mode, all the objects are shared, that is, all the subclasses of the abstract class can be shared, and there is no unshared specific privilege class.
Step One: Create an abstract interface
Abstract the Role class public
interface FlyWeight {
//A schematic method, the parameter externalstate is the outer Yun State
void action (String externalstate) ;
}
Step Two: Create a specific element of the role class
The specific privileges role class public class
Concreteflyweight implements FlyWeight {
//intrinsic state
private String name;
constructor, the intrinsic state is passed as a parameter to the public
concreteflyweight (String name) {
this.name=name;
}
The Externalstate state is used as a parameter in the method to change the behavior of the method, but does not change the intrinsic state name of the object. Public
void Action (String externalstate) {
System.out.println ("intrinsic state =" + this.name);
SYSTEM.OUT.PRINTLN ("outer state =" + externalstate);
}
Step Three: Create an exclusive factory
Enjoy the Meta factory role classes public class
Flyweightfactory {
private static final Logger Log=loggerfactory.getlogger ( Flyweightfactory.class);
private static concurrenthashmap<string,flyweight> allflyweight=new concurrenthashmap<string,flyweight > ();
public static FlyWeight Getflyweight (String name) {
if (allflyweight.get (name) ==null) {
synchronized ( Allflyweight) {
if (allflyweight.get (name) ==null) {
System.out.println ("instance name does not exist, execute creation");
FlyWeight flyweight=new concreteflyweight (name);
SYSTEM.OUT.PRINTLN ("Create complete");
Allflyweight.put (name, flyweight);
}
}
return Allflyweight.get (name);
}
Step Four: Test
public class Test {public
static void Main (string[] args) {
flyweightfactory factory=new flyweightfactory ();
//applied for three a,b,b (but actually created only two)
FlyWeight fly=factory.getflyweight ("A");
Fly.action ("Create an Object A");
Fly=factory.getflyweight ("B");
Fly.action ("Create an object B");
Fly=factory.getflyweight ("B");
Fly.action ("Create an object B");
}
Compound privilege mode: a combination of some of the elements used in combination, can form a compound to enjoy the object, such a composite of the object itself can not be shared, but they could be decomposed into a simple to enjoy the object, and the latter can be shared.
Step One: Create an abstract interface to enjoy
Abstract FlyWeight Role class public
interface {
//A schematic method, parameter externalstate is the outer state
void action (String externalstate);
}
Step two: Establish a simple and specific role class
The specific privileges role class public class
Concreteflyweight implements FlyWeight {
//intrinsic state
private String name;
constructor, the intrinsic state is passed as a parameter to the public
concreteflyweight (String name) {
this.name=name;
}
The Externalstate state is used as a parameter in the method to change the behavior of the method, but does not change the intrinsic state name of the object. Public
void Action (String externalstate) {
System.out.println ("intrinsic state =" + this.name);
SYSTEM.OUT.PRINTLN ("outer state =" + externalstate);
}
Step two: Set up a composite specific privilege role class
public class Concretecompositeflyweight implements FlyWeight {
private map<string,flyweight> map=new Hashmap<string,flyweight> ();
Add a new Name,flyweight object to the aggregate public
void Add (String fly) {
map.put (name, fly);
@Override Public
void action (String externalstate) {
FlyWeight fly = null;
For (Object O:map.keyset ()) {
fly=map.get (o);
Fly.action ("Execute-----(external State)");}
}
Step three: Set up the Enjoy Yuan factory
//the role class public classes Flyweightfactory {private static Concurrenthashmap<string,flyweight> ;
Allflyweight=new concurrenthashmap<string,flyweight> (); The compound element factory method (which receives the intrinsic state (key) of a List collection parameter, traverses to get to the corresponding object) public FlyWeight getcompositeflyweight (list<string> compos
Itestate) {concretecompositeflyweight compository=new concretecompositeflyweight ();
for (String state:compositestate) {Compository.add (state, this.getflyweight);
return compository;
//Exclusive element factory method public static FlyWeight Getflyweight (String name) {if (Allflyweight.get (name) ==null) {
Synchronized (allflyweight) {if (Allflyweight.get (name) ==null) {System.out.println ("instance name does not exist, execute creation");
FlyWeight flyweight=new concreteflyweight (name);
SYSTEM.OUT.PRINTLN ("Create complete");
Allflyweight.put (name, flyweight);
}} return Allflyweight.get (name); }
}
Step Four: Test
public class Test {public
static void Main (string[] args) {
list<string> compositestate=new arraylist< String> ();
Compositestate.add ("A");
Compositestate.add ("B");
Compositestate.add ("C");
Compositestate.add ("C");
Flyweightfactory flyfactory=new flyweightfactory ();
FlyWeight compositefly1= flyfactory.getcompositeflyweight (compositestate);
FlyWeight compositefly2= flyfactory.getcompositeflyweight (compositestate);
Compositefly1.action ("Complex mode-----");
System.out.println ("----------");
System.out.println ("The compound is shared objects:" + (Compositefly1==compositefly2));
String state= "Hello";
FlyWeight fly1=flyfactory.getflyweight (state);
FlyWeight fly2=flyfactory.getflyweight (state);
System.out.println ("Whether the object is shared by the exclusive object:" + (Fly1==fly2));
}