public static Flyweight getSignInfo(ExtrinsicState key)
        {
            Flyweight flyweight = null;

            foreach (KeyValuePair <ExtrinsicState, Flyweight> pair in pool)
            {
                if (pair.Key.hashCode() == key.hashCode() && pair.Key.equals(key))
                {
                    flyweight = pool[pair.Key];
                }
            }
            if (flyweight == null)
            {
                flyweight = new ConcreteFlyweight1(key);
                pool[key] = flyweight;
            }
            return(flyweight);
        }
        public static void main()
        {
            ExtrinsicState state1 = new ExtrinsicState();

            state1.setSubject("科目1");
            state1.setLocaltion("上海");

            ExtrinsicState state2 = new ExtrinsicState();

            state2.setSubject("科目1");
            state2.setLocaltion("上海");

            FlyweightFactory.getSignInfo(state1);
            FlyweightFactory.getSignInfo(state2);

            SignInfoFactory.getSignInfobyString(state1.getSubjectandLocation());
            SignInfoFactory.getSignInfobyString(state2.getSubjectandLocation());

            //第一个版本,直接用ExtrinsicState结构作为索引的时候
            long time = System.DateTime.Now.Ticks;

            for (int index = 0; index < 10000000; index++)
            {
                FlyweightFactory.getSignInfo(state2);
            }
            long curtime = System.DateTime.Now.Ticks;

            Console.WriteLine("直接用ExtrinsicState结构作为索引的时候 执行时间=" + (curtime - time) + "ticks" + ",startTime=" + time + ",endTime=" + curtime);

            //第二个版本,用string作为索引的时候
            long   time2  = System.DateTime.Now.Ticks;
            string strKey = state2.getSubject() + "." + state2.getLocation();

            for (int index = 0; index < 10000000; index++)
            {
                SignInfoFactory.getSignInfobyString(strKey);
            }
            long curtime2 = System.DateTime.Now.Ticks;

            Console.WriteLine("用string作为索引的时候 执行时间=" + (curtime2 - time2) + "ticks" + ",startTime=" + time2 + ",endTime=" + curtime2);
        }
        //进化版本,优化索引问题
        public static Flyweight getSignInfobyString(string key)
        {
            Flyweight flyweight = null;

            foreach (KeyValuePair <ExtrinsicState, Flyweight> pair in pool)
            {
                if (pair.Key.hashCode() == key.GetHashCode() && pair.Key.getLocation() == key)
                {
                    flyweight = pool[pair.Key];
                }
            }
            if (flyweight == null)
            {
                ExtrinsicState newState = new ExtrinsicState();
                newState.setSubject(key.Split('.')[0]);
                newState.setLocaltion(key.Split('.')[1]);
                flyweight      = new ConcreteFlyweight1(newState);
                pool[newState] = flyweight;
            }
            return(flyweight);
        }
 public ConcreteFlyweight2(ExtrinsicState Extrinsic) : base(Extrinsic)
 {
 }
 public bool equals(ExtrinsicState otherState)
 {
     return(this.subject == otherState.subject && this.location == otherState.location);
 }
示例#6
0
        protected readonly ExtrinsicState Extrinsic;//外部状态只能被写一次

        public Flyweight(ExtrinsicState Extrinsic)
        {
            this.Extrinsic = Extrinsic;
        }