Fly Volume mode (Flyweight pattern)

Source: Internet
Author: User

Fly Volume Mode: Allows an instance of a class to be used to provide many "virtual instances".

In a large number of objects, it is possible to cause memory overflow, the common part of the abstraction out, if there is the same business request, directly return in memory existing objects, avoid repeated creation. (String in Java, if not, create a string to hold in the string constant pool, or return directly)

Class Diagram:

 Public InterfacePlanet { Public Abstract voidDisplayintXinty);} Public classTree implements Planet {Private intx; Private inty; PrivateString height;  Public intGetX () {returnx; }     Public voidSetX (intx) { This. x =x; }     Public intGetY () {returny; }     Public voidSety (inty) { This. y =y; }     PublicTree (String height) { This. Height =height; } @Override Public voidDisplayintXinty) {System. out. println (height +"Tree in x="+ x +"y="+y); }} Public classPlanetfactory {Private StaticFinal hashmap<string, planet> planetmap =NewHashmap<string, planet>();  Public StaticPlanet gettree (String height) {Tree Tree= (Tree) planetmap.Get(height); if(Tree = =NULL) {Tree=NewTree (height);            Planetmap.put (height, tree); System. out. println ("Creating Tree of Height:"+height);//Create one only if you can't find an object in the map, print it out}returnTree; }} Public classClient {Private StaticFinal String height[] = {"Tall","Medium"," Short"}; Private StaticString getrandomheight () {returnheight[(int) (Math.random () *Height.length)]; }    Private Static intGetrandomx () {return(int) (Math.random () * -); }    Private Static intgetrandomy () {return(int) (Math.random () * -); }     Public Static voidMain (string[] args) { for(inti =0; I < -; i++) {Tree tree=(Tree) Planetfactory.gettree (Getrandomheight ());        Tree.display (Getrandomx (), getrandomy ()); }    }}

A test result:

Creating Tree of Height:tall
Talltree in x=57 y=88
Talltree in x=10 y=24
Creating Tree of Height:short
Shorttree in X=8 y=61
Shorttree in x=92 y=27
Shorttree in x=58 y=73
Talltree in x=22 y=25
Shorttree in x=95 y=3
Creating Tree of Height:medium
Mediumtree in X=0 Y=1
Mediumtree in x=95 y=38
Mediumtree in X=35 y=56
Talltree in x=67 y=5
Mediumtree in X=35 y=47
Shorttree in x=37 y=32
Shorttree in x=40 y=94
Mediumtree in x=43 y=11
Mediumtree in X=4 y=94
Shorttree in x=68 y=70
Shorttree in x=62 y=56
Shorttree in X=13 y=69
Mediumtree in x=21 y=49

Fly Volume mode (Flyweight 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.