你的位置:首页 > ASP.net教程

[ASP.net教程]《大话设计模式》学习笔记22:享元模式


  

   

网站共享代码示例:

1.Flyweight:

  public abstract class WebSite  {    public abstract void Use();  }

2.ConcreteeFlyweight:

  public class ConcreteWebSite:WebSite  {    private string webSiteCategory;    public ConcreteWebSite(string webSiteCategory)    {      this.webSiteCategory = webSiteCategory;    }    public override void Use()    {      Console.WriteLine("网站分类:" + webSiteCategory);    }  }

3.FlyweightFactory:

  public class WebSiteFactory  {    private Hashtable webSites = new Hashtable();    public WebSite GetWebSiteCategory(string key)    {      if(!webSites.ContainsKey(key))      {        webSites.Add(key, new ConcreteWebSite(key));      }      return (WebSite)webSites[key];    }    public int GetWebSiteCount()    {      return webSites.Count;    }  }

4.客户端代码:

  class Program  {    static void Main(string[] args)    {      WebSiteFactory webSiteFactory = new WebSiteFactory();      WebSite webSiteA = webSiteFactory.GetWebSiteCategory("产品展示");      webSiteA.Use();      WebSite webSiteB = webSiteFactory.GetWebSiteCategory("产品展示");      webSiteB.Use();      WebSite webSiteC = webSiteFactory.GetWebSiteCategory("博客");      webSiteC.Use();      WebSite webSiteD = webSiteFactory.GetWebSiteCategory("博客");      webSiteD.Use();      Console.WriteLine("网站分类总数为:{0}", webSiteFactory.GetWebSiteCount());    }  }

 

内部状态与外部状态:

  在享元对象内部并且不会随环境改变而改变的共享部分,可以称为享元对象的内部状态;而随环境改变而改变的、不可以共享的状态为外部状态。

  享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,如果能把那些参数移到类实例的外面,在方法调用时将它们传递进来,就可以通过共享大幅度地减少单个实例的数目。

网站共享代码示例:

  

1.外部状态:

  public class User  {    public string Name { get; private set; }    public User(string name)    {      Name = name;    }  }

2.Flyweight:

  public abstract class WebSite  {    public abstract void Use(User user);  }

3.ConcreteeFlyweight:

  public class ConcreteWebSite : WebSite  {    private string webSiteCategory;    public ConcreteWebSite(string webSiteCategory)    {      this.webSiteCategory = webSiteCategory;    }    public override void Use(User user)    {      Console.WriteLine("网站分类:" + webSiteCategory + "\t用户:" + user.Name);    }  }

4.FlyweightFactory:

  public class WebSiteFactory  {    private Hashtable webSites = new Hashtable();    public WebSite GetWebSiteCategory(string key)    {      if(!webSites.ContainsKey(key))      {        webSites.Add(key, new ConcreteWebSite(key));      }      return (WebSite)webSites[key];    }    public int GetWebSiteCount()    {      return webSites.Count;    }  }

5.客户端代码:

  class Program  {    static void Main(string[] args)    {      WebSiteFactory webSiteFactory = new WebSiteFactory();      WebSite webSiteA = webSiteFactory.GetWebSiteCategory("产品展示");      webSiteA.Use(new User("用户A"));      WebSite webSiteB = webSiteFactory.GetWebSiteCategory("产品展示");      webSiteB.Use(new User("用户B"));      WebSite webSiteC = webSiteFactory.GetWebSiteCategory("博客");      webSiteC.Use(new User("用户C"));      WebSite webSiteD = webSiteFactory.GetWebSiteCategory("博客");      webSiteD.Use(new User("用户D"));      Console.WriteLine("网站分类总数为:{0}", webSiteFactory.GetWebSiteCount());    }  }