`
cloudtech
  • 浏览: 4590236 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

设计模式(C#)之享元模式(Flyweight Pattern)

 
阅读更多

设计模式(C#)之享元模式(Flyweight Pattern)

代码下载

1.概念

运用共享技术有效地支持大量细粒度的对象。

2.类图

Model.cs

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

namespace FlyweightPattern
{
    public class Model
    {
        public double D1
        {
            get;
            set;
        }

        public double D2
        {
            get;
            set;
        }
    }
}


AbstractSum.cs

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

namespace FlyweightPattern
{
    public abstract class AbstractSum
    {
        public abstract double Sum(Model md);
       
    }
}


Sum1.cs

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

namespace FlyweightPattern
{
    public class Sum1 : AbstractSum
    {
        public override double Sum(Model md)
        {
            return md.D1 + md.D2;
        }
    }
}


Sum2.cs

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

namespace FlyweightPattern
{
    public class Sum2 : AbstractSum
    {
        public override double Sum(Model md)
        {
            return md.D1 * md.D2;
        }
    }
}


FlyweightUse.cs

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

namespace FlyweightPattern
{
    public class FlyweightUse
    {
       
        private Dictionary<string, AbstractSum> _sumObjects = new Dictionary<string, AbstractSum>();
        public AbstractSum GetSumObject(string key)
        {
            AbstractSum SumObject = null;

            if (_sumObjects.ContainsKey(key))
            {
                SumObject = _sumObjects[key];
            }
            else
            {
                switch (key)
                {
                    case "Sum1": SumObject = new Sum1(); break;
                    case "Sum2": SumObject = new Sum2(); break;
                }

                _sumObjects.Add(key, SumObject);
            }

            return SumObject;
        }

    }
}


3.调用

代码下载

分享到:
评论

相关推荐

    C#设计模式_设计模式_C#_

    享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型: 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 16. 观察者模式(Observer Pattern) 17. ...

    C#设计模式-吕震宇

    设计模式(14)-Flyweight Pattern C#设计模式(13)-Proxy Pattern C#设计模式(12)-Decorator Pattern C#设计模式(11)-Composite Pattern C#设计模式(10)-Adapter Pattern C#设计模式(9)-Prototype ...

    C#版 24种设计模式

    适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...

    C#设计模式.PDF

    C#设计模式(4)-Simple Factory Pattern 24 一、 简单工厂(Simple Factory)模式 24 二、 Simple Factory模式角色与结构: 24 三、 程序举例: 25 四、 Simple Factory模式演化 27 五、 优点与缺点: 29 C#设计...

    设计模式代码——c#

    11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 16. 观察者模式(Observer Pattern...

    C#设计模式大全

    C#设计模式(4)-Simple Factory Pattern 一、 简单工厂(Simple Factory)模式 二、 Simple Factory模式角色与结构: 三、 程序举例: 四、 Simple Factory模式演化 五、 优点与缺点: C#设计模式(5)-...

    C#设计模式(23种设计模式)

    享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern...

    32种设计模式

    享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者...

    C#23种设计模式

    11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern)...

    23种设计模式 (创建型,结构型,行为型)

    享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern...

    C#3.0设计模式.pdf

    1. C# Meets Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About Patterns 2 About UML 3 About C# 3.0 5 About the Examples 6 2. Structural ...

    设计模式迷你手册.chm

    设计模式迷你手册.chm,大小仅 188 KB,图文并茂,介绍性强,每个设计模式附有 C++、C# 示例源码示例。 目录: 创建型 Factory Method Abstract Factory Builder Prototype Singleton 结构型 Adapter Bridge ...

    二十三种设计模式【PDF版】

    设计模式之 Flyweight(共享元) 提供 Java运行性能,降低小而大量重复的类的开销. C. 行为模式 设计模式之 Command(命令) 什么是将行为封装,Command 是最好的说明. 设计模式之 Observer(观察者) 介绍如何使用 ...

    asp.net知识库

    多样式星期名字转换 [Design, C#] .NET关于string转换的一个小Bug Regular Expressions 完整的在.net后台执行javascript脚本集合 ASP.NET 中的正则表达式 常用的匹配正则表达式和实例 经典正则表达式 delegate vs. ...

    Unity-Programming-Patterns:Unity中编程模式的集合,以及使用它们的示例。 这些主要来自《游戏编程模式》一书,但从C ++译为C#

    设计模式。 比建筑模式(例如Singleton)更具体。 反模式。 是许多程序员用来解决问题的模式的集合,尽管他们不应该使用它们,因为它们不是解决问题的有效方法。 一个示例是“上帝对象”,最有可能称为Game...

Global site tag (gtag.js) - Google Analytics