享元模式(Flyweight)

1.    定义

       运用共享技术有效地支持大量细粒度的对象。

2.      UML 类图

 

3.      结构代码

// Flyweight pattern -- Structural example

using System;

using System.Collections;

 

namespace DoFactory.GangOfFour.Flyweight.Structural

{

  /// <summary>

  /// MainApp startup class for Structural

  /// Flyweight Design Pattern.

  /// </summary>

  class MainApp

  {

    /// <summary>

    /// Entry point into console application.

    /// </summary>

    static void Main()

    {

      // Arbitrary extrinsic state

      int extrinsicstate = 22;

 

      FlyweightFactory factory = new FlyweightFactory();

 

      // Work with different flyweight instances

      Flyweight fx = factory.GetFlyweight("X");

      fx.Operation(--extrinsicstate);

 

      Flyweight fy = factory.GetFlyweight("Y");

      fy.Operation(--extrinsicstate);

 

      Flyweight fz = factory.GetFlyweight("Z");

      fz.Operation(--extrinsicstate);

 

      UnsharedConcreteFlyweight fu = new

        UnsharedConcreteFlyweight();

 

      fu.Operation(--extrinsicstate);

 

      // Wait for user

      Console.ReadKey();

    }

  }

 

  /// <summary>

  /// The 'FlyweightFactory' class

  /// </summary>

  class FlyweightFactory

  {

    private Hashtable flyweights = new Hashtable();

 

    // Constructor

    public FlyweightFactory()

    {

      flyweights.Add("X", new ConcreteFlyweight());

      flyweights.Add("Y", new ConcreteFlyweight());

      flyweights.Add("Z", new ConcreteFlyweight());

    }

 

    public Flyweight GetFlyweight(string key)

    {

      return ((Flyweight)flyweights[key]);

    }

  }

 

  /// <summary>

  /// The 'Flyweight' abstract class

  /// </summary>

  abstract class Flyweight

  {

    public abstract void Operation(int extrinsicstate);

  }

 

  /// <summary>

  /// The 'ConcreteFlyweight' class

  /// </summary>

  class ConcreteFlyweight : Flyweight

  {

    public override void Operation(int extrinsicstate)

    {

      Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);

    }

  }

 

  /// <summary>

  /// The 'UnsharedConcreteFlyweight' class

  /// </summary>

  class UnsharedConcreteFlyweight : Flyweight

  {

    public override void Operation(int extrinsicstate)

    {

      Console.WriteLine("UnsharedConcreteFlyweight: " +

        extrinsicstate);

    }

  }

}


Output
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18

4.      实例代码

// Flyweight pattern -- Real World example

using System;

using System.Collections.Generic;

 

namespace DoFactory.GangOfFour.Flyweight.RealWorld

{

  /// <summary>

  /// MainApp startup class for Real-World

  /// Flyweight Design Pattern.

  /// </summary>

  class MainApp

  {

    /// <summary>

    /// Entry point into console application.

    /// </summary>

    static void Main()

    {

      // 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);

      }

 

      // Wait for user

      Console.ReadKey();

    }

  }

 

  /// <summary>

  /// The 'FlyweightFactory' class

  /// </summary>

  class CharacterFactory

  {

    private Dictionary<char, Character> _characters =

      new Dictionary<char, Character>();

 

    public Character GetCharacter(char key)

    {

      // Uses "lazy initialization"

      Character character = null;

      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;

    }

  }

 

  /// <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

  {

    // Constructor

    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

  {

    // Constructor

    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 + ")");

    }

  }

}


Output
A (pointsize 11)
A (pointsize 12)
Z (pointsize 13)
Z (pointsize 14)
B (pointsize 15)
B (pointsize 16)
Z (pointsize 17)
B (pointsize 18)

该文章来自:http://www.dofactory.com/Patterns/PatternFlyweight.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值