蝇量模式(flyweight)

·如果一个应用程序使用大量的对象,造成很大的存储开销

·蝇量(享元)模式:
    ·蝇量模式运用共享技术有效地支持大量细粒度的对象
    ·蝇量模式的重点在于区分对象的共享变量(内部状态)和不可共享变量(外部状态,将此类变量从类从剔除,由外部传入)
    ·蝇量的优点:
        ·减少运行时对象实例的个数,节省内存
        ·将许多“虚拟”对象的状态集中管理
    ·蝇量的用途:
        ·当一个类有许多的实例,而这些实例能被同一方法控制的时候,我们就可以使用蝇量模式
    ·蝇量的缺点:
        ·享元模式使得系统更加复杂。为了使对象可以共享,需要将一些状态外部化,这使得程序的逻辑复杂化。另外它将享元对象的状态外部化,而读取外部状态使得运行时间稍微变长
·简单描述:
    ·享元类, 将享元类的状态分为共享变量(内部状态)和不可共享变量(外部状态,将此类变量从类从剔除,由外部传入)
    ·创建并管理共享对象的类,当用户请求一个共享对象时,该类的对象提供一个已创建的实例或者创建一个(如果不存在的话)
    ·客户类,持有享元类对象引用,并计算或存储其外部状态

public abstract class Tree {
    protected double x;
    protected double y;
    protected int age;
    protected String name;
    public Tree(){
    }
    public Tree(int age){
        this.age = age;
    }
    public Tree(double x, double y, int age){
        this.x = x;
        this.y = y;
        this.age = age;
    }
    public void display(){
        String prompt = null;
        if(age>10){
            prompt = "old";
        }else{
            prompt = "young";
        }
        System.out.println("["+prompt+":x-"+x+", y-"+y+"]");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

----------------------

public class PhoenixTree extends Tree {
    public PhoenixTree(){
        super();
        this.name = "PhoenixTree";
    }
    @Override
    public void display() {
        System.out.print(name+"#");
        super.display();
    }
}
----------------------

public class Peach extends Tree {
    public Peach(){
        super();
        this.name = "Peach";
    }
    @Override
    public void display() {
        System.out.print(name+"#");
        super.display();
    }
}

----------------------

public class Osier extends Tree {
    public Osier(){
        super();
        this.name = "Osier";
    }
    @Override
    public void display() {
        System.out.print(name+"#");
        super.display();
    }
}

----------------------

public class DummyTree extends Tree {
    public DummyTree(){
        super();
        this.name = "DummyTree";
    }
    @Override
    public void display() {
        System.out.print(name+"#");
        super.display();
    }
}

----------------------

public class TreeManager {
    private static volatile TreeManager instance;
    private static HashMap<String,Tree> pool= new HashMap<String,Tree>();
    private TreeManager(){
        pool.put("Osier", new Osier());
        pool.put("PhoenixTree", new PhoenixTree());
    }
    public static TreeManager getInstance(){
        if(instance==null){
            synchronized (TreeManager.class) {
                if(instance==null){
                    instance = new TreeManager();
                }
            }
        }
        return instance;
    }
    @SuppressWarnings("finally")
    public Tree getTree(String name){
        Tree tree = null;
        tree = pool.get(name);
        if(tree != null){
            return tree;
        }else{
            try {
                Class<?> clazz = Class.forName("com.pattern.flyweight."+name);
                pool.put(name, (Tree)clazz.newInstance());
            } catch (Exception e) {
                if(!pool.containsKey("DummyTree")){
                    pool.put("DummyTree", new DummyTree());    
                }
                name = "DummyTree";    
            }finally{
                return pool.get(name);
            }
        }
    }
}

----------------------

public class Park {
    Tree tree;
    public void draw(){
        double[][] treesPosInfo = {
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2},
                {12.3,19.2}, {2.3,1.2}, {32.3,9.2},
                {112.3,129.2}, {11.3,1329.2}, {1211.3,1.2}
            };
        int[] treesAgeInfo = {7, 8, 9, 10, 11, 12, 7, 8, 9, 10, 11, 12
                , 7, 8, 9, 10, 11, 12, 7, 8, 9, 10, 11, 12
                , 7, 8, 9, 10, 11, 12, 7, 8, 9, 10, 11, 12
                , 7, 8, 9, 10, 11, 12, 7, 8, 9, 10, 11, 12
                , 7, 8, 9, 10, 11, 12, 7, 8, 9, 10, 11, 12
                };
        TreeManager manager = TreeManager.getInstance();
        for(int i=0; i<treesPosInfo.length; i++){
            System.out.print("tree@"+i+"\t");
            int temp = i%4;
            if(temp==0){
                tree = manager.getTree("Osier");
                tree.setX(treesPosInfo[i][0]);
                tree.setY(treesPosInfo[i][1]);
                tree.setAge(treesAgeInfo[i]);
                tree.display();
            }else if(temp==1){
                tree = manager.getTree("PhoenixTree");
                tree.setX(treesPosInfo[i][0]);
                tree.setY(treesPosInfo[i][1]);
                tree.setAge(treesAgeInfo[i]);
                tree.display();
            }else if(temp==2){
                tree = manager.getTree("Peach");
                tree.setX(treesPosInfo[i][0]);
                tree.setY(treesPosInfo[i][1]);
                tree.setAge(treesAgeInfo[i]);
                tree.display();
            }else{
                tree = manager.getTree("apple");
                tree.setX(treesPosInfo[i][0]);
                tree.setY(treesPosInfo[i][1]);
                tree.setAge(treesAgeInfo[i]);
                tree.display();
            }
        }
    }
}

----------------------

原文地址:https://www.cnblogs.com/growup/p/2022669.html