Not the same Flyweight (Design Mode 4)

Foreword

Flyweight, surface mean sharing unit, belonging to the structure of design patterns. Oh? good ah, now shared cultural tall, share it certainly save a lot of resources, is certainly the place with a lot of it, but not much, but it is an integral part of a pattern.
As for why, look at the body part, will be analyzed by calculating the place Why is not much use, or in some places Why should not be used at the same time come to be why the structure type, in the end belongs to which type of structure.

Drive Trigger

Tell us about what is structural:
structural model that describes how the classes and objects together to form a larger structure, which describes the different things in two ways: classes and objects, according to this, can be divided into categories structural schema and object structures. Class structural model of interest based combination, a plurality of classes may be combined into a larger system, there is generally only inheritance class structure type and model relations implemented; structural model combined object classes and objects of interest, by associating relation defined such that an object instance of another class in a class, then call a method by which the object. The "Synthesis of multiplexing principles", to make use of the association relationship in the system instead of inheritance, and therefore most of the structural model is an object model type structures.
Well, first there is a small idea of the structure type, and then start it.
There is a Font, font type.

public class Font
{
    // unique
    private string key;
    //表示字体大小
    private int size;
    //表示字体颜色
    private string color;
    public Font(string key, int size,string color)
    {
        this.key = key;
        this.size = size;
        this.color = color;
    }
}

One pair font rendering static class.

public static class render
{
    public static void renderFont(char a,Font font) {
        //进行字体渲染
    }
}

main call

static void Main(string[] args)
{
    Font fontx = new Font("xxx",12,"red");
    render.renderFont('a',fontx);
    Font fonty = new Font("yyy", 13, "red");
    render.renderFont('b',fonty);
    Font fontx2 = new Font("xxx", 12, "red");
    render.renderFont('c',fontx2);
}

If you look carefully in the main, I will find two of the same rendering:

Font fontx = new Font("xxx",12,"red");

This is my description of the rendering process, such as when we render the article is an output, do not know what to output the next, what is the font type Yes.
The process described above is the current font type fontx, I wrote a, then use the font fonty, to describe b, and finally I be useful font fontx2 (and fontx same configuration) to render c.
Under such circumstances there will be a problem? Suppose there are 100,000 words need to be rendered.
First check under Font instances of how many byte occupied.
In the Font head plus StructLayout, so that it is aligned to structLayout, otherwise unable to obtain its size.

[StructLayout(LayoutKind.Sequential)]
public class Font

Then call:

Font fontx = new Font("xxx",12,"red");
unsafe {
    var byteSize = Marshal.SizeOf(fontx);
    Console.WriteLine("查询大小");
}

The query result is 24 bytes.
Counted:
24byte 1 * 10 ^ 6 = 2.4 10 ^ 7/1024 /1024=2.4 10 ^ 7 /(1.048576 10 ^ 6) is approximately equal to 24m.
That is, I suppose I need to render 10w word memory 24m, you can think and know, it is to be optimized.
That plus a factory class it:

public class FontFactory
{
    private Hashtable flyweights = new Hashtable();

    public Font getFont(string key,int size,string color) {
        if (!flyweights.ContainsKey(key))
        {
            flyweights.Add(key,new Font(key,size,color));
        }
        return (Font)flyweights[key];
    }
}

transfer:

static void Main(string[] args)
{
    FontFactory fontFactory = new FontFactory();
    Font fontx = fontFactory.getFont("xxx",12,"red");
    Font fonty = fontFactory.getFont("yyy", 13, "red");
}

A font generated by the plant, if any, are extracted, if not to generate, and then return.
However, to the previously mentioned, this mode uses less. Why do you say?
First of all, we need to consume mechanism FontFactory of the cpu and part of the memory can not be ignored.
If the object of consumption produced only a few k, then this optimization is almost meaningless.
The second of it, you can see produced to meet the Font read-only, read-only why it?
If you need to change, then not run into this problem, why not amend, modify consumption is relatively large.
So the scene is not a lot, but the server does have so many places to use. Note: The use of local scenes and not a concept Kazakhstan.
Finally, look at the concept of object structure type.
Object class structural model of interest in combination with the object, so that the relationship defined by another instance of the object class in a class, then call a method by which the object. The "Synthesis of multiplexing principles", to make use of the association relationship in the system instead of inheritance, and therefore most of the structural model is an object model type structures.
It goes without saying, precisely in line with the concept of this model.

uml 图

Follow-up on

to sum up

Flyweight mainly used to reduce the number of objects created to reduce memory usage and improve performance.
Scene: a particularly large number of objects will produce, consume too much memory is available.

Guess you like

Origin www.cnblogs.com/aoximin/p/12088894.html