C#设计模式系列:享元模式(Flyweight)
当频繁地从数据源读取数据时,读出的内容存在重复,那么需要使用享元模式(Flyweight)来提高内存效率,Flyweight模式将节省更多空间,共享的Flyweight越多,空间节省越大。
1、享元模式简介
1.1>、定义
享元模式(Flyweight)的存在是为了避免大量拥有相同内容的小类的开销(如内存开销),使大家共享一个类。
1.2>、使用频率
低
2、享元模式结构
2.1>、结构图

2.2>、参与者
享元模式参与者:
◊ Flyweight:声明一个接口,通过这个接口flyweight可以直接接收并作用于外部状态。
◊ ConcreteFlyweight:实现Flyweight接口,并为内部状态增加存储空间。ConcreteFlyweight对象必须是可以共享的,它所存储的状态必须是内部的,即它必须独立于ConcreteFlyweight对象的场景。
◊ UnsharedConcreteFlyweight:并非所有的Flyweight子类都需要被共享。Flyweight接口使共享成为可能,但它不强制共享。在Flyweight对象结构的某些层次,UnsharedConcreteFlyweight对象通常将ConcreteFlyweight对象作为子节点。
◊ FlyweightFactory
° 创建和管理flyweight对象。
° 确保flyweight对象被合理共享。当Client请求一个flyweight对象时,FlyweightFactory需要可以进行分配,若flyweight对象不存在时,则先创建一个。
◊ Client:维持一个对Flyweight的引用
在享元模式中,Client调用Flyweight下的ConcreteFlyweight,如果ConcreteFlyweight存在则调用成功;否则就调用FlyweightFactory生产所需要的继承Flyweight接口的ConcreteFlyweight,以供调用。
3、享元模式结构实现

