1. The scenario is resolved

1.1 Scenario Description

1.2 OO design

1.3 Demand Change

1.4 Bringing Problems

2. Improve with design patterns

2.1 analysis

A large number of small objects, object properties split – internal properties and external properties split;

For example, the tree object is generally designed as: there is a collection of tree objects, each object has its corresponding abscissa, ordinate, and age.

The fly population pattern is: Multiple collections (each of the same length), the first holds all the virtual tree objects, the second holds the abscissa of all the trees, the third holds all the ordinates of the trees, and the fourth holds the ages of all the trees. Combine tree objects by taking values from each collection as needed.

2.2 Redesign

2.3 the source code

  • Oo pattern,Tree(Tree object),TreesTest(Tree set),OOTest(test class)
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 OOTest { 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(); // Allocate memory: long total = Runtime.geTruntime ().totalMemory(); // Free free = Runtime.getruntime ().freememory (); // Long used = total-free; System.out.println(" Max memory = "+ Max); System.out.println(" Allocated memory = "+ total); System.out.println(" free space in allocated memory = "+ free); System.out.println(" used memory = "+ used memory); System.out.println(" time = "+ system.currentTimemillis ()); System.out.println(""); }}Copy the code
  • Fly Count Patterns,TreeFlyWeight(Fly Count Tree),TreeManager(Fly Count Tree Management),FlyWeightTest(Test Class)
public class TreeFlyWeight { public TreeFlyWeight() { } public void display(int xCoord, int yCoord, 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 FlyWeightTest { public static void main(String[] args) { showMemInfo(); TreeManager mTreeManager; mTreeManager = new TreeManager(); showMemInfo(); mTreeManager.displayTrees(); showMemInfo(); } public static void showMemInfo() {// Free free = Runtime.geTruntime ().freememory (); // Allocate memory: long total = Runtime.geTruntime ().totalMemory(); // maxMemory: long Max = Runtime.getruntime ().maxMemory(); // Long used = total-free; System.out.println(" Max memory = "+ Max); System.out.println(" Allocated memory = "+ total); System.out.println(" free space in allocated memory = "+ free); System.out.println(" used memory = "+ used memory); System.out.println(" time = "+ system.currentTimemillis ()); System.out.println(""); }}Copy the code
  • Support for multiple Plant modes,Plant,Tree,Grass,PlantFactory,PlantManager,FlyWeight2Test
public abstract class Plant { public Plant() { } public abstract void display(int xCoord, int yCoord, int age); } public class Tree extends Plant { @Override public void display(int xCoord, int yCoord, int age) { // System.out.print("Tree x"); } } public class Grass extends Plant { @Override public void display(int xCoord, int yCoord, int age) { // System.out.print("Grass x"); } } 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); } } 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]); } } } public class FlyWeight2Test { public static void main(String[] args) { showMemInfo(); PlantManager mPlantManager; mPlantManager = new PlantManager(); showMemInfo(); mPlantManager.displayTrees(); showMemInfo(); } public static void showMemInfo() {// Free free = Runtime.geTruntime ().freememory (); // Allocate memory: long total = Runtime.geTruntime ().totalMemory(); // maxMemory: long Max = Runtime.getruntime ().maxMemory(); // Long used = total-free; System.out.println(" Max memory = "+ Max); System.out.println(" Allocated memory = "+ total); System.out.println(" free space in allocated memory = "+ free); System.out.println(" used memory = "+ used memory); System.out.println(" time = "+ system.currentTimemillis ()); System.out.println(""); }}Copy the code

3. Summary of design patterns

3.1 define

** Fly count mode: ** efficiently supports a large number of fine-grained objects by sharing them.

3.2 Analysis

3.3 Advantages and disadvantages of fly quantity model

  • Advantages:
    • Reduce the number of object instances at run time, saving creation overhead and memory
    • Centrally manage the state of many “virtual” objects
  • Disadvantages:
    • The system design is more complex
    • The external state of the object needs to be maintained exclusively

4. Design pattern usage scenarios and attention

4.1 Application:

  • You need a lot of fine-grained objects
  • These objects don’t have much external state
  • Divided into several groups according to the internal state, each group is replaced by only one fly count object

5. Refer to the article

Total content in HeadFirst design mode and related videos