极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)「建议收藏」

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

大家好,又见面了,我是全栈君。

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

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

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

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

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

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

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

 类图:

 

极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)「建议收藏」

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

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

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

        一、抽象轻量级类(Flyweight)

复制代码
    //抽象轻量级类(Flyweight)
abstract class Clothes
{
public string Name { get; set; }
public int Defense { get; set; }
public abstract void GetDefense();
}
复制代码

        二、具体轻量级类(ConcreteFlyweight)

复制代码
    //具体轻量级类(ConcreteFlyweight)
class LowClothes : Clothes
{
public LowClothes()
{
Name = "青铜圣衣";
Defense = 50;
}
public override void GetDefense()
{
Console.WriteLine(Name + "的防御是" + Defense);
}
}

//具体轻量级类(ConcreteFlyweight)
class MiddleClothes : Clothes
{
public MiddleClothes()
{
Name = "白银圣衣";
Defense = 80;
}
public override void GetDefense()
{
Console.WriteLine(Name + "的防御是" + Defense);
}
}

//具体轻量级类(ConcreteFlyweight)
class HighClothes : Clothes
{
public HighClothes()
{
Name = "黄金圣衣";
Defense = 100;
}
public override void GetDefense()
{
Console.WriteLine(Name + "的防御是" + Defense);
}
}
复制代码

        三、轻量级类工厂(FlyweightFactory)

复制代码
    //轻量级类工厂(FlyweightFactory)
class ClothesFactory
{
private Hashtable clothesHT = new Hashtable();
public Clothes GetClothes(string clothesName)
{
Clothes clothes = (Clothes)clothesHT[clothesName];
if (clothes == null)
{
switch (clothesName)
{
case "青铜圣衣": clothes = new LowClothes();
break;
case "白银圣衣": clothes = new MiddleClothes();
break;
case "黄金圣衣": clothes = new HighClothes();
break;
}
clothesHT.Add(clothesName, clothes);
}
return clothes;
}
}
复制代码

        四、客户端(Client) 

复制代码
    //客户端(Client)
class Program
{
static void Main(string[] args)
{
ClothesFactory fac = new ClothesFactory();
fac.GetClothes("黄金圣衣").GetDefense();
fac.GetClothes("白银圣衣").GetDefense();
fac.GetClothes("黄金圣衣").GetDefense();
fac.GetClothes("黄金圣衣").GetDefense();
fac.GetClothes("青铜圣衣").GetDefense();
fac.GetClothes("白银圣衣").GetDefense();
Console.ReadLine();
}
}
复制代码

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

本文转自程兴亮博客园博客,原文链接:http://www.cnblogs.com/chengxingliang/archive/2011/10/12/2200444.html,如需转载请自行联系原作者


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/108011.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号