Design Mode (22) --- metadata Mode
Definition: Using shared objects can effectively support a large number of fine-grained objects.
The definition of the shared object model puts forward two requirements: fine-grained objects and shared objects.
We know that allocating too many objects to an application will impair the performance of the program and easily cause memory overflow. The metadata mode can be effectively avoided.
Because the number of fine-grained objects is large and their properties are similar, we divide these objects into two parts: Internal State and external State.
Internal status:
--- | The internal state is the information shared by the object, which is stored inside the object and does not change with the environment. Such as id and address.
External status:
--- | The external state is a tag on which the object depends. It is a State that changes with the environment and cannot be shared. Such as exam subjects + exam locations.
Normal Mode
Flyweight abstract meta-Role
--- | Abstract class of the product, which defines the interface or implementation of the object's external State and internal state
ConcreteFlyweight
--- | Implements obligations defined by abstract roles. This role must be aware that the internal state processing is not related to the environment. An operation cannot change the internal state while modifying the external state.
Unshared concreteflyweight: A non-shared meta-Role
--- | An object that does not have an external State or cannot use the sharing technology due to security requirements. This object is generally not displayed in the Yuan sharing factory.
FlyweightFactory
--- | The role is very simple. It is to construct a pool container and provide methods to obtain objects from the pool.
Public class FlyweightTest {}/*** abstract metadata role. * Defines the interface and Implementation of the object's external and internal states. * @ Author admin */abstract class Flyweight {// internal status private String intrinsic; // external status protected String extrinsic; // The constructor provides the external String public Flyweight (String extrinsic) {this. extrinsic = extrinsic;} // define the Business Operation public abstract void operate (); // getter and public String getIntrinsic () {return intrinsic ;} public void setIntrinsic (String intrinsic) {this. intrinsic = intrinsic;}/*** specific meta-role * @ author admin */class ConcreteFlyweight1 extends Flyweight {public ConcreteFlyweight1 (String extrinsic) {super (extrinsic );} @ Overridepublic void operate () {System. out. println ("role 1... operation ") ;}}/*** specific meta-role * @ author admin */class ConcreteFlyweight2 extends Flyweight {public ConcreteFlyweight2 (String extrinsic) {super (extrinsic );} @ Overridepublic void operate () {System. out. println ("role 2... operation ") ;}}/*** meta factory role * @ author admin */class FlyweightFactory {// define a container private static HashMap
Pool = new HashMap
(); // The metadata factory public static Flyweight getFlyweight (String extrinsic) {// the object to be returned Flyweight flyweight = null; if (! Pool. containsKey (extrinsic) {// create a metadata object flyweight = new ConcreteFlyweight1 (extrinsic) based on the external State; pool. put (extrinsic, flyweight);} else {flyweight = pool. get (extrinsic);} return flyweight ;}}
Example
A student registration system is registered and registered within three days. The daily traffic volume reaches millions, with a large number of users. OOM memory overflow occurs after the system is developed.
. There are two reasons for this problem: 1. memory overflow (the JVM cannot obtain continuous memory space due to unintentional code defects)
2. There are too many objects and the memory is exhausted.
To restrict object creation.
Public class FlyweightT {public static void main (String [] args) {// create an object for (int I = 0; I <4; I ++) {String subject = "subject" + I; for (int j = 0; j <30; j ++) {String location = "location" + j; SignInfo signInfo = SignInfoFactory. getSignInfo (subject + location) ;}} SignInfo s = SignInfoFactory. getSignInfo ("subject 3 location 4");}/*** abstract student metadata role * provides some basic information about the student. * @ Author admin */abstract class SignInfo {// student idprivate String id; // student test location private String location; // test subject private String subject; // exam private String postAddress; public String getId () {return id;} public void setId (String id) {this. id = id;} public String getLocation () {return location;} public void setLocation (String location) {this. location = location;} public String getSubject () {return subject;} public Void setSubject (String subject) {this. subject = subject;} public String getPostAddress () {return postAddress;} public void setPostAddress (String postAddress) {this. postAddress = postAddress ;}}/*** a specific meta-role, * sets the external status. * @ Author admin **/class SignInfo4Pool extends SignInfo {private String key; public SignInfo4Pool (String key) {this. key = key;} public String getKey () {return key;} public void setKey (String key) {this. key = key ;}}/*** metadata factory class. * Create an object metadata object * @ author admin **/class SignInfoFactory {private static HashMap
Pool = new HashMap
(); Public static SignInfo getSignInfo (String key) {SignInfo signInfo = null; if (pool. containsKey (key) {System. out. println ("Get From Object pool... "); signInfo = pool. get (key);} else {System. out. println ("Create a new object... "); signInfo = new SignInfo4Pool (key); pool. put (key, signInfo) ;}return signInfo ;}}
Advantages and disadvantages of the meta-mode:
This greatly reduces the number of objects created by applications, reduces memory usage, and enhances program performance.
When using the metadata mode, try to use the basic data types provided by java to improve program performance.
Use Cases:
A large number of similar objects exist in the system.
Fine-grained objects all have close external States, and their internal states are irrelevant to the environment, that is, the objects do not have a specific identity.
Scenarios requiring Buffer Pool