定义: 享元模式是设计模式中少数几个以提高系统性能为目的的模式。 如果在一个系统中存在多个相同的对象,那么只需共享一份对象的拷贝,而不必为每一次使用都创建新的对象。
优势: 1、可以节省重复创建对象的开销,因为被享元模式维护的相同对象只会被创建一次,当创建对象比较耗时,便可以节省大量时间。 2、由于创建对象数量减少,所以对系统内存的需求也减少,将使GC的压力也相应的降低,进而使得系统拥有一个更健康的内存结构和更快的反应速度。
享元模式与对象池的区别: 在一个对象池中,所有的对象都是等价的,任意两个对象在任何使用场景中都可以被对象池中的其他对象代替。而在享元模式中,享元工厂所维护的所有对象都是不同的,任何两个对象之间不能相互代替。
/**
* 所有报表生成类将作为享元对象在一个公司中共享
*
* @author PC
* @date 2022/07/12
**/
public interface IReportManager {
public String createReport();
}
/**
* 具体享元类 财务报表
*
* @author PC
* @date 2022/07/12
**/
public class FinancialReportManager implements IReportManager {
protected String tenantId = null;
public FinancialReportManager(String tenantId){
this.tenantId = tenantId;
}
@Override
public String createReport() {
return "This is a finnancial report";
}
}
/**
* 具体享元类 员工报表
*
* @author PC
* @date 2022/07/12
**/
public class EmployeeReportManager implements IReportManager {
protected String tenantId = null;
public EmployeeReportManager(String tenantId){
this.tenantId = tenantId;
}
@Override
public String createReport() {
return "This is a employee report";
}
}
享元工厂: 确保同一个公司使用相同的对象产生报表。
/**
* 享元工厂类
*
* @author PC
* @date 2022/07/12
**/
public class ReportManagerFactory {
Map<String,IReportManager> financialReportManager =
new HashMap<>();
Map<String, IReportManager> employeeReportManager =
new HashMap<>();
//通过租户ID获取享元
IReportManager getFinancialReportManager(String tenantId){
IReportManager r = financialReportManager.get(tenantId);
if(r == null){
r = new FinancialReportManager(tenantId);
//维护已创建的享元对象
financialReportManager.put(tenantId, r);
}
return r;
}
//通过租户ID获取享元
IReportManager getEmployeeReportManager(String tenantId){
IReportManager r = employeeReportManager.get(tenantId);
if(r == null){
r = new EmployeeReportManager(tenantId);
//维护已创建的享元对象
employeeReportManager.put(tenantId, r);
}
return r;
}
}
ReportManagerFactory作为享元工厂,以租户的ID为索引,维护了一个享元对象的集合,它确保相同租户的请求都返回同一个享元实例,从而确保享元对象的有效复用。