JAVA and mode 12th-yuan mode = Singleton mode + factory mode + Synthesis Mode

Source: Internet
Author: User

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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.