Esempio n. 1
0
        static void Main(string[] args)
        {
            //VectorRenderer renderer = new VectorRenderer();
            RasterRenderer renderer = new RasterRenderer();
            var            circle   = new Circle(renderer, 3);

            circle.Draw();
            circle.Resize(2);
            circle.Draw();

            //nacin sa Dependency Injection jer "je zamorno rucno upisivati renderere" ako ti npr treba 100 krugova sa istim rendererom
            //iz Nugeta nam treba autofac
            var cb = new ContainerBuilder();

            //registrujemo da se automatski kad neki metod trazi IRenderer ubacuje instanca VectorRenderera
            cb.RegisterType <VectorRenderer>().As <IRenderer>().SingleInstance(); // ako su svi isti mozemo stavit da bude singleton

            //registrujemo kako se pravi krug
            cb.Register((c, p) => new Circle(c.Resolve <IRenderer>(), p.Positional <float>(0)));

            //koristimo cb
            using (var c = cb.Build())
            {
                var circleDI = c.Resolve <Circle>(new PositionalParameter(0, 5.0f)); //mora 5.0f ne moze 5
                circleDI.Draw();
                circleDI.Resize(2);
                circleDI.Draw();
            }

            Console.ReadLine();
        }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello Life without Bridge Design pattern!");

            var triangleVectorRenderer = new LifeWithoutBridge.TriangleVectorRenderer();

            Console.WriteLine(triangleVectorRenderer.Draw());

            var squareVectorRenderer = new LifeWithoutBridge.SquareVectorRenderer();

            Console.WriteLine(squareVectorRenderer.Draw());


            Console.WriteLine("Hello Bridge Design pattern!");

            var vectorRenderer = new VectorRenderer();
            var rasterRenderer = new RasterRenderer();

            var triangle = new Triangle(vectorRenderer);

            Console.WriteLine(triangle.Draw());

            var square = new Square(rasterRenderer);

            Console.WriteLine(square.Draw());
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            //  Without Autofac
            WriteLine("Without Autofac:");
            IRenderer renderer = new RasterRenderer();
            var       circle   = new Circle(renderer, 5);

            circle.Draw();
            circle.Resize(0.3f);
            circle.Draw();
            renderer = new VectorRenderer();
            circle   = new Circle(renderer, 7);
            circle.Draw();
            WriteLine();

            //  With Autofac
            WriteLine("With Autofac:");
            var cb = new ContainerBuilder();

            cb.RegisterType <VectorRenderer>().As <IRenderer>().SingleInstance();
            cb.Register((c, p) => new Circle(c.Resolve <IRenderer>(), p.Positional <float>(0)));
            using (var c = cb.Build())
            {
                var circleAuto = c.Resolve <Circle>(new PositionalParameter(0, 5.0f));
                circleAuto.Draw();
                circleAuto.Resize(0.3f);
                circleAuto.Draw();
            }
        }
Esempio n. 4
0
        public static void BridgePattern()
        {
            IRenderer renderer = new RasterRenderer();
            var       circle   = new Circle(renderer, 5);

            circle.Draw();
            circle.Resize(2);
            circle.Draw();
        }
Esempio n. 5
0
        private static void Main(string[] args)
        {
            /*
             * In this example we see the highly coupled implementation of a psuedo ThreadScheduler
             * for different platforms. The problem with this approach is for every variation of ThreadScheduler
             * every platform needs to implement their variations as well.
             */

            var winpts = new WindowsPTS();

            winpts.Schedule("Windows PT#1");
            var wincts = new WindowsCTS();

            wincts.Schedule("Windows CT#1");
            var nixpts = new UnixPTS();

            nixpts.Schedule("Unix PT#1");
            var nixcts = new UnixCTS();

            nixcts.Schedule("Unix CT#1");

            /*
             * The example below illustrates a better way of implementing this ThreadScheduler by using
             * the bridge pattern. We seperate variations of ThreadScheduler, and implemented a parameterized
             * ctor for each variation of ThreadScheduler (in base class) that requires the platform scheduler.
             * This way platform schedulers does not need to extends any scheduler variation.
             */

            var winplatform = new WindowsPlatform();
            var nixplatform = new UnixPlatform();

            var pts = new PreemptiveThreadScheduler(winplatform);
            var cts = new CooperativeScheduler(nixplatform);

            pts.Schedule("Windows Thread #1");
            cts.Schedule("Unix Thread #1");

            /*
             * Below is the course example of Bridge pattern in which we required to refactor a
             * psuedo renderer that is capable of rendering objects in various forms.
             */

            var vrenderer = new VectorRenderer();
            var rrenderer = new RasterRenderer();

            var square = new Square(vrenderer);

            WriteLine(square);
            square.Renderer = rrenderer;
            WriteLine(square);

            var triangle = new Triangle(vrenderer);

            WriteLine(triangle);
            triangle.Renderer = rrenderer;
            WriteLine(triangle);
        }
        static void Main(string[] args)
        {
            IRenderer renderer = new RasterRenderer(); // In reality we would use DI
            var       circle   = new Circle(renderer, 5);

            circle.Draw();
            circle.Resize(5);
            circle.Draw();
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            IRenderer render  = new VectorRenderer();
            IRenderer render2 = new RasterRenderer();
            var       circle  = new Circle(render2, 5);

            circle.Draw();
            circle.Resize(5);
            circle.Draw();
        }