定义
通过共享的方式高效地支持大量细粒度的对象。
例子
景观设计软件项目:现在有一个设计景观展示的项目,项目内部已经有的对象有树,树的属性有年龄,x、y的坐标。现在要表示成一个森林,则需要通过大量的树模拟成森林
每一颗树的属性和方法都是相同的,只是值不同
传统方法就是定义大量的树对象,当成森林使用时则循环遍历每一颗树的内部方法就行。
/**
* 传统模式,树的对象,包含树的x,y坐标和年龄
*/
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.print("x");
}
}
/**
* 测试传统模式,一个对象一个类,用到时循环遍历
*/
public class TreesTest {
private int length = 10000000;
private Tree[] treelst = new Tree[length];
public TreesTest() {
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, len = treelst.length; i < len; i++) {
treelst[i].display();
}
}
}
/**
* 传统模式,将树定义成单个对象,模拟森林时则直接通过遍历遍历一个个的对象,占用内存较大,耗时较长
*/
public class MainTest {
public static void main(String[] args) {
showMemInfo();
TreesTest mTreesTest;
mTreesTest = new TreesTest();
showMemInfo();
mTreesTest.display();
showMemInfo();
}
public static void showMemInfo() {
// 最大内存:
long max = Runtime.getRuntime().maxMemory();
// 分配内存:
long total = Runtime.getRuntime().totalMemory();
// 已分配内存中的剩余空间 :
long free = Runtime.getRuntime().freeMemory();
// 已占用的内存:
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();
}
}
结果
//结果
Connected to the target VM, address: '127.0.0.1:3763', transport: 'socket'
最大内存 = 2844786688
已分配内存 = 192937984
已分配内存中的剩余空间 = 187881512
已用内存 = 5056472
时间 = 1533525208855
最大内存 = 2844786688
已分配内存 = 534249472
已分配内存中的剩余空间 = 253427904
已用内存 = 280821568
时间 = 1533525214648
Disconnected from the target VM, address: '127.0.0.1:3763', transport: 'socket'
最大内存 = 2844786688
已分配内存 = 534249472
已分配内存中的剩余空间 = 253427904
已用内存 = 280821568
时间 = 1533525214662
使用蝇量模式
由于每一个树对象的属性和方法都是相同的,只是属性的值不同。则可以蝇量模式,定义一个树的管理者对象,内部定义多个集合来存储所有树的单个属性,则当成森林调用时只用调用这个管理者对象即可。
public class TreeFlyWeight {
public TreeFlyWeight() {
}
public void display(int x, int y, int age) {
// System.out.print("x");
}
}
/**
* 蝇量模式,重新定义一个对象模拟森林,在对象内部用集合重新定义多个树对象的属性
*/
public class TreeManager {
private int length = 10000000;
int[] xArray = new int[length], yArray = new int[length],
AgeArray = new int[length];
private TreeFlyWeight mTreeFlyWeight;
public TreeManager() {
mTreeFlyWeight = 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++) {
mTreeFlyWeight.display(xArray[i], yArray[i], AgeArray[i]);
}
}
}
/**
* 蝇量模式,定义新的对象将树对象的各属性内部定义成集合,则调用时只用调用这一个对象即可
* 占用内存更少,耗时更短
*/
public class MainTest {
public static void main(String[] args) {
showMemInfo();
TreeManager mTreeManager;
mTreeManager = new TreeManager();
showMemInfo();
mTreeManager.displayTrees();
showMemInfo();
}
public static void showMemInfo() {
// 已分配内存中的剩余空间 :
long free = Runtime.getRuntime().freeMemory();
// 分配内存:
long total = Runtime.getRuntime().totalMemory();
// 最大内存:
long max = Runtime.getRuntime().maxMemory();
// 已占用的内存:
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();
}
}
//结果
Connected to the target VM, address: '127.0.0.1:3867', transport: 'socket'
最大内存 = 2844786688
已分配内存 = 192937984
已分配内存中的剩余空间 = 187881512
已用内存 = 5056472
时间 = 1533526134677
最大内存 = 2844786688
已分配内存 = 192937984
已分配内存中的剩余空间 = 67881464
已用内存 = 125056520
时间 = 1533526135882
Disconnected from the target VM, address: '127.0.0.1:3867', transport: 'socket'
最大内存 = 2844786688
已分配内存 = 192937984
已分配内存中的剩余空间 = 67881464
已用内存 = 125056520
时间 = 1533526135894
总结
优点
- 减少运行时的对象实例个数,节省创建开销和内存
- 将许多“虚拟”对象的状态集中管理
缺点
- 系统设计更加复杂
- 需要专门维护对象的外部状态
适用场合:
- 需要大量细粒度对象
- 这些对象的外部状态不多
- 按照内部状态分成几个组,每一个组都仅用一个蝇量对象代替