Design mode 11:flyweight Enjoy meta-mode (structured mode)

Source: Internet
Author: User

Flyweight-mode (structured mode)

Object-oriented costs

Object-oriented solves the problem of system abstraction, and in most cases it does not compromise the performance of the system. However, in some special applications, because the number of objects is too large, the adoption of object-oriented system will bring an unbearable memory overhead. Objects such as primitives in a specific application, character objects in a word processing application, and so on.

Motive (motivation)

The problem with a purely object approach is that a large number of fine-grained objects are quickly flooded into the system, with high operating costs-primarily the cost of memory requirements.

How can you avoid a large number of fine-grained object problems while still allowing external clients to transparently use the object-oriented approach?

Intentions (Intent)

The use of sharing technology to effectively support a large number of fine-grained objects-"design pattern" GoF

    classFont//(4+4+4) bytes+8bytes=20bytes (8bytes:4bytes garbage collection control bit, 4bytes virtual table pointer)    {         Public stringFontName;//4bytes Just for example, the actual should use the property of the         Public intSize//4bytes         PublicColor color;//4bytes
Public Font (String fontname,int size,color Color)
{
This.fontname = FontName;
this.size = size;
This.color = color;
}
} /*4:string FontName, the string is special, because the string pool has been used, regardless of its size in the heap, here only the pointer size is calculated. 4:int size 4:color Color 8:4bytes garbage collection Control bit +4bytes virtual table pointer total 20bytes*/ classCharactor//(2+4+20+2) bytes+8bytes=36bytes { Pubic CharChr//2bytes PubicFont font;//20bytes } /*2:char chr 4:font font, pointer accounted for 4bytes 20:font font heap size 2:32-bit system 4bytes fill effect, char CHR only accounted for 2bytes, added 2bytes 8:4bytes garbage Collection Control bit +4bytes virtual table pointer total 36bytes*/ classSystem { Public Static voidMain () {List<Charactor> list=NewList<charactor>(); for(inti =0; I <100000; i++) {charactor charactor=Newcharactor (); List. ADD (charactor); } //36bytes*100000=3600000bytes≈3600k≈3.6m//If the result of 10 million objects is about 360M, this is too big, consider the enjoy meta mode. } }

When the number of Charactor objects is very long, consider using the enjoy meta mode. The charactor needs to be reformed:

    classCharactor { Public CharCHR; Privatefont font; Private Staticdictionary<string,font>Dicfont;  PublicFont Cfunt {Get{returnfont;} Set            {                stringKey = Value.fontname + Value.size +Value.color; if(DicFont.Keys.Contains (key)) { This. Font =Dicfont[key]; }                Else{dicfont.add (key, value);  This. Font =value; }            }        }    }

This will not occupy extra space when duplicate fonts are present.

 charactor c1=new   Charactor (); Font f1  = new  Font ( "  Arial  , 10              = F1;            Charactor C2  =  Charactor (); Font F2  = new  Font ( "  Arial  , 10              = F2; 

F1 and F2 are different objects that point to different memory addresses, but when you assign a value to C1, and C2 CFont, the dictionary<string,font> of C2 does not allocate new memory space. This can save a lot of space when the object is very large.

. NET in the string pool is the use of the enjoy meta-mode.

Several points of flyweight model

    • Object-oriented is a good solution to the problem of abstraction, but as a program entity running in a machine, we need to consider the cost of the object. Flyweight design pattern mainly solves the object-oriented cost problem, and generally does not touch the object-oriented abstraction problem.
    • Flyweight uses object sharing to reduce the number of objects in the system, thereby reducing the memory pressure on the system from fine-grained objects. In the concrete implementation aspect, must pay attention to the object state processing.
    • The number of objects is too large to cause an increase in object memory overhead-What is the size? This requires careful assessment of the specific application, not the assumption.

Design mode 11:flyweight Enjoy meta-mode (structured mode)

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.