• 设计模式之享元模式


    享元模式:运用共享技术有效地支持大量细粒度的对象
    在这里插入图片描述
    FlyweightFactory(工厂类)
    负责产生和管理Flyweight的组件
    内部通常使用容器类来存储共享的Flyweight组件
    提供工厂方法产生对应的组件,当产生的是共享组件时,就加入到Flyweight管理容器内

    Flyweight(组件接口)
    定义组件的操作接口

    ConcreteFlyweight(可以共享的组件)
    实现Flyweight接口
    产生的组件时可以共享的,并加入到Flyweight管理器中

    UnsharedConcreteFlyweight(不可以共享的组件)
    实现Flyweight接口,也可以选择不继承自Flyweight接口
    可以定义为单独的组件,不包含任何共享资源
    也可以讲一些共享组件定义为类的成员,成为内部状态;并另外定义其他不被共享的成员,作为外部状态使用

     class Program
        {
            static void Main(string[] args)
            {
                int extrinsicstate = 25;
    
                FlyweightFactory f = new FlyweightFactory();
    
                Flyweight fx = f.GetFlyweight("X");
                fx.Operation(--extrinsicstate);
    
    
                Flyweight fy = f.GetFlyweight("Y");
                fx.Operation(--extrinsicstate);
    
                Flyweight fz = f.GetFlyweight("Z");
                fx.Operation(--extrinsicstate);
    
                Flyweight uf = new UnsharedConcreteFlyweight();
    
                uf.Operation(--extrinsicstate);
    
    
                Console.ReadLine();
            }
        }
        abstract class Flyweight
        {
            public abstract void Operation(int extrinsicstate);
    
        }
    
        class ConcreteFlyweight : Flyweight
        {
            public override void Operation(int extrinsicstate)
            {
                Console.WriteLine("具体Flyweight:"+extrinsicstate);
            }
        }
    
        class UnsharedConcreteFlyweight : Flyweight
        {
            public override void Operation(int extrinsicstate)
            {
                Console.WriteLine("不共享的具体Flyweight:"+extrinsicstate);
            }
        }
    
        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]);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    享元模式的实例

     class Program
        {
            static void Main(string[] args)
            {
                WebSiteFactory f = new WebSiteFactory();
    
                WebSite fx = f.GetWebSiteCategory("网页");
                fx.Use();
    
                WebSite fy = f.GetWebSiteCategory("网页");
                fy.Use();
    
                WebSite fz = f.GetWebSiteCategory("网页");
                fz.Use();
    
    
                WebSite f1 = f.GetWebSiteCategory("博客");
                f1.Use();
    
                WebSite f2 = f.GetWebSiteCategory("博客");
                f2.Use();
    
                WebSite f3 = f.GetWebSiteCategory("博客");
                f3.Use();
    
    
                Console.WriteLine("网站分类总数为{0}",f.GetWebSiteCount());
    
                Console.ReadLine();
            }
        }
    
        abstract class WebSite
        {
            public abstract void Use();
        }
    
        class ConcreteWebSite : WebSite
        {
            private string name = "";
    
            public ConcreteWebSite(string name)
            {
                this.name = name;
            }
            public override void Use()
            {
                Console.WriteLine("网络分类:"+name);
            }
        }
    
        class WebSiteFactory
        {
            private Hashtable flyweights = new Hashtable();
    
            //获得网站分类
            public WebSite GetWebSiteCategory(string key)
            {
                if (!flyweights.ContainsKey(key))
                {
                    flyweights.Add(key, new ConcreteWebSite(key));
                }
                return ((WebSite)flyweights[key]);
            }
    
            //获得网站分离总数
            public int GetWebSiteCount()
            {
                return flyweights.Count;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
  • 相关阅读:
    易云维医院后勤综合管理平台为医院智慧后勤的建设与发展做出贡献
    javacc之路5---词法分析器技巧
    字符串(字符数组)的各种操作功能
    MindSponge分子动力学模拟——多路径分子模拟(2024.05)
    Android学习笔记 4. ImageView
    字符函数和字符串函数
    记录一下:基于nginx配置的封禁真实IP
    typing.Union` 标注一多种变量类型
    前端网络框架Bootstrap教程|Front-End Web UI Frameworks and Tools: Bootstrap4
    nvm 管理 node版本
  • 原文地址:https://blog.csdn.net/ht_game/article/details/133079882