Design pattern of the fly volume pattern

Source: Internet
Author: User

Fly Volume pattern
Fly Volume mode: efficiently supports a large number of fine-grained objects in a shared manner.

Advantages:
Reduce the number of instances of objects at run time.
Centrally manage the state of many "virtual" objects.

Disadvantages:
System design is more complex.
The external state of the object needs to be specifically maintained.

Applicable occasions:
Requires a large number of fine-grained objects.
There are not many external states for these objects.
The internal states are divided into groups, and each group is replaced with only one fly volume object.

Class structure diagram

Example code:

Package Com.flyweight;public Abstract class Plant {public Plant () {}public abstract void display (int xcoord, int ycoord, I NT age);}

Package Com.flyweight;public class Tree extends Plant {@Overridepublic void display (int xcoord, int. ycoord, int age) {//T ODO auto-generated Method Stub//system.out.println ("Tree x");}}

Package Com.flyweight;public class Grass extends Plant {@Overridepublic void display (int xcoord, int. ycoord, int age) {// TODO auto-generated Method Stub//system.out.print ("Grass x");}}

Package Com.flyweight;import Java.util.hashmap;public class Plantfactory {private hashmap<integer,plant> Plantmap = new hashmap<integer,plant> ();p ublic 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);}}

Package Com.flyweight;public class Plantmanager {private int length = 10000000;private int[] Xarray = new Int[length],yarr ay = new Int[length],agearray = new Int[length],typearray = new Int[length];p rivate plantfactory mplantfactory;public Plan Tmanager () {mplantfactory = new plantfactory (); for (int i=0; i<length; i++) {xarray[i] = (int) (Math.random () * length); y Array[i] = (int) (Math.random () * length); Agearray[i] = (int) (Math.random () * length)%5;typearray[i] = (int) (Math.random () * length)%2;} public void DisplayTree () {for (int i=0; i<length; i++) {mplantfactory.getplant (Typearray[i]). Display (Xarray[i], Yarray[i], agearray[i]);}}

Package Com.flyweight;public class Maintest {/** * @param args */public static void main (string[] args) {//TODO Auto-gene Rated method Stubshowmeminfo (); Plantmanager Mplantmanager;mplantmanager = new Plantmanager (); Showmeminfo (); Mplantmanager.displaytree (); Showmeminfo ();} public static void Showmeminfo () {//MAX memory Long max = Runtime.getruntime (). MaxMemory ();//Allocate memory long total = Runtime.getruntime (). TotalMemory ();//allocated in memory the remaining space long free = Runtime.getruntime (). Freememory ();//memory occupied long used = Total-free; System.out.println ("max memory =" + max); SYSTEM.OUT.PRINTLN ("Allocated memory =" + total); System.out.println ("Remaining space in allocated memory =" + free); SYSTEM.OUT.PRINTLN ("Used memory =" + used); System.out.println ("time =" + System.currenttimemillis ()); System.out.println ("");}}

Output results

Max memory = 259522560 Allocated memory = 16252928 space remaining in allocated memory = 15536784 used memory = 716144 time = 1441355500069 max memory = 259522560 Allocated memory = 194347008 allocated memory remaining space = 32881632 used memory = 161465376 time = 1441355502603 max memory = 259522560 Allocated memory = 194347008 space remaining in allocated memory = 32881632 used memory = 161465376 time = 1441355503120

Design pattern of the fly volume pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.