博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)
阅读量:7079 次
发布时间:2019-06-28

本文共 2310 字,大约阅读时间需要 7 分钟。

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

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

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

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

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

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

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

 类图:

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

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

        下面我们在控制台程序去演示一下如何使用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.     } 

        如需源码请点击  下载。

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

转载地址:http://vkjml.baihongyu.com/

你可能感兴趣的文章
unity, 设置帧率上限
查看>>
linux watchdog demo hacking
查看>>
两个函数连续性的讨论
查看>>
从头開始写项目Makefile(五):嵌套运行
查看>>
3种Nginx防盗链的方法
查看>>
UVA 10057 A mid-summer night's dream.
查看>>
JavaScript中的this
查看>>
vs2005,.net的C#语言开发生成com组件的tlb文件
查看>>
自动修改电脑IP地址.bat
查看>>
当网站上线时记得设置umbDebug为false--致Umbraco开发者
查看>>
JavaScript 操作 Cookie
查看>>
Asp.net MVC3扩展之Ajax异常处理特性
查看>>
Perl语言入门-第五章-输入与输出-习题
查看>>
【转载】wpf学习笔记数据绑定8
查看>>
制作iOS应用图标的最简单方法
查看>>
淘宝JavaScript 编码风格规范
查看>>
[原创]桓泽学音频编解码(8):关于MP3和AAC量化器设计的研究
查看>>
苹果开发者帐号(Company)申请流程
查看>>
字符串分拆并统计的处理示例.sql
查看>>
Nhibernate 3.0 cookbook学习笔记 一对多与多对多映射
查看>>