Flyweight mode-sharing objects freely

  Flyweight definition : the use of sharing technology to effectively support a large number of fine-grained objects.
  The definition of Flyweight Mode puts forward two requirements, fine-grained and shared objects . Because of the requirement of fine-grainedness, it is inevitable that there will be a large number of objects with similar properties. At this time, we divide the information of these objects into two parts: internal state and external state.

  Status classification of flyweight objects:

  • Internal state: the shared part that is inside the flyweight object and will not change as the environment changes
  • External state: the unshareable part that changes with the change of the environment
  • (For example, in Gobang, the black and white colors of the pieces are the internal state, and the position coordinates of the pieces are the external state)

  scenes to be used:

  • The program uses a lot of objects, and these objects cause a lot of storage overhead.
  • Most of the object's state is the external state. If the external state of the object is deleted, then a large number of object groups can be replaced with relatively few shared objects.

  advantage:

  • It greatly reduces the number of objects that need to be created and avoids the overhead of a large number of similar classes by sharing existing objects (only one copy of the same object is saved), thereby improving the utilization of system resources.

  Disadvantages:

  • It is necessary to maintain a list of all flyweights in the system, which itself consumes resources.
  • When there are not enough object instances to share, do not force the use of Flyweight mode, because in order to make the object shareable, some state needs to be externalized, which complicates the logic of the program.

Insert picture description here
  Code background: There is not much difference between different websites. The frameworks are all shareable, but the functions of the websites are different. Some are used to display products, and some are used to publish news.

User category: customer account used for the website, which is the external state of the website category

    class User
    {
    
    
        private string name;
        public User(string name)
        {
    
    
            this.name = name;
        }
        public string Name
        {
    
    
            get {
    
     return name; }
        }
    }

Website abstract class:

    abstract class WebSite
    {
    
    
        //使用方法需要传递用户对象
        public abstract void Use(User user);
    }

Specific website categories:

    class ConcreteWebSite:WebSite
    {
    
    
        private string name = "";
        public ConcreteWebSite(string name)
        {
    
    
            this.name = name;
        }
        public override void Use(User user)//重写Use方法
        {
    
    
            Console.WriteLine("网站分类:" + name +" 用户:"+user.Name) ;
        }
    }

Website factory class:

using System.Collections;//为了使用哈希表

    class WebSiteFactory
    {
    
    
        private Hashtable flyweights = new Hashtable();

        //获得网站分类
        public WebSite GetWebSiteCategory(string key)
        {
    
    
        	//判断键是不是不存在,这里if语句简写了
            if (!flyweights.ContainsKey(key))
                flyweights.Add(key,new ConcreteWebSite(key));//是不存在,就以键为name实例化一个ConcreteWebSite对象添加到哈希表
            return ((WebSite )flyweights[key]);//返回flyweights键为key的值,并把值强转成WebSite类型
        }
        //获得网站分类总数
        public int GetWebSiteCount()
        {
    
    
            return flyweights.Count;
        }
    }

Client:

        static void Main(string[] args)
        {
    
    
            WebSiteFactory f = new WebSiteFactory();
            WebSite fx = f.GetWebSiteCategory ("产品展示");
            fx.Use(new User("咪西"));//实例用户对象,用于Use方法显示

            WebSite fy = f.GetWebSiteCategory("产品展示");
            fy.Use(new User("啾咪"));

            WebSite f1 = f.GetWebSiteCategory("博客");
            f1.Use(new User("旧浪"));

            WebSite f2 = f.GetWebSiteCategory("小游戏");
            f2.Use(new User("7k7k"));
			
			//实际网站只有三种
            Console.WriteLine("得到网站分类总数为{0}",f.GetWebSiteCount ());

        }

Insert picture description here

Guess you like

Origin blog.csdn.net/CharmaineXia/article/details/111063511