享元模式

public interface IReportManager {
    public String createReport();
}
//财务报表
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";
    }
}
// 员工报表
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";
    }
}
import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂,以tenantId为索引,维护了一个享元对象的集合,它确保相同tenantId的请求都返回同一个享元实例,
 * 确保享元对象的有效复用
 *
 * 享元模式,即共享对象,在某些对象需要重复创建,且最终只需要得到单一结果的情况下使用。
 * 因为此种模式是利用先前创建的已有对象,通过某种规则去判断当前所需对象是否可以利用原有对象做相应修改后得到想要的效果
 *  享元模式的优点在于它大幅度地降低内存中对象的数量。但是,它做到这一点所付出的代价也是很高的:
 *   - 享元模式使得系统更加复杂。为了使对象可以共享,需要将一些状态外部化,这使得程序的逻辑复杂化。
 *   - 享元模式将享元对象的状态外部化,而读取外部状态使得运行时间稍微变长。
 */
public class ReportManagerFactory {
    Map<String,IReportManager> financialReportManager = new HashMap<>();
    Map<String,IReportManager> employeeReportManager = new HashMap<>();
    //通过tenantId获取享元
    public IReportManager getFinancialReportManager(String tenantId){
        IReportManager r = financialReportManager.get(tenantId);
        if (r==null){
            r = new FinancialReportManager(tenantId);
            financialReportManager.put(tenantId,r);
        }
        return r;
    }
    public IReportManager getEmployeeReportManager(String tenantId){
        IReportManager r = employeeReportManager.get(tenantId);
        if (r==null){
            r = new EmployeeReportManager(tenantId);
            employeeReportManager.put(tenantId,r);
        }
        return r;
    }
}

import com.mod.IReportManager;
import com.mod.ReportManagerFactory;

/**
 * 测试
 */
public class ReportManagerTest {
    public static void main(String[] args){
        ReportManagerFactory factory = new ReportManagerFactory();
        IReportManager er = factory.getEmployeeReportManager("a");
        System.out.println(er.createReport());
    }
}

在五子棋上的应用


/**
 * 五子棋
 */
public abstract class AbStractChessman {
    //棋子坐标
    protected int x;
    protected int y;
    //黑白
    protected String chess;
    public AbStractChessman(String chess) {
        this.chess = chess;
    }
    //棋子坐标
    public abstract void point(int x,int y);
    public void show(){
        System.out.println(chess+"("+this.x+","+this.y+")");
    }
}
//黑子
public class BlackChessman extends AbStractChessman {
    public BlackChessman() {
        super("●");
    }

    @Override
    public void point(int x, int y) {
        this.x = x;
        this.y = y;
        show();
    }
}
//白子
public class WhiteChessman extends AbStractChessman {
    public WhiteChessman() {
        super("○");
    }

    @Override
    public void point(int x, int y) {
        this.x = x;
        this.y = y;
        show();
    }
}

import java.util.Hashtable;

/**
 * 棋子工厂
 */
public class FiveChessmanFactory  {
    private FiveChessmanFactory(){}
    //单例模式工厂
    private static FiveChessmanFactory fiveChessmanFactory = new FiveChessmanFactory();
    //缓存存放共享对象
    private final Hashtable<Character,AbStractChessman> cache = new Hashtable<>();
    public static FiveChessmanFactory getInstance(){
        return fiveChessmanFactory;
    }
    public AbStractChessman getChessmanObject(char c){
        AbStractChessman abStractChessman = cache.get(c);
        if (abStractChessman==null){
            switch (c){
                case 'B':
                    abStractChessman = new BlackChessman();
                    break;
                case 'W':
                    abStractChessman = new WhiteChessman();
                    break;
                default:
                    break;
            }
        }
        return abStractChessman;
    }
}

测试


import com.mod.AbStractChessman;
import com.mod.FiveChessmanFactory;

import java.util.Random;

//测试
public class FiveChessmanTest {
    public static void main(String[] args){
        FiveChessmanFactory factory = FiveChessmanFactory.getInstance();
        AbStractChessman abStractChessman = null;
        int r = 0;
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            r = random.nextInt(2);
            switch (r){
                case 0:
                    abStractChessman = factory.getChessmanObject('B');
                    break;
                case 1:
                    abStractChessman = factory.getChessmanObject('W');
                    break;
            }
            if (abStractChessman!=null){
                abStractChessman.point(i,random.nextInt(15));
            }
        }
    }
    //●(0,13)
    //○(1,7)
    //●(2,12)
    //○(3,9)
    //●(4,6)
    //○(5,5)
    //●(6,4)
    //○(7,13)
    //●(8,3)
    //○(9,14)
}
posted @ 2019-08-27 11:06  fly_bk  阅读(160)  评论(0编辑  收藏  举报