Sharing Mode = Singleton mode + factory mode + merging mode
Singleton mode:
Ensure that a class has only one instance and provides a global access point to it.
Structure:
Pay attention to the multi-threaded Singleton.
[Java]
Package com. bankht. Flyweight. complex;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 03:25:14
*
* @ Class description: Singleton
*/
Public class Singleton {
Private static Singleton instance;
Private static final Object syncRoot = new Object ();
Private Singleton (){
}
Public static Singleton getInstance (){
If (instance = null ){
Synchronized (syncRoot ){
Instance = new Singleton ();
}
}
Return instance;
}
}
Package com. bankht. Flyweight. complex;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 03:25:14
*
* @ Class description: Singleton
*/
Public class Singleton {
Private static Singleton instance;
Private static final Object syncRoot = new Object ();
Private Singleton (){
}
Public static Singleton getInstance (){
If (instance = null ){
Synchronized (syncRoot ){
Instance = new Singleton ();
}
}
Return instance;
}
}
Metadata mode:
Enjoy yuan is Flyweight, a professional term in sports. It is the most lightweight in boxing, wrestling, and weightlifting competitions. Porting this word to a software project is also used to represent a very small object, that is, a fine-grained object. The Flyweight object can be understood as a shared object, that is, a shared fine-grained object. The metadata sharing mode enables efficient support for a large number of fine-grained objects by means of sharing. Its purpose is to save occupied space resources and improve system performance.
The metadata mode is used to share objects, that is, to use a shared pool to reduce memory allocation overhead. The sharing mode is usually used together with the factory mode. It contains multiple shared composite objects. Therefore, the sharing mode = Singleton mode + factory mode + merging mode. Its structure is 12-25.
The FlyweightFactoiy metadata factory is responsible for creating and managing the metadata objects. It must ensure that the metadata objects can be shared by the system. When a client object requests a metadata object, the metadata factory needs to check whether there is a qualified metadata object in the system. If yes, the Yuan-sharing factory should provide the existing yuan-sharing object. if the system does not have an appropriate Yuan-sharing object, the Yuan-sharing factory should create a new suitable Yuan-sharing object. Flyweight is a superclass of all the specific Metadata classes. It specifies public interfaces or abstract classes to be implemented for these classes. MyFlyweight1 and MyFlyweight2 are the interfaces defined by abstract metadata.
Author: m13666425773