Exemple #1
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();
        }
Exemple #2
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);
        }
Exemple #3
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();
        }
Exemple #4
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();
        }
        private static void ShowHello()
        {
            Console.WriteLine("**********李四********");
            BaseWord h = FlyweightFactory.GetWord(WordType.H);
            BaseWord e = FlyweightFactory.GetWord(WordType.E);
            BaseWord l = FlyweightFactory.GetWord(WordType.L);
            BaseWord o = FlyweightFactory.GetWord(WordType.O);

            Console.WriteLine("{0}{1}{2}{3}{4}",
                              h.Display(), e.Display(),
                              l.Display(), l.Display(),
                              o.Display());
        }
Exemple #6
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);
        }
Exemple #7
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);
        }
Exemple #8
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");
        }
Exemple #9
0
        static void Main(string[] args)
        {
            // The client code usually creates a bunch of pre-populated
            // flyweights in the initialization stage of the application.
            var factory = new FlyweightFactory(
                new Car {
                Company = "Chevrolet", Model = "Camaro2018", Color = "pink"
            },
                new Car {
                Company = "Mercedes Benz", Model = "C300", Color = "black"
            },
                new Car {
                Company = "Mercedes Benz", Model = "C500", Color = "red"
            },
                new Car {
                Company = "BMW", Model = "M5", Color = "red"
            },
                new Car {
                Company = "BMW", Model = "X6", Color = "white"
            }
                );

            factory.listFlyweights();

            addCarToPoliceDatabase(factory, new Car
            {
                Number  = "CL234IR",
                Owner   = "James Doe",
                Company = "BMW",
                Model   = "M5",
                Color   = "red"
            });

            addCarToPoliceDatabase(factory, new Car
            {
                Number  = "CL234IR",
                Owner   = "James Doe",
                Company = "BMW",
                Model   = "X1",
                Color   = "red"
            });

            factory.listFlyweights();
        }
Exemple #10
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();
        }
Exemple #11
0
        /// <summary>
        /// 享元模式:
        /// 如果我们需要大量相同的对象,如果一个个去new的话,会导致很浪费资源和性能,而享元就是为了解决这个问题的
        /// 看到这里,大家脑海里肯定会想:哎,这不是原型模式吗?怎么享元模式跟原型模式一样了?
        ///
        /// 让我来解释一下吧:
        /// 原型模式:为了避免new,而采用克隆的方式来构造对象(注重构造),原型模式是一个创建型模式,所以注重的是创建
        /// 享元模式:不仅仅是为了避免new,更多的还是为了重用一个对象(注重重用),它的一些属性我们是可以直接用的。
        ///
        /// 此文中的例子:大头儿子一家
        /// 假如大头儿子一家三口,家里有两辆车,一辆白色,一辆黑色,然后家里每次出行时需要开车
        /// 不可能说每一次出行都要去新买一辆车吧(new),所以只需要买一个(new一次),之后要使用,直接去车库开就好了
        /// 如果家里的车不够了,假如大头儿子长大了,想要买一辆蓝色的车,此时车库里没有,则需要重新去买一辆放在车库里
        /// 以后大头儿子要开车的话就可以从车库里去开了
        ///
        /// 享元模式又分为:1.内部状态;2.外部状态;
        /// 内部状态:在享元的内部不会因为环境的改变而改变,在上述例子中车就是内部状态,只要买了就在车库,不会改变
        /// 外部状态:会随环境改变而改变,不可以享元的状态,在上述例子中大头儿子一家就是外部状态,车不会变,但是谁开车,这个是会变化的,这个无法享元
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //不管取多少遍车,同一个颜色的车,永远只会实例化一次(买一次)
            FlyweightFactory carFactory = new FlyweightFactory();
            Car car1 = carFactory.GetCar("白色");
            Car car2 = carFactory.GetCar("黑色");
            Car car3 = carFactory.GetCar("蓝色");

            carFactory.GetCar("白色");
            carFactory.GetCar("黑色");
            carFactory.GetCar("蓝色");

            //指定外部环境(谁去开车)
            car1.Drive(new Driver("围裙妈妈"));
            car2.Drive(new Driver("小头爸爸"));
            car3.Drive(new Driver("大头儿子"));

            Console.WriteLine("End");
            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);
        }
Exemple #13
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();
        }
Exemple #14
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));
        }
Exemple #15
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();
        }
        //private static BaseWord e1 = new E();
        //private static BaseWord l = new L();
        //private static BaseWord v = new V();
        //private static BaseWord n = new N();
        /// <summary>
        /// 张三
        /// </summary>
        private static void Show()
        {
            Console.WriteLine("**********张三********");
            //BaseWord e1 = new E();
            //BaseWord l = new L();
            ////BaseWord e2 = new E();
            //BaseWord v = new V();
            ////BaseWord e3 = new E();
            //BaseWord n = new N();

            BaseWord e1 = FlyweightFactory.GetWord(WordType.E);
            BaseWord l  = FlyweightFactory.GetWord(WordType.L);
            //BaseWord e2 = new E();
            BaseWord v = FlyweightFactory.GetWord(WordType.V);
            //BaseWord e3 = new E();
            BaseWord n = FlyweightFactory.GetWord(WordType.N);

            Console.WriteLine("{0}{1}{2}{3}{4}{5}",
                              e1.Display(), l.Display(),
                              e1.Display(), v.Display(),
                              e1.Display(), n.Display());
            //e2.Display(),v.Display(),
            //e3.Display(),n.Display());
        }
Exemple #17
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();
        }
Exemple #18
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();
        }
Exemple #19
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("不可以消除");
            }
        }