二十三种设计模式之蝇量模式(享元模式)
来源:互联网 发布:新疆大学教务网络系统 编辑:程序博客网 时间: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=========================================
阅读全文
0 0
- 二十三种设计模式之蝇量模式(享元模式)
- 二十三种设计模式之结构型模式之享元模式
- 【重读设计模式】享元模式(蝇量模式)
- 二十三 设计模式之桥梁模式
- [设计模式]二十三种设计模式
- 二十三种设计模式之观察者模式简述
- 二十三种设计模式之单例模式
- 二十三种设计模式之装饰者模式
- 二十三种设计模式之工厂模式
- 二十三种设计模式之命令模式
- 二十三种设计模式之适配器模式
- 二十三种设计模式之外观模式
- 二十三种设计模式之模板模式
- 二十三种设计模式之迭代器模式
- 二十三种设计模式之组合模式
- 二十三种设计模式之状态模式
- 二十三种设计模式之代理模式(一)
- 二十三种设计模式之桥接模式
- Windows下git实现免密登录服务器
- nodeJS基础---进程Process
- Server Tomcat v7.0 Server at localhost failed to start.错误可能原因之一
- react入门
- Oracle的登录方式和新建用户权限等
- 二十三种设计模式之蝇量模式(享元模式)
- 面向对象入门
- keras参数调优
- 51nod 排列与交换 序列dp
- PC桌面软件美化界面:几种有名的DirectUI库介绍
- Junit5显示名称:Display Names
- 类和接口
- 在webstorm中使用typescript开发react程序(一)环境搭建
- InstantRun原理(1)——初始化逻辑