Design Mode-Metadata Mode
The original Article "Use share to support large number of fine-grained objects efficiently" indicates that using shared objects can effectively support a large number of fine-grained objects. Here, shared objects refer to the resident memory of objects and can be used. fine-grained objects refer to objects with many similar or identical attributes and can be extracted from common attributes. The key to enabling shared object sharing is to distinguish between internal and external States. The public attributes mentioned above are internal states, and different attributes are external states. Let's take a look at the definitions of internal and external states:
Internal status: information that can be shared is stored inside the object, and does not change as the environment changes.
External status: the status changes and cannot be shared as the external environment changes. The external status is saved by the client and transmitted to the object if necessary.
Four roles in the metadata mode:
Abstract Flyweight: abstraction of the Meta class specifies the public interface that must be implemented. Operations in the External State can be passed in through parameters of the method.
ConcreteFlyweight: implements the business defined by the abstract meta-role. The internal status of a specific meta-object must be independent of the surrounding environment of the object, so that the meta-object can be shared in the system.
UnsharableFlyweight: Objects represented by a composite meta-role cannot be shared, but a composite meta-object can be divided into multiple combinations of specific meta-roles.
FlyweightFactory: this role constructs a pool container to create and manage the metadata role. It also provides methods to obtain objects from the container pool to ensure that the metadata objects can be shared by the system.
Class diagram of the metadata mode:
Various implementation codes:
Abstract meta-role:
Package com. zz. flyweight;/*** abstract enjoy meta role (Flyweight) * Copyright May 13, 2015 * created by txxs * all right reserved */public abstract class Flyweight {// internal State private String intrinsic; // external State private String Extrinsic; // defines the Business Operation public abstract void operate (); // requires that the meta-role must accept the external State public Flyweight (String Extrinsic) {this. extrinsic = Extrinsic;} public String getIntrinsic () {return intrinsic;} public void setIntrinsic (String intrinsic) {this. intrinsic = intrinsic ;}}
Specific metadata roles:
Package com. zz. flyweight;/*** specific meta-role * Copyright May 13, 2015 * created by txxs * all right reserved */public class ConcreteFlyweight extends Flyweight {// accept the external State public ConcreteFlyweight (String Extrinsic) {super (Extrinsic) ;}@ Overridepublic void operate () {// business processing }}
Compound meta-role:
Package com. zz. flyweight;/*** composite meta-role * Copyright May 13, 2015 * created by txxs * all right reserved */public class UnsharedConcreteFlyweight extends Flyweight {// accept external State public UnsharedConcreteFlyweight (String Extrinsic) {super (Extrinsic) ;}@ Overridepublic void operate () {// business logic }}
Enjoy yuan Factory:
Package com. zz. flyweight; import java. util. hashMap;/*** enjoy the metadata factory * Copyright May 13, 2015 * created by txxs * all right reserved */public class FlyweightFactory {// define a pool container private static HashMap
Pool = new HashMap
(); // The metadata factory public static Flyweight getFlyweight (String extrinsic) {// the object to be returned Flyweight flyweight = null; // obtain the returned object if (pool. containsKey (extrinsic) {flyweight = pool. get (extrinsic);} else {flyweight = new ConcreteFlyweight (extrinsic); pool. put (extrinsic, flyweight);} return flyweight ;}}
Advantages and disadvantages of the meta-mode:
Greatly reduce the number of objects in the memory, reduce memory occupation, and improve performance. However, the complexity of the system is increased, and the external and internal states need to be separated. The internal state is fixed and should not change with the change of the external State, making the logic more complicated; in addition, the metadata mode externalizes the state of the object, and reads the external State to slightly extend the running time.
Applicable scenarios:
1. There are a large number of similar objects in the system. These objects consume a large amount of memory.
2. scenarios requiring buffer pools
For example, the java base class library uses a large amount of the enjoy meta mode, and the String, Integer, and other classes use the enjoy meta mode to provide an internal pool mechanism. refer to the following code:
Package com. zz. decorator. impl;/**** test class * Copyright April 20, 2015 * created by txxs * all right reserved */public class ClientTest {public static void main (String args []) {String a = "abc"; String B = "abc"; System. out. println (a = B); System. out. println ("----------------------"); String c = new String ("Hello"); String d = new String ("Hello"); System. out. println (c = d );}}The first output is true, and the second output is fasle.