Flyweight.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.FlyweightPattern.Structural
{
public abstract class Flyweight
{
public abstract void Operation(int extrinsicstate);
}
}
ConcreteFlyweight.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.FlyweightPattern.Structural
{
public class ConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
}
}
}
UnsharedConcreteFlyweight.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.FlyweightPattern.Structural
{
public class UnsharedConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("UnsharedConcreteFlyweight: " + extrinsicstate);
}
}
}
FlyweightFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Collections; namespace DesignPatterns.FlyweightPattern.Structural
{
public class FlyweightFactory
{
private Hashtable flyweights = new Hashtable(); 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]);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using DesignPatterns.FlyweightPattern.Structural; namespace DesignPatterns.FlyweightPattern
{
class Program
{
static void Main(string[] args)
{
// Arbitrary extrinsic state
int extrinsicstate = ;
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);
}
}
}
运行输出:
ConcreteFlyweight:
ConcreteFlyweight:
ConcreteFlyweight:
UnsharedConcreteFlyweight:
请按任意键继续. . .
4、享元模式应用分析
享元模式适用情形:
◊ 一个应用程序使用了大量的对象
◊ 完全由于使用大量的对象,造成很大的存储开销
◊ 对象的大多数状态都可变为外部状态
◊ 如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象
◊ 应用程序不依赖对象标识
享元模式特点:
◊ 享元模式的核心是把大量共享的对象收集在一起使用简单工厂模式进行管理,避免由于大量的小对象导致系统内存过度消耗。
◊ 享元当重复对象较多时有很好的空间复杂度,但在查找搜索上消耗了时间复杂度。
C#设计模式系列:享元模式(Flyweight)的更多相关文章
- 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern)
原文:乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) 作者:weba ...
- 设计模式-11享元模式(Flyweight Pattern)
1.模式动机 在面向对象程序设计过程中,有时会面临要创建大量相同或相似对象实例的问题.创建那么多的对象将会耗费很多的系统资源,它是系统性能提高的一个瓶颈. 享元模式就是把相同或相似对象的公共部分提取出 ...
- 二十四种设计模式:享元模式(Flyweight Pattern)
享元模式(Flyweight Pattern) 介绍运用共享技术有效地支持大量细粒度的对象. 示例有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要运用共享技术支 ...
- 设计模式之享元模式(Flyweight)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 【UE4 设计模式】享元模式 Flyweight Pattern
概述 描述 运用共享技术有效地支持大量细粒度对象的复用.系统只使用少量的对象,而这些对象都很相似,状态变化很小,可以实现对象的多次复用. 由于享元模式要求能够共享的对象必须是细粒度对象,因此它又称为轻 ...
- [设计模式] 11 享元模式 Flyweight
转 http://blog.csdn.net/wuzhekai1985/article/details/6670298 问题 在面向对象系统的设计何实现中,创建对象是最为常见的操作.这里面就有一个问题 ...
- 设计模式 笔记 享元模式 Flyweight
//---------------------------15/04/20---------------------------- //Flyweight 享元模式------对象结构型模式 /* 1 ...
- 【设计模式】—— 享元模式Flyweight
前言:[模式总览]——————————by xingoo 模式意图 享元模式,也叫[轻量级模式]或者[蝇量级模式].主要目的就是为了减少细粒度资源的消耗.比如,一个编辑器用到大量的字母数字和符号,但是 ...
- 结构型设计模式之享元模式(Flyweight)
结构 意图 运用共享技术有效地支持大量细粒度的对象. 适用性 一个应用程序使用了大量的对象. 完全由于使用大量的对象,造成很大的存储开销. 对象的大多数状态都可变为外部状态. 如果删除对象的外部状态, ...
- 设计模式之享元模式(FlyWeight)
#include <iostream> #include <string> #include <list> #include <vector> usin ...
随机推荐
- 迎战Meta 2,微软新专利有望解决Hololens视场角野窄问题
上周,微软HoloLens的竞争对手AR眼镜Meta 2正式发货,微软是该急了.我们知道Meta 2不仅在价格上比HoloLens便宜,而且在性能上也不弱,Meta2的可视角度达到90度,比HoloL ...
- 基于redis的点赞功能设计
前言 点赞其实是一个很有意思的功能.基本的设计思路有大致两种, 一种自然是用mysql等 数据库直接落地存储, 另外一种就是利用点赞的业务特征来扔到redis(或memcache)中, 然后离线刷回m ...
- 如何让eclipse进行智能提示?
1.打开eclipse工具,点击window菜单,选择preferences选项 2.然后,选择Java->Editor->content assist 3.然后在Auto activat ...
- 第二章 Matlab面向对象编程基础
DeepLab是一款基于Matlab面向对象编程的深度学习工具箱,所以了解Matlab面向对象编程的特点是必要的.笔者在做Matlab面向对象编程的时候发现无论是互联网上还是书店里卖的各式Matlab ...
- Angular 单元格合并
在Angular实现表格输出的话,使用ng-repeat输出信息, 使用了: ng-repeat-start ng-repeat-end ng-hide="$first" < ...
- JavaScript-Function基础知识
function 1. 定义:一段预先设置的代码块,可以反复调用,根据输入参数的不同,返回不同的值: 2. 函数的声明方法: (1)function 命令声明函数 functio ...
- Codeforces Round #361 (Div. 2) B
B - Mike and Shortcuts Description Recently, Mike was very busy with studying for exams and contests ...
- 关于iOS后台问题( 一 )(ios后台刷新,后台定位,后台下载,真后台)
关于iOS的后台,以下引用一些文段进行一下脑补,请同学们大致看一下,有个基础,原文出处 -------------------------------------------------------- ...
- C代码实现数组
直接上代码,没有什么说的 # include <stdio.h> # include <malloc.h> # include <stdlib.h> //用C实现数 ...
- WPF中的Pack URI
更多资源:http://denghejun.github.io 问题 说来也简单:首先,我在WPF项目中建立了一个用户自定义控件(CustomControl),VS模板为我们自动生成了 CustomC ...