Beispiel #1
0
        static void Main(string[] args)
        {
            int i = 100;
            FlyweightFactory factory = new FlyweightFactory();

            Flyweight flyA = factory.GetFlyweight("A");

            flyA.Operation(i);

            Flyweight flyB = factory.GetFlyweight("B");

            flyB.Operation(i * 2);

            Flyweight flyC = factory.GetFlyweight("C");

            flyC.Operation(i * 3);

            Flyweight flyD = factory.GetFlyweight("D");

            flyD.Operation(i * 4);

            Flyweight flyE = new UnsharedFlyweight();

            flyE.Operation(i / 2);

            Flyweight flyF = factory.GetFlyweight("A");

            flyF.Operation(i / 2);
        }
Beispiel #2
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            // Arbitrary extrinsic state
            int extrinsicstate = 22;

            FlyweightFactory factory = new FlyweightFactory();

            // Work with different flyweight instances
            Flyweight fx = factory.GetFlyweight("X");

            fx.Operation(--extrinsicstate);

            Flyweight fy = factory.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);

            Flyweight fz = factory.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight fu = new
                                           UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);

            // Wait for user
            Console.ReadKey();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            //介绍
            //    意图:运用共享技术有效地支持大量细粒度的对象。

            //主要解决:在有大量对象时,有可能会造成内存溢出,我们把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重新创建。

            //何时使用: 1、系统中有大量对象。 2、这些对象消耗大量内存。 3、这些对象的状态大部分可以外部化。 4、这些对象可以按照内蕴状态分为很多组,当把外蕴对象从对象中剔除出来时,每一组对象都可以用一个对象来代替。 5、系统不依赖于这些对象身份,这些对象是不可分辨的。

            //如何解决:用唯一标识码判断,如果在内存中有,则返回这个唯一标识码所标识的对象。

            //关键代码:用 HashMap 存储这些对象。

            //应用实例: 1、JAVA 中的 String,如果有则返回,如果没有则创建一个字符串保存在字符串缓存池里面。 2、数据库的数据池。

            //优点:大大减少对象的创建,降低系统的内存,使效率提高。

            //缺点:提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。

            //使用场景: 1、系统有大量相似对象。 2、需要缓冲池的场景。

            //注意事项: 1、注意划分外部状态和内部状态,否则可能会引起线程安全问题。 2、这些类必须有一个工厂对象加以控制。

            int extrinsicstate = 22;
            var factory        = new FlyweightFactory();
            var flyweightX     = factory.GetFlyweight("X");

            flyweightX.Operation(--extrinsicstate);

            var flyweightY = factory.GetFlyweight("Y");

            flyweightY.Operation(--extrinsicstate);

            var flyweightZ = factory.GetFlyweight("Z");

            flyweightZ.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight unsharedConcreteFlyweight = new UnsharedConcreteFlyweight();

            unsharedConcreteFlyweight.Operation(--extrinsicstate);



            var webSiteFactory = new WebSiteFactory();
            var fx             = webSiteFactory.GetWebSite("产品展示");

            fx.Use(new User("X"));

            var fy = webSiteFactory.GetWebSite("产品展示");

            fy.Use(new User("Y"));

            var fz = webSiteFactory.GetWebSite("产品展示");

            fz.Use(new User("Z"));
            Console.WriteLine(webSiteFactory.GetWebSiteCount());

            Console.ReadLine();
        }
