享元模式是设计模式中少数几个以提高系统性能为目的的模式,核心思想:如果在一个系统中存在多个相同的对象,那么只需要共享一份对象的拷贝,而不必为每一次使用都创建新的对象。在享元模式中,由于需要构造和维护这些可以共享的对象,因此,使用工厂类来维护和创建对象。
享元模式对性能提升主要有两点:节省重复创建对象的开销;由于创建对象的数量减少,对系统内存的需求减小,也减小了GC的压力。
角色:享元工厂–用以创建具体享元类,维护相同的享元对象,能够保证相同的享元对象可以被系统共享,其内部使用了类似单例模式的算法,当请求对象已经存在时,直接返回对象,不存在创建对象;抽象享元—定义需要共享的对象的业务接口,享元类被创建出来总是为了实现某些特定的业务逻辑,而抽象享元变定义这些逻辑的语义行为;具体享元类—实现抽象享元的接口,完成某一具体逻辑;Main—-使用享元模式的组件,通过享元工厂取得享元对象。
享元工厂是享元模式的核心,确保系统可以共享相同的对象。一般情况下,享元工厂会维护一个对象列表,当任何组件尝试获取享元类时,如果请求的享元类已经被创建,则直接返回已有的享元类,没有则创建一个新的享元对象,并将它加入到维护队列中。
享元模式的应用场景:假设甲、乙、丙三个公司为某个系统的用户,则定义每个公司为这套系统的一个租户。每个租户有100名员工,如果这些员工可以登录这套系统查看自己的收入情况,并且为了系统安全,每个租户拥有自己的数据库。为使系统设计最为合理,在这种情况下,使用享元模式为每个租户分别提供工资查询的接口,而每个租户下的所有员工共享一个查询。
享元工厂和对象池的区别:在一个对象池中,所有的对象都是等价的,任意两个对象在任何使用场景中都可以被对象池中的其他对象代替。在享元模式中,享元工厂所维护的所有对象都是不同的,任何两个对象间不能相互代替。
定义抽象享元:
public interface IReportManager {
String createReport();
}
定义具体享元类:
public class FinancialReportManager implements IReportManager{
protected String tenantId ;
public FinancialReportManager(String tenantId){
System.out.println("初始化财政报表");
this.tenantId = tenantId;
}
public String createReport() {
return "This is a finanicial report";
}
}
public class EmployeeReportManager implements IReportManager {
protected String tenantId ;
public EmployeeReportManager(String tenantId){
System.out.println("初始化个人报表");
this.tenantId = tenantId;
}
public String createReport() {
return "This is a employee report";
}
}
定义享元工厂类:
package com.performance.optimization.design.flyweight;
import java.util.HashMap;
import java.util.Map;
/**
* 享元工厂
* @author qiaolin
*
*/
public class ReportManagerFactory {
private static final Map<String,IReportManager> financialReportManager = new HashMap<String,IReportManager>();
private static final Map<String,IReportManager> employeeReportManager = new HashMap<String,IReportManager>();
/**
* 获取财政报表对象
* @param tenantId
* @return
*/
public static IReportManager getFinancialReportManager(String tenantId){
IReportManager reportManager = financialReportManager.get(tenantId);
if(reportManager == null){
reportManager = new FinancialReportManager(tenantId);
financialReportManager.put(tenantId, reportManager);
}
return reportManager;
}
/**
* 获取个人报表对象
* @param tenantId
* @return
*/
public static IReportManager getEmployeeReportManager(String tenantId){
IReportManager reportManager = employeeReportManager.get(tenantId);
if(reportManager == null){
reportManager = new EmployeeReportManager(tenantId);
employeeReportManager.put(tenantId, reportManager);
}
return reportManager;
}
}
测试用例:
@Test
public void test(){
//此时会创建employeeReportManager对象
IReportManager employeeReportManager = ReportManagerFactory.getEmployeeReportManager("A");
System.out.println(employeeReportManager.createReport());
//此时会创建employeeReportManager对象
IReportManager reportManager = ReportManagerFactory.getFinancialReportManager("A");
System.out.println(reportManager.createReport());
//此时不会创建employeeReportManager对象,直接取已经创建的employeeReportManager对象
reportManager = ReportManagerFactory.getEmployeeReportManager("A");
System.out.println(reportManager.createReport());
//此时是同一个对象
System.out.println(reportManager == employeeReportManager );
}
测试结果:
初始化个人报表
This is a employee report
初始化财政报表
This is a finanicial report
This is a employee report
true