define
The share pattern is a structural pattern that uses sharing techniques to effectively support a large number of fine-grained objects.
Pure share mode
In the share pattern, we can build a pure share pattern based on its definition. The following is a class diagram of pure share mode:
Flyweight.java
public interface Flyweight {
public void operation(String state);
}
Copy the code
For the content part, ConcreteFlyweight must be shareable, and any state it holds must be intrinsic, regardless of the context in which it is applied. The string “Hello,” for example, doesn’t matter where it’s used; it’s an immutable object.
ConcreteFlyweight.java
public class ConcreteFlyweight implements Flyweight { private String intrinsicState = null; Public ConcreteFlyweight(String _intrinsicState) {this.intrinsicState = _intrinsicState; } @override public void operation(String extrinsicState) {Override public void operation(String extrinsicState) { System.out.println("Intrinsic state:" + intrinsicState);
System.out.println("External state:"+ extrinsicState); }}Copy the code
In the share mode, a crucial module is the factory module. The Flyweight Factory maintains a Flyweight pool (holding the internal state) through which the Flyweight Factory controls the entire share mode.
FlyweightFactory.java
public class FlyweightFactory {
private Map<Integer, Flyweight> labels = new HashMap<Integer, Flyweight>();
public Flyweight factory(String intrinsicState) {
int hashCode = intrinsicState.hashCode();
Flyweight fly = labels.get(hashCode);
if (fly == null) {
fly = new ConcreteFlyweight(intrinsicState);
labels.put(hashCode, fly);
}
returnfly; }}Copy the code
As you can see in the factory module above, the FlyweightFactory actually performs a filtering function, filtering duplicate objects and caching new ones.
Unshared share mode
The opposite of a shared share object is an unshared share object. You may be wondering about unshared share objects, if not shared, why include them in the share schema? This may be for completeness reasons, or for some scenarios to use both shared and unshared privileges. I do not fully understand this point, and it is possible that there is no need for a non-shared share element object. Here is the unshared meta-class diagram:
public class UnsharedConcreteFlyweight implements Flyweight {
private String intrinsicState = null;
public UnsharedConcreteFlyweight(String _intrinsicState) {
this.intrinsicState = _intrinsicState;
}
@Override
public void operation(String extrinsicState) {
System.out.println("Intrinsic state:" + intrinsicState);
System.out.println("External state:"+ extrinsicState); }}Copy the code
Here is the code that tests pure and unshared privileges.
Client.java
public class Client {
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight fly1 = factory.factory("Hello");
fly1.operation("ExtrinsicState-1");
Flyweight fly2 = factory.factory("DesignPattern");
fly2.operation("ExtrinsicState-2");
Flyweight fly3 = factory.factory("Flyweight");
fly3.operation("ExtrinsicState-3");
Flyweight fly4 = factory.factory("Hello");
fly4.operation("ExtrinsicState-4");
System.out.println("fly1 == fly2 ? " + (fly1 == fly2));
System.out.println("fly1 == fly3 ? " + (fly1 == fly3));
System.out.println("fly1 == fly4 ? " + (fly1 == fly4));
Flyweight fly5 = new UnsharedConcreteFlyweight("Unshared");
fly5.operation("ExtrinsicState-5"); }}Copy the code
Extrinsicstate-2 Extrinsicstate-2 Flyweight extrinsicstate-3 Extrinsicstate-4 fly1 == fly2?false
fly1 == fly3 ? false
fly1 == fly4 ? trueAggregates state: Unshared Aggregates state: Extrinsicstate-5Copy the code
Composite share mode
In the pure share above, all the shares are in the same group. Sometimes such a large collection may not play a good application function, so the introduction of a composite share mode. Here is the composite meta-schema class diagram:
That is to say, the compound share element is equivalent to a regrouping of the simple share element, and in each group of the compound share element there is an independent simple share mode. The key codes are as follows:
ConcreteCompositeFlyweight.java
public class ConcreteCompositeFlyweight implements Flyweight {
private Map<Integer, Flyweight> labels = new HashMap<Integer, Flyweight>();
public void add(int key, Flyweight flyweight) {
labels.put(key, flyweight);
}
@Override
public void operation(String extrinsicState) {
Flyweight flyweight = null;
for(Object key : labels.keySet()) { flyweight = labels.get(key); flyweight.operation(extrinsicState); }}}Copy the code
The build factory also needs to be redesigned as follows:
FlyweightFactory.java
public class FlyweightFactory { private Map<Integer, Flyweight> labels = new HashMap<Integer, Flyweight>(); /** * Public Flyweight Factory (String intrinsicState) {... .returnfly; } / composite Flyweight factory * * * * / public Flyweight compositeFactory (a List < String > intrinsicStates) {ConcreteCompositeFlyweight Flyweight = new ConcreteCompositeFlyweight();for (String intrinsicState : intrinsicStates) {
flyweight.add(intrinsicState.hashCode(), factory(intrinsicState));
}
returnflyweight; }}Copy the code
The test class
Client.java
public void compositeFlyweight() {
List<String> intrinsicStates = new ArrayList<String>();
intrinsicStates.add("Hello");
intrinsicStates.add("Java");
intrinsicStates.add("DesignPattern");
intrinsicStates.add("Flyweight");
FlyweightFactory factory = new FlyweightFactory();
Flyweight flyweight1 = factory.compositeFactory(intrinsicStates);
Flyweight flyweight2 = factory.compositeFactory(intrinsicStates);
System.out.println("flyweight1 == flyweight2 ? " + (flyweight1 == flyweight2));
flyweight1.operation("Composite Hoi-1");
flyweight2.operation("Composite Premium 2");
}
Copy the code
flyweight1 == flyweight2 ? falseIntrinsic state: Java intrinsic state: Compound Heyuan -1 intrinsic state: Flyweight extrinsic state: Compound Heyuan -1 intrinsic state: Hello extrinsic state: Compound Heyuan -1 intrinsic state: DesignPattern Extrinsic state: Compound Heyuan -1 intrinsic state: Java extrinsic state: Compound xiangyuan-2 aggregate state: Flyweight aggregate state: Compound Xiangyuan-2 aggregate state: Hello aggregate state: Compound xiangyuan-2 aggregate state: DesignPattern aggregate state: Compound Xiangyuan-2 aggregate state: Flyweight aggregate state: Compound Xiangyuan-2 aggregate state: Hello aggregate state: Compound Xiangyuan-2 aggregate state: DesignPattern aggregate state: Compound Xiangyuan-2 aggregate state: Compound Xiangyuan-2 aggregate stateCopy the code
Meta schema in the JDK
In JDK design, there is also a meta schema. For example, some constant pool designs (String constant pool, Integer constant pool, etc.);
Life is beautiful like summer flowers, death like autumn leaves