复合享元涉及的角色如下:
1、抽象享元角色
2、具体享元
3、复合享元(复合享元是由单纯具体享元复合而成的)//如果是简单享元这条没有
4、享元工厂
5、客户端
简单享元演示代码如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace FlyweightFactory
{
class Program
{
//抽象享元
abstract class Cause //事业
{
abstract public void OutInput(string name);//输出所从事的行业
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//具体享元(互联网)
class IT : Cause
{
private string work = "从事互联网行业";
![](/Images/OutliningIndicators/InBlock.gif)
override public void OutInput(string name)
{
Console.WriteLine("{0} 是{1}",
name, work);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//具体享元(房地产)
class House : Cause
{
private string work = "从事房地产行业";
![](/Images/OutliningIndicators/InBlock.gif)
override public void OutInput(string name)
{
Console.WriteLine("{0} 是{1}",
name, work);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//(享元工厂)
class CauseFactory
{
![](/Images/OutliningIndicators/InBlock.gif)
private Hashtable flyweights = new Hashtable();
![](/Images/OutliningIndicators/InBlock.gif)
// 构造函数(享元工厂) 享元工厂作用:就是把多个对象的实例放在一起,好供客户端动态调用
public CauseFactory()
{
flyweights.Add("I", new IT());
flyweights.Add("H", new House());
}
![](/Images/OutliningIndicators/InBlock.gif)
//得到从事的事业(享元)
public Cause GetCause(string key)
{
return ((Cause)flyweights[key]);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
//客户端
static void Main(string[] args)
{
string MaYun = "马云";
string WangShi = "王石";
CauseFactory f = new CauseFactory();
//根据不同类别判别从事那个行业
Cause c1 = f.GetCause("I");//字母I对应对象IT行业
c1.OutInput(MaYun);//输出马云所从事的行业
![](/Images/OutliningIndicators/InBlock.gif)
Cause c2 = f.GetCause("H");//字母H对应对象House行业
c2.OutInput(WangShi);//输出王石所从事的行业
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
享元模式要点
1、本设计模式主要解决面向对象的代价问题。
2、本模式通过共享对象来减少系统中对象实例个数,从而减少内存消耗。
1、抽象享元角色
2、具体享元
3、复合享元(复合享元是由单纯具体享元复合而成的)//如果是简单享元这条没有
4、享元工厂
5、客户端
简单享元演示代码如下
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
享元模式要点
1、本设计模式主要解决面向对象的代价问题。
2、本模式通过共享对象来减少系统中对象实例个数,从而减少内存消耗。