設計模式-12-享元

說明dom

1.創建抽象享元產品AbsFlyweight,使FlyweightA,FlyweightB繼承他,而且具體產品的屬性不相同,處理好外部參數和內部參數ide

2.創建享元工廠FlyweightFactory並聚合抽象享元產品,只須要new出全部具體產品並經過外部參數的傳遞,實現不一樣的功能,下降建立大型對象的內存消耗this

3.創建享元管理器執行AbsFlyweight的方法spa

 public abstract class AbsFlyweight
    {
       
        public abstract void Exc(int a, int b, int c);
        public abstract void Exc();
    }
class FlyweightA : AbsFlyweight
    {
        int a,  b,  c;


        public override void Exc(int a, int b, int c)
        {
        }

        public override void Exc()
        {
        }

        public FlyweightA()
        {

        }
        public FlyweightA(int a, int b, int c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }
 class FlyweightB : AbsFlyweight
    {
        int a, b;
        public FlyweightB(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
        public FlyweightB()
        {

        }

        public override void Exc(int a, int b, int c)
        {
            
        }

        public override void Exc()
        {
        }
    }
public class FlyweightFactory
    {
        List<AbsFlyweight> lAbsFlyweight;
        public FlyweightFactory()
        {
            lAbsFlyweight = new List<AbsFlyweight>();
            lAbsFlyweight.Add(new FlyweightA());
            lAbsFlyweight.Add(new FlyweightB());
        }
        public AbsFlyweight Get(int index)
        {
            return lAbsFlyweight[index];
        }
    }
public class FlyweightManage
    {
        int[] la, lb, lc,ltype;
        Random r;
        int count;
        public FlyweightManage(int count, Random r)
        {
            this.r = r;
            this.count = count;
            la =new int[count];
            lb = new int[count];
            lc = new int[count];
            ltype = new int[count];
            for (int i = 0; i < count; i++)
            {
                la[i]=(r.Next());
                lb[i]=(r.Next());
                lc[i]=(r.Next());
                ltype[i] = r.Next() % 2;
            }
        }
        public void Exc()
        {
            FlyweightFactory fact = new FlyweightFactory();
            for (int i = 0; i < count; i++)
            {
                fact.Get(r.Next(ltype[i])).Exc(la[i], lb[i], lc[i]);
            }
        }
        

    }
  int count = 10000000;
            Random r = new Random();
            Console.WriteLine("不使用享元建立對象");
            Stopwatch watch1 = new Stopwatch();
            watch1.Start();
            List<AbsFlyweight> lAbsFlyweight = new List<AbsFlyweight>();
            for (int i=0;i<count;i++)
            {
                AbsFlyweight f;
                if (r.Next()%2==0)
                {
                     f = new FlyweightA(r.Next(), r.Next(), r.Next());
                }
                else
                {
                     f = new FlyweightB(r.Next(), r.Next());
                }
                lAbsFlyweight.Add(f);
            }
            foreach(var eAbsFlyweight in lAbsFlyweight)
            {
                eAbsFlyweight.Exc();
            }
            watch1.Stop();
            Console.WriteLine("用時:"+watch1.ElapsedMilliseconds);
            Console.WriteLine("使用享元建立對象");
            watch1.Reset();
            watch1.Start();
            FlyweightManage flyweightManage = new FlyweightManage(count,r);
            flyweightManage.Exc();
            watch1.Stop();
            Console.WriteLine("用時:" + watch1.ElapsedMilliseconds);
相關文章
相關標籤/搜索