Java Design optimization-Enjoy meta mode

Source: Internet
Author: User

The enjoy meta-mode is one of the few design patterns in design mode that can be designed to increase system performance. Its core idea is that if there are multiple identical objects in a system, you can simply share a copy of the object without creating a new object for each use. In the enjoy meta-mode, a factory class is often used to maintain and create objects because of the need to build and maintain these shareable objects.

The main help for performance improvement in the enjoy meta mode is two points:

1. You can save the overhead of repeatedly creating objects, because the same objects maintained by the enjoy meta-mode are only created once and can save a lot of time when object creation is time consuming;

2. Due to the reduced number of objects created, all the need for system memory is also reduced, which reduces the pressure on the GC so that the system has a healthier memory structure and faster response times.

The main elements of the enjoy meta-factory, the abstract to enjoy the yuan, the specific elements and the main function of several parts. The functions of each part are as follows:

The enjoy meta-factory is at the heart of the enjoy meta-model, which needs to ensure that the system can share the same objects. The main function of the enjoy meta-mode is to reuse large shrimp (heavyweight objects) to save memory space and create time. The main application of the enjoy meta mode is the SaaS system (software as a service).

Abstract enjoy meta:

1  Public Interface Ireportmanager {2      Public String CreateReport (); 3 }

Specific benefits:

1  Public classEmployeereportmanagerImplementsIreportmanager {2     3     PrivateString tenantId =NULL;4     5     6     7      PublicEmployeereportmanager (String tenantId) {8          This. TenantId =tenantId;9System.out.println ("Employeereportmanager.employeereportmanager ()");Ten     } One  A  -  - @Override the      PublicString CreateReport () { -         return"This was a employee report"; -     } -  +}
 public  class  Financialreportmanager implements   ireportmanager{ private  String tenantId = null     < span> public   Financialreportmanager (String        TenantId) { this . TenantId = TenantId;    System.out.println ( "Financialreportmanager.financialreportmanager ()" ); } @Override  public   String CreateReport ()    { return  "This is a financial report" 

Enjoy the META factory:

1  Public classReportmanagerfactory {2     PrivateMap<string,ireportmanager> Financialreportmanager =NewHashmap<string,ireportmanager>();3     PrivateMap<string,ireportmanager> Employeereportmanager =NewHashmap<string,ireportmanager>();4     5      PublicIreportmanager Getfinancialreportmanager (String tenantId) {6Ireportmanager Ireportmanager =Financialreportmanager.get (tenantId);7         if(ireportmanager==NULL){8Ireportmanager =NewFinancialreportmanager (tenantId);9         }Ten         returnIreportmanager; One     } A      -      PublicIreportmanager Getemployeereportmanager (String tenantId) { -Ireportmanager Ireportmanager =Financialreportmanager.get (tenantId); the         if(ireportmanager==NULL){ -Ireportmanager =NewFinancialreportmanager (tenantId); -         } -         returnIreportmanager; +     } -      +}

Main method:

1  Public classTestClient {2 3      Public Static voidMain (string[] args) {4Reportmanagerfactory RMF =Newreportmanagerfactory ();5Ireportmanager rm = Rmf.getemployeereportmanager ("1233");6Rmf.getemployeereportmanager ("1233");7 System.out.println (Rm.createreport ());8     }9 Ten}

In addition, the most important difference between the enjoy meta-mode and the object pool is that the enjoy meta-pattern is not interchangeable, they each have various meanings and purposes, and objects in the object pool are equivalent and can be replaced with each other, such as database connections in the database connection pool.

Java Design optimization-Enjoy meta mode

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.