Beispiel #4
0
        public void Launch()
        {
            int extrinsicState = 22;

            FlyweightFactory factory = new FlyweightFactory();

            IFlyweight flyweightA = factory.GetFlyweight("Flyweight A");
            IFlyweight flyweightB = factory.GetFlyweight("Flyweight B");
            IFlyweight flyweightC = factory.GetFlyweight("Flyweight C");

            flyweightA.Operation(--extrinsicState);
            flyweightB.Operation(--extrinsicState);
            flyweightC.Operation(--extrinsicState);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            FlyweightFactory factory    = new FlyweightFactory();
            Flyweight        flyweight1 = factory.GetFlyweight("related");

            flyweight1.Operation("hello world");
            Flyweight flyweight2 = factory.GetFlyweight("unrelated");

            flyweight2.Operation("hello second world");
            Flyweight flyweight3 = factory.GetFlyweight("related");

            flyweight3.Operation("hello super world");
            Flyweight flyweight4 = factory.GetFlyweight("unrelated");

            flyweight4.Operation("Bye");
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            // 定义外部状态,例如字母的位置等信息
            int externalstate = 10;

            string[]         strings = new[] { "A", "B", "C", "D" };
            Random           random  = new Random();
            FlyweightFactory factory = new FlyweightFactory();

            for (int i = 0; i < 10; i++)
            {
                var       s  = strings[random.Next(4)];
                Flyweight fd = factory.GetFlyweight(s);
                if (fd != null)
                {
                    fd.Operate(--externalstate);
                }
                else
                {
                    Console.WriteLine("{0}加入驻留池", s);
                    factory.Flyweights.Add("D", new ConcreteFlyweight(s));
                }
            }

            Console.ReadKey();
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            try
            {
                {//BasicStructure
                    int extrinsicState = 22;
                    FlyweightFactory flyweightFactory = new FlyweightFactory();
                    var flyweight = flyweightFactory.GetFlyweight("X");
                    flyweight.Operation(--extrinsicState);

                    flyweight = flyweightFactory.GetFlyweight("Y");
                    flyweight.Operation(--extrinsicState);

                    flyweight = flyweightFactory.GetFlyweight("Z");
                    flyweight.Operation(--extrinsicState);

                    UnSharedConcreteFlyweight uf = new UnSharedConcreteFlyweight();
                    uf.Operation(--extrinsicState);
                }
                {//SituationSimulation
                    WebSiteFactory webSiteFactory = new WebSiteFactory();
                    var            fx             = webSiteFactory.GetWebSiteCategory("产品展示");
                    fx.Use();
                    var fy = webSiteFactory.GetWebSiteCategory("产品展示");
                    fy.Use();
                    var fz = webSiteFactory.GetWebSiteCategory("产品展示");
                    fz.Use();
                    var fl = webSiteFactory.GetWebSiteCategory("博客");
                    fl.Use();
                    var fm = webSiteFactory.GetWebSiteCategory("博客");
                    fm.Use();
                    var fn = webSiteFactory.GetWebSiteCategory("博客");
                    fn.Use();

                    Console.WriteLine($"网站分类总数为:{webSiteFactory.GetWebSiteCount()}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
        void Test()
        {
            // 外部状态
            int extrinsicState       = 22;
            FlyweightFactory factory = new FlyweightFactory();

            Flyweight fx = factory.GetFlyweight("X");

            fx.Operation(--extrinsicState);

            Flyweight fy = factory.GetFlyweight("Y");

            fy.Operation(--extrinsicState);

            Flyweight fz = factory.GetFlyweight("Z");

            fz.Operation(--extrinsicState);

            Flyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(--extrinsicState);
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            var extrinsicstate = 22;
            FlyweightFactory f = new FlyweightFactory();

            Flyweight fx = f.GetFlyweight("X");

            fx.OPeration(--extrinsicstate);

            Flyweight fy = f.GetFlyweight("Y");

            fy.OPeration(--extrinsicstate);

            Flyweight fz = f.GetFlyweight("Z");

            fz.OPeration(--extrinsicstate);

            UnshareConcreteFlyweight uf = new UnshareConcreteFlyweight();

            uf.OPeration(--extrinsicstate);

            Console.Read();
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            FlyweightFactory flyweightFactory = new FlyweightFactory();

            Flyweight fA = flyweightFactory.GetFlyweight("A");

            fA.Operation(10);

            Flyweight fB = flyweightFactory.GetFlyweight("A");

            fB.Operation(9);

            Flyweight fC = flyweightFactory.GetFlyweight("C");

            fC.Operation(8);

            UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(7);

            Console.WriteLine("fA == fB:" + (fA == fB));
            Console.WriteLine("fA == fC:" + (fA == fC));
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            int extrinsincstate = 22;
            FlyweightFactory f  = new FlyweightFactory();

            Flyweight fx = f.GetFlyweight("X");

            fx.Operation(extrinsincstate);

            Flyweight fy = f.GetFlyweight("Y");

            fy.Operation(extrinsincstate);

            Flyweight fz = f.GetFlyweight("Z");

            fz.Operation(extrinsincstate);

            Flyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(extrinsincstate);

            Console.ReadKey();
        }
Beispiel #12
0
        public static void BaseSample()
        {
            FlyweightFactory factory = new FlyweightFactory();
            var fa = factory.GetFlyweight("A");

            if (fa != null)
            {
                fa.Operation(1);
            }
            var fb = factory.GetFlyweight("B");

            if (fb != null)
            {
                fb.Operation(2);
            }
            else
            {
                factory.flyweights.Add("B", new ConcreteFlyweight("B"));
                fb = factory.GetFlyweight("B");
            }

            fb.Operation(2);
            Console.ReadKey();
        }
Beispiel #13
0
        public static void addCarToPoliceDatabase(FlyweightFactory factory, Car car)
        {
            Console.WriteLine("\nClient: Adding a car to database.");

            var flyweight = factory.GetFlyweight(new Car
            {
                Color   = car.Color,
                Model   = car.Model,
                Company = car.Company
            });

            // The client code either stores or calculates extrinsic state and
            // passes it to the flyweight's methods.
            flyweight.Operation(car);
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            int extrinsicstate       = 12;
            FlyweightFactory factory = new FlyweightFactory();

            Flyweight fx = factory.GetFlyweight("X");

            fx.Operation(--extrinsicstate);

            Flyweight fy = factory.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);

            Flyweight fz = factory.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);

            UnshardConcreteFlyweight uf = new UnshardConcreteFlyweight();

            uf.Operation(--extrinsicstate);


            Console.Read();
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            int i = 100;
            FlyweightFactory factory = new FlyweightFactory();

            Flyweight flyA = factory.GetFlyweight("A");

            flyA.Operation(i);

            Flyweight flyB = factory.GetFlyweight("B");

            flyB.Operation(i * 2);

            Flyweight flyC = factory.GetFlyweight("C");

            flyC.Operation(i * 3);

            Flyweight flyD = factory.GetFlyweight("D");

            flyD.Operation(i * 4);

            Flyweight flyE = new UnsharedFlyweight();

            flyE.Operation(i / 2);

            Flyweight flyF = factory.GetFlyweight("A");

            flyF.Operation(i / 2);

            Console.WriteLine("================================");
            //共享图片工厂
            ImageNodeFactory imageNodeFactory = new ImageNodeFactory();

            Dictionary <int, ImageNode> imageList = new Dictionary <int, ImageNode>();
            int    randomNum = 0;
            Random random    = new Random();

            for (int j = 0; j < 10; j++)
            {
                randomNum = random.Next(0, 10);
                switch (randomNum / 2)
                {
                case 0:
                    imageList.Add(j, imageNodeFactory.GetImage("Lion"));
                    break;

                case 1:
                    imageList.Add(j, imageNodeFactory.GetImage("Tiger"));
                    break;

                case 2:
                    imageList.Add(j, imageNodeFactory.GetImage("Cat"));
                    break;

                default:
                    imageList.Add(j, imageNodeFactory.GetImage("Dog"));
                    break;
                }
            }

            //展示所生成的图像
            for (int j = 0; j < 10; j++)
            {
                Console.WriteLine(j + "号坐标的");
                imageList[i].Show();
            }

            //随机选取两个坐标的图像进行比较
            int a = 0;
            int b = 0;

            while (a == b)
            {
                a = random.Next(0, 10);
                b = random.Next(0, 10);
            }

            //由于使用了共享,所以直接判断对象是否相等即可
            if (imageList[a] == imageList[b])
            {
                Console.WriteLine("可以消除");
            }
            else
            {
                Console.WriteLine("不可以消除");
            }
        }