且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)

更新时间:2022-10-04 20:05:19

五个角色:抽象轻量级类(Flyweight)、具体轻量级类(ConcreteFlyweight)、不共享具体轻量级类(UnsharedConcreteFlyweight)、轻量级类工厂(FlyweightFactory)、客户端(Client) 

        抽象轻量级类(Flyweight):声明一个接口并且有一些属性可以设置对象的状态

        具体轻量级类(ConcreteFlyweight):实现接口,并且有相关的状态

        不共享具体轻量级类(UnsharedConcreteFlyweight):不被共享的具体轻量级类

        轻量级类工厂(FlyweightFactory):创建并且管理Flyweight对象,当客户端发出轻量级类请求时提供一个已创建或者未创建的对象

        客户端(Client) :只需要使用轻量级类工厂调用相应的轻量级类即可。

 实现思路:客户端调用轻量级类工厂,工厂去查找是否已经有轻量级类实例,如果有则直接返回给客户端,如果没有则根据条件创建相应的实例给客户端。

 类图:

极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)

应用场景:游戏中的衣服实对象有很多种,会背穿在很多人的身上。

分析:游戏中的衣服它穿在很多人的身上,如果为每个人身上的衣服设置一个实例,那么对服务器的压力将不言而喻,这时如果在衣服内部设置很多种状态属性,在客户端调用的时候,使用轻量级类工厂来创建选择即可。

        下面我们在控制台程序去演示一下如何使用Flyweight Pattern:

        一、抽象轻量级类(Flyweight)

 


  1. //抽象轻量级类(Flyweight) 
  2. abstract class Clothes 
  3.     public string Name { get; set; } 
  4.     public int Defense { get; set; } 
  5.     public abstract void GetDefense(); 

        二、具体轻量级类(ConcreteFlyweight)

 


  1. //具体轻量级类(ConcreteFlyweight) 
  2. class LowClothes : Clothes 
  3.     public LowClothes() 
  4.     { 
  5.         Name = "青铜圣衣"
  6.         Defense = 50; 
  7.     } 
  8.     public override void GetDefense() 
  9.     { 
  10.         Console.WriteLine(Name + "的防御是" + Defense); 
  11.     } 
  12.  
  13. //具体轻量级类(ConcreteFlyweight) 
  14. class MiddleClothes : Clothes 
  15.     public MiddleClothes() 
  16.     { 
  17.         Name = "白银圣衣"
  18.         Defense = 80; 
  19.     } 
  20.     public override void GetDefense() 
  21.     { 
  22.         Console.WriteLine(Name + "的防御是" + Defense); 
  23.     } 
  24.  
  25. //具体轻量级类(ConcreteFlyweight) 
  26. class HighClothes : Clothes 
  27.     public HighClothes() 
  28.     { 
  29.         Name = "黄金圣衣"
  30.         Defense = 100; 
  31.     } 
  32.     public override void GetDefense() 
  33.     { 
  34.         Console.WriteLine(Name + "的防御是" + Defense); 
  35.     } 

        三、轻量级类工厂(FlyweightFactory)

 


  1. //轻量级类工厂(FlyweightFactory) 
  2. class ClothesFactory 
  3.     private Hashtable clothesHT = new Hashtable(); 
  4.     public Clothes GetClothes(string clothesName) 
  5.     { 
  6.         Clothes clothes = (Clothes)clothesHT[clothesName]; 
  7.         if (clothes == null
  8.         { 
  9.             switch (clothesName) 
  10.             { 
  11.                 case "青铜圣衣": clothes = new LowClothes(); 
  12.                     break; 
  13.                 case "白银圣衣": clothes = new MiddleClothes(); 
  14.                     break; 
  15.                 case "黄金圣衣": clothes = new HighClothes(); 
  16.                     break; 
  17.             } 
  18.             clothesHT.Add(clothesName, clothes); 
  19.         } 
  20.         return clothes; 
  21.     } 

        四、客户端(Client) 

 


  1. //客户端(Client) 
  2. class Program 
  3.     static void Main(string[] args) 
  4.     { 
  5.         ClothesFactory fac = new ClothesFactory(); 
  6.         fac.GetClothes("黄金圣衣").GetDefense(); 
  7.         fac.GetClothes("白银圣衣").GetDefense(); 
  8.         fac.GetClothes("黄金圣衣").GetDefense(); 
  9.         fac.GetClothes("黄金圣衣").GetDefense(); 
  10.         fac.GetClothes("青铜圣衣").GetDefense(); 
  11.         fac.GetClothes("白银圣衣").GetDefense(); 
  12.         Console.ReadLine(); 
  13.     } 

        如需源码请点击 FlyWeightPattern.zip 下载。



本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/827013