享元模式

2018-04-16  本文已影响0人  资深智障

结构型设计模式

简要定义

以共享的方式高效的支持大量的细粒度对象,通过复用内存中存在的对象, 降低系统创建对象实例的性能消耗.

享元模式分析

类图
20160329095258720.png

享元模式用来 减少大量实例化相似对象时的性能消耗. 在这些对象中有些信息是共享的, 有些是不能共享的, 这就涉及到享元模式的两种状态: 内蕴状态和外蕴状态, 这两种状态是相互独立的状态, 彼此没有关联

具体实现

以五子棋为例, 棋子的坐标为外蕴状态

//棋子超类
public abstract class AbstractChessman {
    //棋子类别
    protected String chess;
    //棋子坐标
    protected int x;
    protected int y;
    //构造方法
    public AbstractChessman(String chess){
        this.chess = chess;
    }
    //坐标设置
    public abstract void point(int x,int y);
    //显示棋子信息
    public void show(){
        System.out.println(this.chess+"("+this.x+","+this.y+")");
    }
}
//黑棋子类 白子同理
public class BlackChessman extends AbstractChessman {
    public BlackChessman(){
        super("●");
        System.out.println("--一颗黑棋子诞生了!--");
    }
    @Override
    public void point(int x, int y) {
        this.x = x;
        this.y = y;
        this.show();
        
    }
    
}
//棋子工厂类, 以单例模式实现
//该类用来生产棋子对象实例, 并放入缓存中, 下次在获得棋子对象时就从缓存当中获得
public class ChessmanFactory {
    //单例模式
    private static ChessmanFactory chessmanFactory = new ChessmanFactory();
    //缓存共享对象
    private final Hashtable<Character, AbstractChessman> cache = new Hashtable<Character, AbstractChessman>();
    //构造方法私有化
    private ChessmanFactory(){
    }
    //获得单例工厂对象
    public static ChessmanFactory getInstance(){
        return chessmanFactory;
    }
    /*
     * 根据字母获得棋子
     */
    public AbstractChessman getChessmanObject(char c){      
        //从缓存中获得棋子对象实例
        AbstractChessman abstractChessman = this.cache.get(c);
        //判空
        if (abstractChessman==null) {
            //说明缓存中没有该棋子对象实例,需要创建
            switch (c) {
            case 'B':
                abstractChessman = new BlackChessman();
                break;
            case 'W':
                abstractChessman = new WhiteChessman();
                break;
            default:
                System.out.println("非法字符,请重新输入!");
                break;
            }
            //如果有非法字符,那么对象必定仍为空,所以再进行判断
            if (abstractChessman!=null) {
                //放入缓存
                this.cache.put(c, abstractChessman);
            }
        }
        //如果缓存中存在棋子对象则直接返回
        return abstractChessman;
    }
}

//客户端 测试类
public class Test {
    public static void main(String[] args) {
        //创建工厂
        ChessmanFactory chessmanFactory = ChessmanFactory.getInstance();
        //随机数,用于生成棋子对象
        Random random = new Random();
        int radom = 0;
        AbstractChessman abstractChessman = null;
        //随机获得棋子
        for (int i = 0; i < 10; i++) {
            radom = random.nextInt(2);
            switch (radom) {
            case 0:
                //获得黑棋子
                abstractChessman = chessmanFactory.getChessmanObject('B');
                break;
            case 1:
                //获得黑棋子
                abstractChessman = chessmanFactory.getChessmanObject('W');
                break;
            }
            if (abstractChessman!=null) {
                abstractChessman.point(i, random.nextInt(15));
            }
        }
    }
}

享元模式的优缺点

享元模式的优点显而易见, 它能大量降低系统中对象的数量, 但这样做所付出的代价是很高的:

上一篇 下一篇

猜你喜欢

热点阅读