结构型设计模式之享元模式(Flyweight)
| 结构 | ![]() |
| 意图 | 运用共享技术有效地支持大量细粒度的对象。 |
| 适用性 |
|
using System;
using System.Collections; class FlyweightFactory
{
private ArrayList pool = new ArrayList(); // the flyweightfactory can crete all entries in the pool at startup
// (if the pool is small, and it is likely all will be used), or as
// needed, if the pool si large and it is likely some will never be used
public FlyweightFactory()
{
pool.Add(new ConcreteEvenFlyweight());
pool.Add(new ConcreteUnevenFlyweight());
} public Flyweight GetFlyweight(int key)
{
// here we would determine if the flyweight identified by key
// exists, and if so return it. If not, we would create it.
// As in this demo we have implementation all the possible
// flyweights we wish to use, we retrun the suitable one.
int i = key % ;
return((Flyweight)pool[i]);
}
} abstract class Flyweight
{
abstract public void DoOperation(int extrinsicState);
} class UnsharedConcreteFlyweight : Flyweight
{
override public void DoOperation(int extrinsicState)
{ }
} class ConcreteEvenFlyweight : Flyweight
{
override public void DoOperation(int extrinsicState)
{
Console.WriteLine("In ConcreteEvenFlyweight.DoOperation: {0}", extrinsicState);
}
} class ConcreteUnevenFlyweight : Flyweight
{
override public void DoOperation(int extrinsicState)
{
Console.WriteLine("In ConcreteUnevenFlyweight.DoOperation: {0}", extrinsicState);
}
} /// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
int[] data = {,,,,,,,}; FlyweightFactory f = new FlyweightFactory(); int extrinsicState = ;
foreach (int i in data)
{
Flyweight flyweight = f.GetFlyweight(i);
flyweight.DoOperation(extrinsicState);
} return ;
}
}
享元模式
结构型设计模式之享元模式(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
概述 描述 运用共享技术有效地支持大量细粒度对象的复用.系统只使用少量的对象,而这些对象都很相似,状态变化很小,可以实现对象的多次复用. 由于享元模式要求能够共享的对象必须是细粒度对象,因此它又称为轻 ...
- 设计模式 笔记 享元模式 Flyweight
//---------------------------15/04/20---------------------------- //Flyweight 享元模式------对象结构型模式 /* 1 ...
- [设计模式] 11 享元模式 Flyweight
转 http://blog.csdn.net/wuzhekai1985/article/details/6670298 问题 在面向对象系统的设计何实现中,创建对象是最为常见的操作.这里面就有一个问题 ...
- 【设计模式】—— 享元模式Flyweight
前言:[模式总览]——————————by xingoo 模式意图 享元模式,也叫[轻量级模式]或者[蝇量级模式].主要目的就是为了减少细粒度资源的消耗.比如,一个编辑器用到大量的字母数字和符号,但是 ...
- 设计模式之享元模式(FlyWeight)
#include <iostream> #include <string> #include <list> #include <vector> usin ...
随机推荐
- C# 禁止在textBox输入框输入非法字符
首先添加下面代码 //禁止在textBox输入框输入非法字符 private void keypressed(Object o, KeyPressEventArgs e) { if (e.KeyCha ...
- 第三章习题 C++ Primer 第六版
1.使用一个整数输入自己的身高(单位为cm),并将此身高转化为米和厘米共同表示的形式,使用下划线字符来指示输入的位置,使用一个const符号常量来表示转换因子. #include<iostrea ...
- 包围轮廓的矩形边界 opencv
#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; ...
- json loads/dumps
json.dumps : dict转成str json.loads:str转成dict dict_ = {1: 2, 3: 4, "} print(type(dict_), dict_) # ...
- 2286: [Sdoi2011]消耗战
2286: [Sdoi2011]消耗战 链接 分析 虚树练习题. 构建虚树,在虚树上DP. 跟着gxb学虚-tree... 代码 #include <cstdio> #include &l ...
- 自定义控件的getChildCount
我真的是一步一步走过来,看过来的代码.不是能力问题,而是他们用的,我没用过,我用的他们不用.然后一句一句的问为什么,然后一句一句的去想为什么. 只有这样,才能慢慢的熟悉,东一榔头西一棒子,不是分模块再 ...
- APP开发手记01(app与web的困惑)
文章链接:http://quke.org/post/app-dev-fragment.html (转载时请注明本文出处及文章链接) 最近在用博客园的wcf服务做博客园的android和ios的app, ...
- 记录下MoKee编译过程
纯属记录帖 关注和了解这个rom有段时间了. 最近有需要了解odex,折腾了几天还是在坑里. 索性,先编译下MoKee看看. 之前make过 4.2 和 5.1 ,刷到模拟器和N5里. 编译教程可以参 ...
- Linux认知之旅【03 进一步了解Linux命令】!
再仔细的研究一下命令,你会进一步提高提高对Linux的操作! 看完本文有空http://man.linuxde.net/转转!这是个好网站! 一.命令是什么? 计算机术语[command]:形容在对计 ...
- Spring 笔记(一)概念梳理
概念 预备知识 1. POJO POJO是Plain Old Java Object的缩写,是软件开发大师Martin Fowler提出的一个概念,指的是一个普通Java类.也就说,你随便编写一个Ja ...
