Java Design Optimization-enjoy the metadata mode and java Design Optimization

Source: Internet
Author: User

Java Design Optimization-enjoy the metadata mode and java Design Optimization

The enjoy mode is a few design patterns in the design pattern that aim to increase the system performance. Its core idea is: if multiple identical objects exist in a system, you only need to share a copy of the object, instead of creating a new object for each use. In the shared object mode, a factory class is often used to maintain and create objects because you need to build and maintain these objects that can be shared.

The meta-mode provides two major benefits for performance improvement:

1. You can save the overhead of repeatedly creating objects, because the same objects maintained in the enjoy meta mode will only be created once. When the object creation is time-consuming, it can save a lot of time;

2. as the number of created objects decreases, all requirements for system memory are also reduced, which reduces the GC pressure accordingly, in this way, the system has a healthier memory structure and a faster response speed.

The metadata mode consists of the metadata factory, abstract metadata, specific metadata, and main functions. The functions are as follows:

The metadata factory is the core of the metadata model. It must ensure that the system can share the same objects. The primary role of the meta-mode is to reuse prawns (heavyweight objects) to reduce the memory space and creation time. The main application of the shared-cost model is the software as a service (SAAS ).

Abstract metadata:

1 public interface IReportManager {2     public String createReport();3 }

Specific metadata:

 1 public class EmployeeReportManager implements IReportManager { 2      3     private String tenantId = null; 4      5      6      7     public EmployeeReportManager(String tenantId) { 8         this.tenantId = tenantId; 9         System.out.println("EmployeeReportManager.EmployeeReportManager()");10     }11 12 13 14     @Override15     public String createReport() {16         return "This is a employee report";17     }18 19 }
public class FinancialReportManager implements IReportManager{    private String tenantId = null;    public FinancialReportManager(String tenantId) {        this.tenantId = tenantId;        System.out.println("FinancialReportManager.FinancialReportManager()");    }    @Override    public String createReport() {        return "This is a financial report";    }                }

Enjoy yuan Factory:

 1 public class ReportManagerFactory { 2     private Map<String,IReportManager> financialReportManager = new HashMap<String,IReportManager>(); 3     private Map<String,IReportManager> employeeReportManager = new HashMap<String,IReportManager>(); 4      5     public IReportManager getFinancialReportManager(String tenantId){ 6         IReportManager iReportManager = financialReportManager.get(tenantId); 7         if(iReportManager==null){ 8             iReportManager = new FinancialReportManager(tenantId); 9         }10         return iReportManager;11     }12     13     public IReportManager getEmployeeReportManager(String tenantId){14         IReportManager iReportManager = financialReportManager.get(tenantId);15         if(iReportManager==null){16             iReportManager = new FinancialReportManager(tenantId);17         }18         return iReportManager;19     }20     21 }

Main method:

 1 public class TestClient { 2  3     public static void main(String[] args) { 4         ReportManagerFactory rmf = new ReportManagerFactory(); 5         IReportManager rm = rmf.getEmployeeReportManager("1233"); 6         rmf.getEmployeeReportManager("1233"); 7         System.out.println(rm.createReport()); 8     } 9 10 }

In addition, the biggest difference between the object-sharing mode and the Object pool is that the object-sharing mode is irreplaceable. They have different meanings and purposes, and the objects in the object pool are equivalent, can be replaced with each other, such as database connection in the database connection pool.

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.