C#面向对象设计模式纵横谈 学习笔记12 FlyWeight享元模式(结构型模式)

在我们面向对象编程时,我们会经常将很多对象封装在一起,成为对象A,如果A中的某个对象所占用的内存比较大,同时我们需要创建很多对象A,我们需要一种设计模式来解决这个问题。打个比方,假设有一个网络游戏,同时有很多用户在线,那么我们就要创建很多网游用户对象,但是每个用户对象都包含了衣服对象,但是这个衣服对象的类别就3中,我们肯定不希望为每个用户来创建一个衣服对象,那么我们就将衣服对象放入一个共享池里,那么所有的用户都使用这个共享池里面的衣服,我们就通过这种方式来节省了衣服这快的内存。

  1.  class User
  2.     {
  3.         /// <summary>
  4.         /// 用户名
  5.         /// </summary>
  6.         string Name;
  7.         /// <summary>
  8.         /// 用户武器
  9.         /// </summary>
  10.         string Weapon;
  11.         /// <summary>
  12.         /// 用户性别
  13.         /// </summary>
  14.         string Gender;
  15.         /// <summary>
  16.         /// 用户衣服
  17.         /// </summary>
  18.         public UserClother Clother;
  19.     }
  20.     class UserClother
  21.     {
  22.         /// <summary>
  23.         /// 衣服名,确定衣服的唯一性
  24.         /// </summary>
  25.         string ClotherName;
  26.         /// <summary>
  27.         /// 衣服的其他属性
  28.         /// </summary>
  29.         string OtherProfile;
  30.         public UserClother(string ClotherName)
  31.         {
  32.             this.ClotherName = ClotherName;
  33.         }
  34.     }
  35.     static class UserClotherFactory
  36.     {
  37.         private static Hashtable clotherList = new Hashtable();
  38.         public static UserClother GetUserClose(string ClotherName)
  39.         {
  40.             //从共享池中得到衣服对象
  41.             UserClother clother = clotherList[ClotherName] as UserClother;
  42.             //如果共享池里面不存在这个对象那么就创建这个对象,并加入到共享池中
  43.             if (clother == null)
  44.             {
  45.                 clother = new UserClother(ClotherName);
  46.                 clotherList.Add(ClotherName, clother);
  47.             }
  48.             return clother;
  49.         }
  50.     }
  51.     class Program
  52.     {
  53.         static void Main(string[] args)
  54.         {
  55.             Console.WriteLine(GC.GetTotalMemory(false)); 
  56.             //假设有10万个用户在线
  57.             ArrayList userList = new ArrayList(100000);
  58.             
  59.             //假设衣服有三种
  60.             string clother1 = "Clother1";
  61.             string clother2 = "Clother2";
  62.             string clother3 = "Clother3";
  63.             User objUser = null;
  64.             for (int i = 0; i < 100000; i++)
  65.             {
  66.                 if (i % 3 == 0)
  67.                 {
  68.                     objUser = new User();
  69.                     objUser.Clother = UserClotherFactory.GetUserClose(clother1);
  70.                     //objUser.Clother = new UserClother(clother1);
  71.                 }
  72.                 else if (i % 3 == 1)
  73.                 {
  74.                     objUser = new User();
  75.                     objUser.Clother = UserClotherFactory.GetUserClose(clother2);
  76.                     //objUser.Clother = new UserClother(clother2);
  77.                 }
  78.                 else if (i % 3 == 2)
  79.                 {
  80.                     objUser = new User();
  81.                     objUser.Clother = UserClotherFactory.GetUserClose(clother3);
  82.                     //objUser.Clother = new UserClother(clother3);
  83.                 }
  84.                 userList.Add(objUser);
  85.             }
  86.             Console.WriteLine(GC.GetTotalMemory(false));
  87.             Console.ReadLine(); 
  88.         }
  89.     }
  90. Main函数里注销的代码是不使用享元模式的代码。我们可以看到,在有10万用户使用的时候,我们衣服的对象始终只有3个,而不是30万个,这样我们就大大的节省了内存。
  91. 现在我们可以知道了享元模式使用在对象中有大量耗费了大量内存的细粒度对象,并且对外界来说这些对没有任何差别的(或者说经过改造后可以是没有差别的)。如果衣服有很多种,各式各样,如有10万种组合,那么使用享元模式基本上没有什么意义(节省不了多少内存),因为维护共享池的内存开销与直接在用户对象里指定新的衣服对象使用的内存差别不大。
  92. 实现要点

      享元工厂维护一张享元实例表。

      享元不可共享的状态需要在外部维护。

      按照需求可以对享元角色进行抽象。

    注意事项

      享元模式通常针对细粒度的对象,如果这些对象比较拥有非常多的独立状态(不可共享的状态),或者对象并不是细粒度的,那么就不适合运用享元模式。维持大量的外蕴状态不但会使逻辑复杂而且并不能节约资源。

      享元工厂中维护了享元实例的列表,同样也需要占用资源,如果享元占用的资源比较小或者享元的实例不是非常多的话(和列表元素数量差不多),那么就不适合使用享元,关键还是在于权衡得失

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值