享元模式(Flyweight)
来源:互联网 发布:js 双向绑定 编辑:程序博客网 时间:2024/05/22 15:23
1. 定义
运用共享技术有效地支持大量细粒度的对象。2. UML 类图
3. 结构代码
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Flyweight.Structural
{
///<summary>
/// MainApp startup class for Structural
/// Flyweight Design Pattern.
///</summary>
classMainApp
{
///<summary>
/// Entry point into console application.
///</summary>
staticvoid 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>
classFlyweightFactory
{
privateHashtable flyweights = new Hashtable();
// Constructor
public FlyweightFactory()
{
flyweights.Add("X",new ConcreteFlyweight());
flyweights.Add("Y",new ConcreteFlyweight());
flyweights.Add("Z",new ConcreteFlyweight());
}
publicFlyweight GetFlyweight(string key)
{
return ((Flyweight)flyweights[key]);
}
}
///<summary>
/// The 'Flyweight' abstract class
///</summary>
abstractclass Flyweight
{
publicabstract void Operation(int extrinsicstate);
}
///<summary>
/// The 'ConcreteFlyweight' class
///</summary>
classConcreteFlyweight : Flyweight
{
publicoverride void Operation(int extrinsicstate)
{
Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
}
}
///<summary>
/// The 'UnsharedConcreteFlyweight' class
///</summary>
classUnsharedConcreteFlyweight : Flyweight
{
publicoverride void Operation(int extrinsicstate)
{
Console.WriteLine("UnsharedConcreteFlyweight: " +
extrinsicstate);
}
}
}
Output
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18
4. 实例代码
using System;
using System.Collections.Generic;
namespace DoFactory.GangOfFour.Flyweight.RealWorld
{
///<summary>
/// MainApp startup class for Real-World
/// Flyweight Design Pattern.
///</summary>
classMainApp
{
///<summary>
/// Entry point into console application.
///</summary>
staticvoid 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 cin chars)
{
pointSize++;
Character character = factory.GetCharacter(c);
character.Display(pointSize);
}
// Wait for user
Console.ReadKey();
}
}
///<summary>
/// The 'FlyweightFactory' class
///</summary>
classCharacterFactory
{
privateDictionary<char,Character> _characters =
newDictionary<char,Character>();
publicCharacter GetCharacter(char key)
{
// Uses "lazy initialization"
Character character =null;
if (_characters.ContainsKey(key))
{
character = _characters[key];
}
else
{
switch (key)
{
case'A': character = newCharacterA(); break;
case'B': character = newCharacterB(); break;
//...
case'Z': character = newCharacterZ(); break;
}
_characters.Add(key, character);
}
return character;
}
}
///<summary>
/// The 'Flyweight' abstract class
///</summary>
abstractclass Character
{
protectedchar symbol;
protectedint width;
protectedint height;
protectedint ascent;
protectedint descent;
protectedint pointSize;
publicabstract void Display(int pointSize);
}
///<summary>
/// A 'ConcreteFlyweight' class
///</summary>
classCharacterA : Character
{
// Constructor
public CharacterA()
{
this.symbol ='A';
this.height = 100;
this.width = 120;
this.ascent = 70;
this.descent = 0;
}
publicoverride void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol +
" (pointsize " +this.pointSize + ")");
}
}
///<summary>
/// A 'ConcreteFlyweight' class
///</summary>
classCharacterB : Character
{
// Constructor
public CharacterB()
{
this.symbol ='B';
this.height = 100;
this.width = 140;
this.ascent = 72;
this.descent = 0;
}
publicoverride void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol +
" (pointsize " +this.pointSize + ")");
}
}
// ... C, D, E, etc.
///<summary>
/// A 'ConcreteFlyweight' class
///</summary>
classCharacterZ : Character
{
// Constructor
public CharacterZ()
{
this.symbol ='Z';
this.height = 100;
this.width = 100;
this.ascent = 68;
this.descent = 0;
}
publicoverride void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol +
" (pointsize " +this.pointSize + ")");
}
}
}
Output
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
- 享元模式(Flyweight)
- 享元(Flyweight)模式
- 享元模式(Flyweight)
- Flyweight(享元模式)
- 享元模式(Flyweight)
- Flyweight(享元)模式
- 享元(Flyweight)模式
- 享元模式(FlyWeight)
- 享元(flyweight)模式
- 享元模式(Flyweight)
- 享元模式(Flyweight)
- Flyweight(享元模式)
- 享元模式(Flyweight)
- Flyweight(享元模式)
- 享元模式(Flyweight)
- 享元模式(Flyweight)
- 享元模式(Flyweight)
- 享元模式(Flyweight)
- WTL使用CheckBox
- 数据结构与算法分析-第一章Java类(03)
- 黑马程序员____求多项式系数
- 玩转BeagleBoard xM——解决TF卡文件系统"readonly"错误
- Swing自定义水晶按钮
- 享元模式(Flyweight)
- 809 Res Bitmap Drawable Canvas 详解
- oracle_oradim
- 云计算
- C语言中static用法介绍
- 解决WIN7编译出的程序无法连接其他数据库的问题。
- 字符串各种纠结
- hdu 4355 Party All the Time
- ds1302显示时间(通过串口调试助手)---C语言版本