-
蝇量模式(轻量级模式)也叫享元模式(共用一个方法)
-
景观设计软件项目遇到的问题:
-
设置一个公园或者小区,会有树的大小,外观等等
-
虽然对象不复杂,但是如果量大就会消耗很多内存,比如10000000棵树
-
-
代码分析:
package yingliangmoshi.old;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:07
*/
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(){
// System.out.println(xCoord);
}
}
package yingliangmoshi.old;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:09
*/
public class TreesTest {
private int length = 1000000;
private Tree[] treeList = new Tree[length];
public TreesTest(){
for(int i = 0;i<length;i++){
treeList[i] = new Tree((int)(Math.random() * length), (int)(Math.random() * length), (int)(Math.random() * length)%5);
}
}
public void display() {
for(int i = 0; i<treeList.length;i++){
treeList[i].display();
}
}
}
package yingliangmoshi.old;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:12
*/
public class MainTest {
public static void main(String[] args) {
showInfo();
TreesTest treesTest;
treesTest = new TreesTest();
showInfo();
treesTest.display();
showInfo();
}
public static void showInfo(){
long max = Runtime.getRuntime().maxMemory();
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().totalMemory();
long used = total - free;
System.out.println("最大内存= "+max);
System.out.println("已分配内存= "+ total);
System.out.println("已分配剩余内存内存= "+free);
System.out.println("已用内存= "+used);
System.out.println("当前时间="+System.currentTimeMillis());
System.out.println();
}
}
-
这些树之间有什么关系,大量的对象导致内存泄漏,如何将对象变少:
-
分成两种状态:
-
内部状态:可以公用的放在一起的 display()
-
外部状态:xCoord, yCoord, age
- 内部状态放到类里面
- 外部状态简化成一个树管理的类来管理外部状态
- 显示的时候使用外部状态在调用内部状态
-
-
代码分析:
package yingliangmoshi.TreeFlyWeight;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:25
*/
/**
* 内部状态
*/
public class TreeFlyWeight {
public TreeFlyWeight(){
}
//只放一个内部状态
public void display(int xCoord, int yCoord, int age){
// System.out.println(xCoord);
}
}
package yingliangmoshi.TreeFlyWeight;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:26
*/
/**
* 处理外部状态
*/
public class TreeManager {
public int length = 1000000;
//将外部状态放到数组中
int[] xArray = new int[length];
int[] yArray = new int[length];
int[] 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]);
}
}
}
package yingliangmoshi.TreeFlyWeight;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:29
*/
public class MainTest {
public static void main(String[] args) {
showInfo();
TreeManager treeManager;
treeManager = new TreeManager();
showInfo();
treeManager.displayTrees();
showInfo();
}
public static void showInfo(){
long max = Runtime.getRuntime().maxMemory();
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().totalMemory();
long used = total - free;
System.out.println("最大内存= "+ max);
System.out.println("已分配内存= "+ total);
System.out.println("已分配剩余内存内存= "+ free);
System.out.println("已用内存= " + used);
System.out.println("当前时间="+System.currentTimeMillis());
System.out.println();
}
}
- 不管项目变得多么复杂:
当存在多个细粒度对象的时候,找到公共方法,抽象成一个抽象类