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 ...
随机推荐
- 巴特沃斯(Butterworth)滤波器 (2) - 双线性变换
这里接着上篇讲一下双线性变换Bilinear Transformation,它实现了模拟信号(连续域)与数字信号(离散域)之间的转换. 双线性变换公式如下: 反推可得到: 因此可以根据连续域传递函数推 ...
- 配置gradle.properties
在一些项目中会分拆app 和 lib , 这时候引用support的时候,一旦更改版本会出现需要同步更改两个地方的问题.这种情况,可以通过配置gradle.properties实现替换. 在项目编译过 ...
- Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- AMD与CMD(转载)
JavaSript模块化 在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发? 模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题 ...
- javascript 全局对象--w3school
JavaScript全局对象 1. decodeURI()解析某个编码的URI. 2.decodeURInComponent()解析一个编码的URI组件. 3.encodeURI()把字符串编码为U ...
- C,C++
C与C++的Struct有何区别,Java有Struct吗,C++里Struct与Class区别: C++虚析构函数作用: static静态变量初始化: 深复制与浅复制区别: const * int ...
- CentOS安装配置redis
安装前准备,安装gcc 先用 gcc -v命令检测本机是否安装gcc,如果没有则用下面命令安装: yum install cpp yum install binutils yum install gl ...
- sphinx搜索实例
多个关键词搜索一个字段的几种方式测试 @proname "ygm" "磨粉机" "2" //搜索结果proname字段必须同时包含3个词,有 ...
- css制作对话框
当你发现好多图都能用css画出来的时候,你就会觉得css很有魅力了.//我是这么觉得的,先不考虑什么兼容问题 像漫画里出现的对话框,往往都是一个对话框然后就加入一个箭头指向说话的那一方,来表示这个内容 ...
- >hibernate.cfg.xml的一些常用配置
1.数据库的基本配置信息 hibernate.connection.driver_class是配置数据库驱动 hibernate.connection.url是配置数据库的url hibernate. ...