Net设计模式实例之享元模式( Flyweight Pattern)(2)

四.享元模式实例分析(Example

1、场景

一个文档 Document 中只有少数字符需要共享。结构 如下图所示 <?XML:NAMESPACE PREFIX = O />

 

CharacterFactory ,享元工厂,用来创建和管理 Charactor 对象。如果请求的 Charactor 对象存在,怎返回已经存在的对象。否则新创建一个新的对象返回。

Character :享元抽象类,通过这个接口, Character 可以接受并作用与外部状态。
CharacterA /CharacterB/CharacterC :实现享元抽象类,为内部状态添加存储空间。

2、代码

1 字符工厂类CharacterFactory

class CharacterFactory

{

    private Dictionary <char , Character > _characters = new Dictionary <char , Character >();

    public Character GetCharacter(char key)

    {

        // Uses "lazy initialization"

        Character character = nu<?XML:NAMESPACE PREFIX = ST1 />ll ;

        if (_characters.ContainsKey(key))

        {

            character = _characters[key];

        }

        else

        {

            switch (key)

            {

                case 'A' : character = new CharacterA (); break ;

                case 'B' : character = new CharacterB (); break ;

                //...

                case 'Z' : character = new CharacterZ (); break ;

            }

            _characters.Add(key, character);

        }

        return character;

    }

}

 

2 抽象数据对象类DataObject 及其具体实现类CustomersData

/// <summary>

/// The 'Flyweight' abstract class

/// </summary>

abstract class Character

{

    protected char symbol;

    protected int width;

    protected int height;

    protected int ascent;

    protected int descent;

    protected int pointSize;

    public abstract void Display(int pointSize);

}

/// <summary>

/// A 'ConcreteFlyweight' class

/// </summary>

class CharacterA : Character

{

    public CharacterA()

    {

        this .symbol = 'A' ;

        this .height = 100;

        this .width = 120;

        this .ascent = 70;

        this .descent = 0;

    }

    public override void Display(int pointSize)

    {

        this .pointSize = pointSize;

        Console .WriteLine(this .symbol + " (pointsize " + this .pointSize + ")" );

    }

}

/// <summary>

/// A 'ConcreteFlyweight' class

/// </summary>

class CharacterB : Character

{

    public CharacterB()

    {

        this .symbol = 'B' ;

        this .height = 100;

        this .width = 140;

        this .ascent = 72;

        this .descent = 0;

    }

    public override void Display(int pointSize)

    {

        this .pointSize = pointSize;

        Console .WriteLine(this .symbol + " (pointsize " + this .pointSize + ")" );

    }

}

// ... C, D, E, etc.

/// <summary>

/// A 'ConcreteFlyweight' class

/// </summary>

class CharacterZ : Character

{

    // Constructor

    public CharacterZ()

    {

        this .symbol = 'Z' ;

        this .height = 100;

        this .width = 100;

        this .ascent = 68;

        this .descent = 0;

    }

    public override void Display(int pointSize)

    {

        this .pointSize = pointSize;

        Console .WriteLine(this .symbol +" (pointsize " + this .pointSize + ")" );

    }

}

 

3 、客户端代码

static void <?XML:NAMESPACE PREFIX = ST2 />Main (string [] args)

{

    // Build a document with text

    string document = "AAZZBBZB" ;

    char [] chars = document.ToCharArray ();

    CharacterFactory factory = new CharacterFactory ();

    // extrinsic state

    int pointSize = 10;

    // For each character use a flyweight object

    foreach (char c in chars)

    {

         pointSize++;

        Character character = factory.GetCharacter(c);

        character.Display(pointSize);

    }

    Console .ReadKey();

}

3、实例运行结果

五、总结(Summary

本文对享元模式( Flyweight Pattern )的概念、设计结构图、代码、使用场景、进行了描述。以一个享元模式实例进行了说明。如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销,这时可以考虑使用享元模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值