其他分享
首页 > 其他分享> > 设计模式——享元模式

设计模式——享元模式

作者:互联网

定义

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

两种状态:

内部状态:在享元对象内部并且不会随环境改变的共享部分。
外部状态:随环境的变化发生改变的,不可以共享的状态的外部状态。

特点

享元模式可以避免大量给常相似类的开销,在程序设计中有时需要生成大量细粒度的类的实例来表示数据,如果能发现这些实例除了几个参数外基本上都是相同的有时就能够收大富度的减少需要实例化的数量,如果能把那些参数移到类实例的外面,在方法调用时将他们传递进来看,就可以通过共享大富度的减少单个实例的数目。

结构图

在这里插入图片描述

享元模式模板代码:

//Flyweight 类,它是所有具体享原类的超类或者接口通过这个接口,可以接受并作用于外部状态

 abstract class Flyweight
    {
        public abstract void Operation(int extrinsicstate);
    }
 

//concreteFlyweight,继承Flyweight超类或实现Flyweight接口,并为内部转台增加存储空间

  
    class ConcreteFlyweight:Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("具体Flyweight:" + extrinsicstate);
        }
    }
    

unsharedconcreteflyweight不需要共享的Flyweight子类,因为Flyweight接口共享成为可能,但她不强制共享

    
    class UnshareConcreteFlyweight:Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("不共享的具体Flyweight" + extrinsicstate);
        }
    }

FlyweightFactory享元工厂,创建并管理Flyweight对象,它主要是用来确保合理的共享Flyweight,当用户请求一个Flyweight时,Flyweightfactory可以提供一个已经创建的实例或者创建一个

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]);
        }
       
    }
}

客户端代码

class Program
    {
        static void Main(string[] args)
        {
            int extrinsicstate = 22;

            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 UnshareConcreteFlyweight();

            uf.Operation(--extrinsicstate);
            Console.Read();
              }
      }
    

实例:

namespace 第二十六章网站共享代码
{
    //网站抽象类
    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;
        }

    }

    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 fl = f.GetWebSiteCategory("售货");
            fl.Use();

            WebSite fm = f.GetWebSiteCategory("相亲");
            fm.Use();
            Console.WriteLine("网站分类总数为{0}", f.GetWebSiteCount());
            Console.Read();



        }
    }
}

标签:享元,extrinsicstate,WebSite,flyweights,模式,Flyweight,new,设计模式,public
来源: https://blog.csdn.net/weixin_40486955/article/details/90141206