设计模式--蝇量模式(享元模式)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiao__jia__jia/article/details/86776640

              设计模式--蝇量模式

蝇量模式原理

景观设计软件项目遇到的问题

树:XY坐标,树的大小,外观,需要很多树

10000000棵树
思考如何设计

传统方式:

Tree

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");
	}
}


TreesTest

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();
		}
	}

}


MainTest


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("");

	}

}

思考:这些树之间有啥关系      
内部状态和外部状态                 
 

 

TreeFlyWeight

public class TreeFlyWeight {

	public TreeFlyWeight() {

	}

	public void display(int xCoord, int yCoord, int age) {
		// System.out.print("x");
	}

}


TreeManager

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]);
		}
	}

}


MainTest

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("");
	}

}




蝇量模式原理


蝇量模式:通过共享的方式高效地支持大量细粒度的对象。

蝇量模式优缺点

优点:

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

将许多“虚拟”对象的状态集中管理

缺点:

系统设计更加复杂

需要专门维护对象的外部状态

蝇量模式示例代码讲解示例项目类结构



Plant

public abstract class Plant {

	public Plant() {

	}

	public abstract void display(int xCoord, int yCoord, int age);

}


Grass

public class Grass extends Plant {

	@Override
	public void display(int xCoord, int yCoord, int age) {
		// TODO Auto-generated method stub
		// System.out.print("Grass x");
	}

}


Tree

public class Tree extends Plant {

	@Override
	public void display(int xCoord, int yCoord, int age) {
		// TODO Auto-generated method stub
		// System.out.print("Tree x");
	}

}


PlantFactory

import java.util.HashMap;

public class PlantFactory {

	private HashMap<Integer, Plant> plantMap = new HashMap<Integer, Plant>();

	public PlantFactory() {

	}

	public Plant getPlant(int type) {

		if (!plantMap.containsKey(type)) {

			switch (type) {
			case 0:
				plantMap.put(0, new Tree());
				break;
			case 1:
				plantMap.put(1, new Grass());
				break;
			}
		}

		return plantMap.get(type);
	}
}


PlantManager

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 mPlantFactory;
	public PlantManager() {
		
		mPlantFactory=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 displayTrees() {
		for (int i = 0; i < length; i++) {
			mPlantFactory.getPlant(typeArray[i]).display(xArray[i], yArray[i], AgeArray[i]);
			}
	}
}


MainTest

import java.util.ArrayList;

public class MainTest {

	public static void main(String[] args) {

		showMemInfo();

		PlantManager mPlantManager;
		mPlantManager = new PlantManager();

		showMemInfo();
		mPlantManager.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("");
	}

}



蝇量模式关键点
 

蝇量模式原理
蝇量模式:通过共享的方式高效地支持大量细粒度的对象。

 

优点:

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

将许多“虚拟”对象的状态集中管理

缺点:

系统设计更加复杂

需要专门维护对象的外部状态


蝇量模式适用场合

适用场合:

需要大量细粒度对象

这些对象的外部状态不多

按照内部状态分成几个组,每一个组都仅用一个蝇量对象代替

猜你喜欢

转载自blog.csdn.net/xiao__jia__jia/article/details/86776640