惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
T
Tailwind CSS Blog
V
Visual Studio Blog
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
量子位
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
Jina AI
Jina AI
雷峰网
雷峰网
博客园 - 【当耐特】
博客园 - 叶小钗
美团技术团队
宝玉的分享
宝玉的分享
IT之家
IT之家

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(5) Flyweight 享元模式
Tristan(GuoZhijian) · 2008-03-08 · via 博客园 - Tristan(GuoZhijian)

VS 2008

应用程序中,当需要提供某种大量细粒度对象的访问,并且对这些对象可以抽象出不可变的属性,那么可以将抽象出来的对象设计为享元,供各个客户端共享访问,以节省内存开销。
而剩下的不能共享的属性,则可以由客户端维护,并在需要的时候传入享元,传入的不能共享的属性不能影响内部的不可变属性。

1. 模式UML图

2. 应用

    目前的应用程序探测系统中,需要对许多应用程序进行探测,探测各种可能出现的异常,探测到异常后,需要将异常抛出,既告诉探测程序当前发生异常的应用程序的信息以及异常消息。
    考虑使用享元模式,使得对于同一个应用程序的多个线程共享应用程序信息。


示意性代码:

IAlarmEventArgs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Flyweight.BLL {
    
public interface IAlarmEventArgs {
        
void Display(string message);
    }

}

AlarmEventArgs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Flyweight.BLL {
    
public class AlarmEventArgs : IAlarmEventArgs {

        
private string m_AppName;

        
public AlarmEventArgs(string appName) {
            
this.m_AppName = appName;
        }


        
IAlarmEventArgs Members
    }

}

AlarmEventArgsFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Flyweight.BLL {
    
public class AlarmEventArgsFactory {

        
private Dictionary<string, IAlarmEventArgs> dictionary = new Dictionary<string, IAlarmEventArgs>();

        
public IAlarmEventArgs Create(string appName) {
            
if (!dictionary.ContainsKey(appName)) {
                dictionary.Add(appName, 
new AlarmEventArgs(appName));
            }

            
return dictionary[appName];
        }

    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Flyweight.BLL;

namespace DesignPattern.Flyweight {
    
class Program {
        
static void Main(string[] args) {

            AlarmEventArgsFactory factory 
= new AlarmEventArgsFactory();

            
string appName1 = "sh3hgridservice";
            factory.Create(appName1).Display(
"thread stopped!");

            factory.Create(appName1).Display(
"connect to database failed!");

        }

    }

}

Output

3. 思考关于intrinsic state和extrinsic state
确保我们共享的是前者,而后者只在客户端维护,并在需要的时候才传入享元
并且后者传入享元后不会影响到前者