zoukankan      html  css  js  c++  java
  • Flyweight 享元模式

     public class Font    //12+8 bytes  8用于垃圾手机
        {
            string fontName;  //4 bytes
            int size;         //4 bytes 
            Color color;      //4 bytes
    
            public Font( string fontName, int size, Color color )
            {
                this.fontName = fontName;
                this.size = size;
                this.color = color;
            }
    
            public override bool Equals( object obj )
            {
                return base.Equals( obj );
            }
        }
        public class Color
        {
        }
     public class Charactor   //(2+4+20+2)+8 byes =36 bytes  8 bytes用于垃圾收集
        {
            char c;    //2 bytes
            //Font f;    //20 bytes
        //重点就在于下面的实现
    private static Hashtable fontTable = new Hashtable(); public void SetFont( String name, int size, Color color ) { if( fontTable.ContainsKey( name ) ) { return; } else { Font f = new Font( name, size, color ); fontTable.Add( name,f ); } } }

    其实主要就是为了降低内存,对一些固定不变的东西做一个共享,经常变换的不做考虑,在.net里面字符串就是做了享元处理,codebehid也是享元模式的一个处理

  • 相关阅读:
    Quick Union
    Quick Find (QF)
    ubuntu kylin18 安装NVIDIA驱动
    vim 快捷键(update)
    Qt中的ui指针和this指针
    两种状态机扫描按键,第二种只要三行!!!
    RPi:QT+wiringPi demo程序
    esp-12e折腾
    vfd电子时钟制作
    vfd with stm8
  • 原文地址:https://www.cnblogs.com/wangchuang/p/3006754.html
Copyright © 2011-2022 走看看