Java flyweight Mode

Source: Internet
Author: User
Tags define abstract

Flyweight definition:
Avoid the overhead of a large number of small classes with the same content (such as memory consumption), so that everyone can share a class (Meta class ).

The principle of object-oriented language is that everything is an object.
Big. For example, if the word processing software uses every word as an object and thousands of words, the number of objects is several thousand, which is undoubtedly a waste.
For memory usage, we still need to "find out the similarities and differences between these object groups" and design a metadata class. encapsulation can be
Shared classes. In addition, some features depend on the application (context) and cannot be shared.
Important Concepts: intrinsic and extrinsic.

 

This is the definition of this mode. I think the most important thing is to differentiate the internal and external states of the object to be processed so that the internal states of the object can be shared, however, many examples write the flyweight mode into a pool and share the objects in the pool. This seems to deviate from the original intention of the flyweight mode and the effect is not satisfactory. The following is an example of a pattern matching the intention found on the Internet.

1. define abstract Interfaces

Public interface font {<br/> Public void setfont (string color, int size); </P> <p> Public void getfont (); <br/>}

 

2. Specific implementation of interfaces

Public class concretefont implements font {<br/> private string color; <br/> private int size; <br/> private fontinner inner; </P> <p> Public concretefont (fontinner s) {<br/> Inner = s; <br/> // id = "the char is:" + S; <br/>}</P> <p> Public void setfont (string _ color, int _ SIZE) {<br/> color = _ color; <br/> size = _ size; <br/>}</P> <p> Public void getfont () {<br/> system. out. println ("string:" + inner. getfontstring () + "--- color is:" <br/> + color + "--- size is:" + size); <br/>}< br/>}

 

3. Internal states that can be shared

 

Public class fontinner {</P> <p> private string fontstring; </P> <p> private string status; </P> <p> Public fontinner (string fontstring, string status) {<br/> This. setfontstring (fontstring); <br/> This. setstatus (Status); <br/>}</P> <p> Public String getstatus () {<br/> return status; <br/>}</P> <p> Public void setstatus (string status) {<br/> This. status = status; <br/>}</P> <p> Public String getfontstring () {<br/> return fontstring; <br/>}</P> <p> Public void setfontstring (string fontstring) {<br/> This. fontstring = fontstring; <br/>}</P> <p >}< br/>

 

4. fontinnerfactory is responsible for maintaining a flyweight pool (storing the internal status)

Public class fontinnerfactory {<br/> private hashtable <string, fontinner> charhashtable = new hashtable (); </P> <p> Public fontinnerfactory () {<br/>}</P> <p> Public fontinner getflyweight (string fontstring, string status) {<br/> If (charhashtable. get (fontstring )! = NULL) {<br/> return charhashtable. get (fontstring); <br/>} else {<br/> fontinner TMP = new fontinner (fontstring, status); <br/> charhashtable. put (fontstring, TMP); <br/> return TMP; <br/>}</P> <p> Public hashtable getfactory () {<br/> return charhashtable; <br/>}< br/>}

 

Test:

Import Java. util. arraylist; <br/> Import Java. util. hashtable; <br/> Import Java. util. list; </P> <p> public class main {<br/> Public static void main (string [] ARGs) {<br/> int [] size = {8, 9, 10, 11, 12 }; <br/> string [] color = {"ffffff", "000000", "ff00ff", "cccccc", "111111 "}; <br/> fontinnerfactory = new fontinnerfactory (); <br/> string astring = "A test string"; <br/> List <font> fontlist = new arraylist (); <br/> for (INT I = 0; I <astring. length (); I ++) {<br/> Int J = 0; <br/> J = (INT) math. floor (math. random () * 5); <br/> // system. out. println ("J is:" + J + "---" + astring. substring (I, <br/> // I + 1); <br/> concretefont font = new concretefont (fontinnerfactory. getflyweight (<br/> astring. substring (I, I + 1), ""); <br/> font. setfont (color [J], size [J]); <br/> fontlist. add (font); <br/>}</P> <p> hashtable myhashtable = fontinnerfactory. getfactory (); <br/> system. out. println ("hash table size is:" + myhashtable. size (); <br/> for (Font font: fontlist) {<br/> font. getfont (); <br/>}< br/>}

 

Here, fontinner in concretefont is an internal object, which can be shared and not created every time. color and size are external objects set according to the context. From the result, the printed string contains more than 10 characters, but the number of objects in map in fontinnerfactory is only 9, and internal objects are shared.

 

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.