zoukankan      html  css  js  c++  java
  • Flyweight Pattern简单随笔

           

     享元模式: 以共享的方式高效地支持大量的细粒度对象。

     享元对象的状态:
     
      1:内蕴状态(Internal State)内蕴状态存储在享元对象内部且不会随环境改变而改变。因此内蕴状态并可以共享。

      2:外蕴状态(External State)。外蕴状态是随环境改变而改变的、不可以共享的状态。享元对象的外蕴状态必须由客户端保存,并在享元对象被创建之后,在需要使用的时候再传入到享元对象内部。外蕴状态与内蕴状态是相互独立的。

      享元模式的应用条件:

      1: 一个系统有大量的对象。

      2:这些对象耗费大量的内存

      3:这些对象的状态中的大部分都可以外部化。

      4:这些对象可以按照内蕴状态分成很多的组,当把外蕴对象从对象中剔除时,每一个组都可以仅用一个对象代替。

      5:软件系统不依赖于这些对象的身份,换言之,这些对象可以是不可分辨的。

      .NET的享元模式:
     
      .NET中的String类型就是运用了享元模式。.NET中如果第一次创建了一个字符串对象s1,下次再创建相同的字符串s2时只是把它的引用指向s1所引用的具体对象,这就实现了相同字符串在内存中的共享。

      

       在某些情况下,对象的数量可能会太多,从而导致了运行时的代价。那么我们如何去避免大量细粒度的对象,同时又不影响客户程序使用面向对象的方式进行操作,运用共享技术有效地支持大量细粒度的对象。

    私有属性是元素特有的,可以通过传递参数来进行赋值,而公共属性则通过继承来赋予相应的值:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace FlyWeightPatternSam
     6 {
     7     public abstract class Employer
     8     {
     9         protected string _company = "COMPANY";//公共属性
    10         protected string _companyAddress = "ADDRESS";//公共属性
    11 
    12         public abstract void SetPropertity(string name,string age);
    13         public abstract void ShowInformation();
    14 
    15     }
    16 }
    17 
    18 using System;
    19 using System.Collections.Generic;
    20 using System.Text;
    21 
    22 namespace FlyWeightPatternSam
    23 {
    24     public class EmployerA:Employer 
    25     {
    26         private string _name;//特有属性
    27         private string _age;
    28         private string _id="01";
    29         public override void SetPropertity( string name, string age)
    30         {//传递相应的参数来赋值
    31             //throw new Exception("The method or operation is not implemented.");
    32             this._age = age;
    33            
    34             this._name = name;
    35         }
    36 
    37         public override void ShowInformation()
    38         {
    39             //throw new Exception("The method or operation is not implemented.");
    40             Console.WriteLine(_company +"  "+_companyAddress +"  "+_name +"  "+_id +"   "+_age );
    41         }
    42     }
    43 }
    44 

    主要的是通过工厂来进行对象创建,当存在该对象时就取出该对象,否则抛出异常。同时,对工厂使用单件模式控制唯一性;

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace FlyWeightPatternSam
     6 {
     7     public class FlyweightFactory
     8     {
     9         private static readonly FlyweightFactory instance=new FlyweightFactory();
    10         System.Collections.Generic.Dictionary<string, Employer> dictionary = new Dictionary<string, Employer>();
    11 
    12         private FlyweightFactory() 
    13         {
    14             dictionary.Add("01",new EmployerA());
    15             dictionary.Add("02",new EmployerB());
    16             dictionary.Add("03",new EmployerC());
    17         }
    18         
    19         public static  FlyweightFactory Instance
    20         {
    21             get
    22             {
    23                 return instance;
    24             }
    25         }
    26 
    27         public Employer GetEmployer(string str) 
    28         {
    29             Employer employer = null ;
    30             try
    31             {
    32                 employer = dictionary[str] as Employer;    
    33             }
    34             catch(Exception ex)
    35             {
    36                 Console.WriteLine(ex.Message);
    37             }
    38             return employer;
    39         }
    40     }
    41 }
    42 

    客户端代码如下所示:

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace FlyWeightPatternSam
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {

                Employer employer 
    = FlyweightFactory.Instance.GetEmployer("01");
                employer.SetPropertity(
    "jasen","24");
                employer.ShowInformation();
                Console.ReadLine();
                
                employer 
    = FlyweightFactory.Instance.GetEmployer("04");
                Console.ReadLine();
                
                employer 
    = FlyweightFactory.Instance.GetEmployer("01");
                employer.SetPropertity(
    "jasen""25");
                employer.ShowInformation();
                Console.ReadLine();
            }
        }
    }

    操作结果如下:

    **************************************************************************

    源代码下载:/Files/jasenkin/FlyWeightPatternSam.rar

  • 相关阅读:
    微信群之Java技术红包问答
    Java JPA 查询实体部分字段
    我的iphone6退货之路
    Simulated Annealing (模拟退火)
    ICML 2017 Reading (1): Combining Online and Offline Knowledge in UCT
    ICML 2017 Reading
    NIPS 2014
    Gumbel-Max trick, Perturb and MAP and more
    ICML 2014
    非常有趣的JavaScript游戏
  • 原文地址:https://www.cnblogs.com/jasenkin/p/1688249.html
Copyright © 2011-2022 走看看