Java Design Pattern modeling and implementation of cainiao series (21) enjoy metadata Pattern
Flyweight: uses the sharing technology to effectively support a large number of fine-grained objects. The main purpose is to share objects, that is, the shared pool. When there are many objects in the system, the memory overhead can be reduced. To some extent, You can regard a singleton as a special case of Yuan-sharing.
I. uml modeling:
Ii. Code Implementation
/*** Flyweight: uses the sharing technology to effectively support a large number of fine-grained objects. ** The main purpose is to share objects, that is, the shared pool. When there are many objects in the system, the memory overhead can be reduced. */Abstract class FlyWeight {public abstract void method ();}/*** create a subclass holding the key */class SubFlyWeight extends FlyWeight {private String key; public SubFlyWeight (String key) {this. key = key ;}@ Overridepublic void method () {System. out. println (this is the sub method, and the key is + this. key) ;}}/*** metadata Factory: Creates and manages metadata objects */class FlyweightFactory {private Map
Map = new HashMap
();/*** Get the object */public FlyWeight getFlyWeight (String key) {FlyWeight flyWeight = map. get (key); if (flyWeight = null) {flyWeight = new SubFlyWeight (key); map. put (key, flyWeight);} return flyWeight;}/*** get the number of object objects */public int getCount () {return map. size () ;}/ *** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {/*** create a metadata factory */FlyweightFactory factory = new FlyweightFactory ();/***** first case: key is the same ***************/FlyWeight flyWeightA = factory. getFlyWeight (aaa); FlyWeight flyWeightB = factory. getFlyWeight (aaa);/*** you can know that the keys are aaa, so flyWeightA and flyWeightB point to the same memory address */System. out. println (flyWeightA = flyWeightB); flyWeightA. method (); flyWeightB. method ();/*** number of objects to be shared: 1 */System. out. println (factory. getCount ();/******* Case 2: Keys are inconsistent ****************/System. out. println (============================================ ); flyWeight flyWeightC = factory. getFlyWeight (ccc);/*** the print result is false */System. out. println (flyWeightA = flyWeightC); flyWeightC. method ();/*** number of objects to be shared: 2 */System. out. println (factory. getCount ());}}
Print result:
True
This is the sub method, and the key is aaa
This is the sub method, and the key is aaa
1
==============================================
False
This is the sub method, and the key is ccc
2
Iii. Summary
The differences between the shared object and the single instance: 1. Unlike the single instance mode, the shared object mode is a class that can have many objects (share a set of objects ), while Singleton is a class with only one object. 2. They have different purposes. The metadata mode is used to save memory space and improve program performance (avoid a large number of new operations ), the Singleton mode shares the status and features of a single object.