Esempio n. 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();
        }
Esempio n. 2
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();
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
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));
        }
Esempio n. 5
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();
        }