二十三种设计模式之蝇量模式(享元模式)

来源:互联网 发布:新疆大学教务网络系统 编辑:程序博客网 时间:2024/04/30 15:58

蝇量模式(享元模式):通过共享方式高效支持大量细粒度对象. 将一个对象抽象出内部属性作为蝇量对象 抽象出外部属性 作为管理外部状态

特点:减少运行时对象实例的个数,节省开销和内存

public class Tree {    private int xCoord,yCoord,age;    public Tree(int xCoord, int yCoord, int age) {        this.xCoord = xCoord;        this.yCoord = yCoord;        this.age = age;    }    public void display(){    }}
public class TreeTest {    private int length=10000000;    private Tree[] treelst = new Tree[length];    public TreeTest() {        for (int i=0 ;i<length;i++){            treelst[i] = new Tree((int)(Math.random()*length),                        (int)(Math.random()*length),(int)(Math.random()*length)%5);        }    }    public void display(){        for(int i=0;i<treelst.length;i++){            treelst[i].display();        }    }}
public class MainTest {    public static void main(String[] args) {//        showInfo();//        TreeTest treeTest;//        treeTest = new TreeTest();////        showInfo();//        treeTest.display();//        showInfo();//        showInfo();////        TreeManager treeManager;//        treeManager = new TreeManager();////        showInfo();//        treeManager.displayTrees();//        showInfo();        showInfo();        PlantManager plantManager;        plantManager = new PlantManager();        showInfo();        plantManager.display();        showInfo();    }    public static void showInfo(){        //最大内存        long max = Runtime.getRuntime().maxMemory();        //分配内存        long total = Runtime.getRuntime().totalMemory();        //空闲内存        long freeMemory = Runtime.getRuntime().freeMemory();        //已使用内存        long used = total -freeMemory;        System.out.println("max="+max);        System.out.println("total="+total);        System.out.println("freeMemory="+freeMemory);        System.out.println("used="+used);        System.out.println("时间="+System.currentTimeMillis());        System.out.println("=========================================");    }}
public class TreeFlyWeight {    public TreeFlyWeight() {    }    public void display(int xCoord,int yCoord,int age){//        System.out.println('x');    }}
public class TreeManager {    private int length = 10000000;    int[] xArray = new int[length],yArray = new int[length],AgeArray = new int[length];    private TreeFlyWeight treeFlyWeight;    public TreeManager() {        treeFlyWeight = new TreeFlyWeight();        for(int i=0;i<length;i++){            xArray[i] = (int)(Math.random()*length);            yArray[i] = (int)(Math.random()*length);            AgeArray[i] = (int)(Math.random()*length)%5;        }    }    public void displayTrees(){        for (int i=0;i<length;i++){            treeFlyWeight.display(xArray[i],yArray[i],AgeArray[i]);        }    }}
public abstract class Plant {    public Plant(){}    public abstract void display(int xCoord,int yCoord,int age);}
public class Tree2 extends Plant {    @Override    public void display(int xCoord, int yCoord, int age) {        //        System.out.println("Tree2 x");    }}
public class Grass extends Plant {    @Override    public void display(int xCoord, int yCoord, int age) {//        System.out.println("Grass x");    }}
public class PlantFactory {    private HashMap<Integer,Plant> plantHashMap = new HashMap<Integer,Plant>();    public PlantFactory() {    }    public Plant getPlant(int type){        if(!plantHashMap.containsKey(type)){            switch (type){                case 0:                    plantHashMap.put(0,new Tree2());                    break;                case 1:                    plantHashMap.put(1,new Grass());                    break;            }        }        return plantHashMap.get(type);    }}
public class PlantManager {    private int length = 10000000;    private int[] xArray = new int[length],yArray = new int[length],    AgeArray = new int[length],typeArray = new int[length];    private PlantFactory plantFactory;    public PlantManager(){        plantFactory = new PlantFactory();        for(int i=0;i<length;i++){            xArray[i] = (int)(Math.random()*length);            yArray[i] = (int)(Math.random()*length);            AgeArray[i] = (int)(Math.random()*length)%5;            typeArray[i] =  (int)(Math.random()*length)%2;        }    }    public void display(){        for(int i=0;i<length;i++){            plantFactory.getPlant(typeArray[i]).display(xArray[i],yArray[i],AgeArray[i]);        }    }}
*******************************传统方式实现**************************************max=859832320total=58720256freeMemory=56509424used=2210832时间=1502804524796=========================================max=859832320total=350224384freeMemory=69184288used=281 040 096时间=1502804532526=========================================max=859832320total=350224384freeMemory=69184288used=281040096时间=1502804532542=========================================*******************************蝇量实现**************************************max=859832320total=58720256freeMemory=56509424used=2210832时间=1502805198436=========================================max=859832320total=139460608freeMemory=17249728used=12 2210 880时间=1502805199977=========================================max=859832320total=139460608freeMemory=17249728used=12 2210 880时间=1502805199992=========================================*******************************增加草**************************************max=859832320total=58720256freeMemory=56509424used=2210832时间=1502806741592=========================================max=859832320total=179830784freeMemory=17619888used=16 2210 896时间=1502806743749=========================================max=859832320total=179830784freeMemory=17619888used=162210896时间=1502806744340=========================================

原创粉丝点击