享元模式

定义

当系统中需要多次创建某些对象,而这些对象又无需改变,或者是改变时均一致时。可以只创建对应类的对象,并将其存于内存中,实现共享,需要用到时直接内存获取,无需多次创建。

类图

代码

 1   public abstract class BaseColor
 2     {
 3         private string name;
 4         public BaseColor(string colorName)
 5         {
 6             name = colorName;
 7         }
 8         public virtual int GetValue()
 9         {
10             return 0x000FFF;
11         }
12     }
13     public class RedColor : BaseColor
14     {
15         public RedColor() : base("RedColor")
16         {
17 
18         }
19         public override int GetValue()
20         {
21             return 0xFFC0CB;
22         }
23     }
24     public class BlueColor : BaseColor
25     {
26         public BlueColor() : base("BlueColor")
27         {
28 
29         }
30         public override int GetValue()
31         {
32             return 0x0000FF;
33         }
34     }
35     public class ColorFlyweightFactory
36     {
37         private Dictionary<string, BaseColor> colors;
38         private static object mLock = new object();
39         private static ColorFlyweightFactory instance = null;
40         private ColorFlyweightFactory()
41         {
42             colors = new Dictionary<string, BaseColor>();
43         }
44         public static ColorFlyweightFactory GetInstance()
45         {
46             if (instance == null)
47             {
48                 lock (mLock)
49                 {
50                     if (instance == null)
51                     {
52                         instance = new ColorFlyweightFactory();
53                     }
54                 }
55             }
56             return instance;
57         }
58         /// <summary>
59         /// 创建颜色
60         /// </summary>
61         /// <param name="colorName">约定使用颜色类名作为参数</param>
62         /// <returns></returns>
63         public BaseColor CreateColor(string colorName)
64         {
65             if (colors.ContainsKey(colorName))
66             {
67                 return colors[colorName];
68             }
69             else
70             {
71                 try
72                 {
73                     //约定使用颜色类名作为参数,当然可以分离出去,此处略
74                     BaseColor color = (BaseColor)Activator.CreateInstance(Type.GetType(colorName));
75                     colors.Add(colorName, color);
76                     return color;
77                 }
78                 catch (Exception ex)
79                 {
80                     throw new Exception(string.Format("不存在{0}", colorName),ex);
81                 }
82             }
83         }
84     }
85     public class ColorClient
86     {
87         BaseColor red= ColorFlyweightFactory.GetInstance().CreateColor("RedColor");
88         BaseColor blue = ColorFlyweightFactory.GetInstance().CreateColor("BlueColor");
89         BaseColor red2 = ColorFlyweightFactory.GetInstance().CreateColor("RedColor");//当再次需要到红色时,无需再创建,直接从工厂的字典中获取
90         BaseColor blue2 = ColorFlyweightFactory.GetInstance().CreateColor("BlueColor");//再次需要蓝色也无需再次创建。
91     }

总结

享元模式在于确定了类对象具有同步可共用的特征,通过缓存管理,减少对象创建和销毁的性能消耗,代码和对象均可实现良好的管理。

posted @ 2015-10-22 00:32  Tim_et  阅读(205)  评论(0编辑  收藏  举报