设置模式之 -> 享元模式

package javatuning.ch2.flyweight;

public interface IReportManager {
	public String createReport();
}
package javatuning.ch2.flyweight;

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";
	}
}
package javatuning.ch2.flyweight;

public class FinancialReportManager implements IReportManager {
	protected String tenantId=null;
	public FinancialReportManager(String tenantId){
		this.tenantId=tenantId;
	}
	@Override
	public String createReport() {
		return "This is a financial report";
	}
}
package javatuning.ch2.flyweight;

import java.util.HashMap;
import java.util.Map;

public class ReportManagerFactory {
	
	Map<String ,IReportManager> financialReportManager=new HashMap<String ,IReportManager>();
	Map<String ,IReportManager> employeeReportManager=new HashMap<String ,IReportManager>();
	
	IReportManager getFinancialReportManager(String tenantId){
		IReportManager r=financialReportManager.get(tenantId);
		if(r==null){
			r=new FinancialReportManager(tenantId);
			financialReportManager.put(tenantId, r);
		}
		return r;
	}
	
	IReportManager getEmployeeReportReportManager(String tenantId){
		IReportManager r=employeeReportManager.get(tenantId);
		if(r==null){
			r=new EmployeeReportManager(tenantId);
			employeeReportManager.put(tenantId, r);
		}
		return r;
	}
}
package javatuning.ch2.flyweight;

public class Main {

	public static void main(String[] args) {
		ReportManagerFactory rmf=new ReportManagerFactory();
		IReportManager rm=rmf.getFinancialReportManager("A");
		System.out.println(rm.createReport());
	}
}
发布了176 篇原创文章 · 获赞 1 · 访问量 7178

猜你喜欢

转载自blog.csdn.net/qq_37769323/article/details/104173318