重点概念
1. 这个模式相对简单。享元模式,就是在工厂中包含所要共享的元素实例,多个对象通过发给这个工厂所需要对象的标记,获得同一个实例,这样多个对象的操作只需要一个对象实例即可,看基础代码示例。
2. 内部状态(共享对象)和外部状态(个性化部分)(代码示例见后面网站第三版)
核心意思,是需要改变的内容部分,可以作为参数传递给方法,而对象本身依然是一个。
3. 如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的内存开销时可以考虑使用享元模式
4. 对象的大多数状态可以外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享元模式。
5. 享元模式节约内存开销,减少对象实例化个数。
示例程序
using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace 享元模式 { class Program { static void Main(string[] args) { int extrinsicstate = 22; FlyweightFactory f = new FlyweightFactory(); Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = f.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fz = f.GetFlyweight("Z"); fz.Operation(--extrinsicstate); UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight(); uf.Operation(--extrinsicstate); Console.Read(); } } class FlyweightFactory { private Hashtable flyweights = new Hashtable(); public FlyweightFactory() { flyweights.Add("X", new ConcreteFlyweight()); flyweights.Add("Y", new ConcreteFlyweight()); flyweights.Add("Z", new ConcreteFlyweight()); } public Flyweight GetFlyweight(string key) { return ((Flyweight)flyweights[key]); } } abstract class Flyweight { public abstract void Operation(int extrinsicstate); } class ConcreteFlyweight : Flyweight { public override void Operation(int extrinsicstate) { Console.WriteLine("具体Flyweight:" + extrinsicstate); } } class UnsharedConcreteFlyweight : Flyweight { public override void Operation(int extrinsicstate) { Console.WriteLine("不共享的具体Flyweight:" + extrinsicstate); } } }
网站,第三版
using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace 享元模式 { class Program { static void Main(string[] args) { WebSiteFactory f = new WebSiteFactory(); WebSite fx = f.GetWebSiteCategory("产品展示"); fx.Use(new User("小菜")); WebSite fy = f.GetWebSiteCategory("产品展示"); fy.Use(new User("大鸟")); WebSite fz = f.GetWebSiteCategory("产品展示"); fz.Use(new User("娇娇")); WebSite fl = f.GetWebSiteCategory("博客"); fl.Use(new User("老顽童")); WebSite fm = f.GetWebSiteCategory("博客"); fm.Use(new User("桃谷六仙")); WebSite fn = f.GetWebSiteCategory("博客"); fn.Use(new User("南海鳄神")); Console.WriteLine("得到网站分类总数为 {0}", f.GetWebSiteCount()); //string titleA = "大话设计模式"; //string titleB = "大话设计模式"; //Console.WriteLine(Object.ReferenceEquals(titleA, titleB)); Console.Read(); } } //用户 public class User { private string name; public User(string name) { this.name = name; } public string Name { get { return name; } } } //网站工厂 class WebSiteFactory { private Hashtable flyweights = new Hashtable(); //获得网站分类 public WebSite GetWebSiteCategory(string key) { if (!flyweights.ContainsKey(key)) flyweights.Add(key, new ConcreteWebSite(key)); return ((WebSite)flyweights[key]); } //获得网站分类总数 public int GetWebSiteCount() { return flyweights.Count; } } //网站 abstract class WebSite { public abstract void Use(User user); } //具体的网站 class ConcreteWebSite : WebSite { private string name = ""; public ConcreteWebSite(string name) { this.name = name; } public override void Use(User user) { Console.WriteLine("网站分类:" + name + " 用户:" + user.Name); } } }
冯瑞涛