コード例 #1
0
        static void Main(string[] args)
        {
            var director   = new Director();
            var carBuilder = new CarBuilder();

            director.ConstructSportsCar(carBuilder);
            Car car = carBuilder.GetResult();

            Console.WriteLine($"Type {car.TypeCar}");

            var carManualBuilder = new CarManualBuilder();

            director.ConstructSportsCar(carManualBuilder);
            Manual carManual = carManualBuilder.GetResult();

            Console.WriteLine($"Type {carManual.TypeCar}");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Mihu89/DesignPatterns
        private static void CreationalPatterns()
        {
            #region Singleton
            Use.Applay();

            /*
             * Computer comp = new Computer();
             * comp.Launch("Windows 8.1");
             * Console.WriteLine(comp.OS.Name);
             *
             * // у нас не получится изменить ОС, так как объект уже создан
             * comp.OS = OS.getInstance("Windows 10");
             * Console.WriteLine(comp.OS.Name);    */
            Parallel.Invoke(
                () => S3_0(),
                () => S3_1()
                );
            #endregion

            #region FactoryMethod
            DP.C.FactoryMethod.Use.Applay();
            #endregion
            #region Prototype
            // Example 1
            DP.C.Prototype.Use.Apply();
            // Example 2
            var prototype = new ConcretPrototype1
            {
                Property1        = "A",
                Property2        = "B",
                PrototypeDetails = new PrototypeDetails {
                    Details = "prototype Details"
                }
            };
            var newObject = prototype.Clone() as ConcretPrototype1;
            newObject.PrototypeDetails.Details = "New details from newObject";

            Console.WriteLine(prototype);
            Console.WriteLine(newObject);

            var prototype2 = new ConcretPrototype2
            {
                OtherProperty    = "Z",
                Property1        = "X",
                Property2        = "Y",
                PrototypeDetails = new PrototypeDetails
                {
                    Details = "Prototype2 details"
                }
            };
            // create new instance using prototype2
            var newObject2 = prototype2.Clone() as ConcretPrototype2;
            newObject2.Property1 = "P01";

            Console.WriteLine(prototype2);
            Console.WriteLine(newObject2);
            #endregion

            #region Builder
            Director   director = new Director();
            CarBuilder builder  = new CarBuilder();
            director.ConstructSportCar(builder);
            Car car = builder.GetResult();

            CarManualBuilder carManualBuilder = new CarManualBuilder();
            director.ConstructSportCar(carManualBuilder);

            Manual manual = carManualBuilder.GetResult();
            #endregion
            //DP.B.Strategy.Real.Use.Applay();
            //DP.B.Mediator.Real.Use.Apply();
            //DP.S.Adapter.Real.Use.Applay();

            Console.ReadLine();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            #region FactoryMethod

            ITransport passengerTransport = new PassengerLogistics().CreateTransport();
            ITransport seaTransport       = new SeaLogistics().CreateTransport();
            ITransport roadTransport      = new RoadLogistics().CreateTransport();
            passengerTransport.Delivery += PrintEvent;
            seaTransport.Delivery       += PrintEvent;
            roadTransport.Delivery      += PrintEvent;
            passengerTransport.Transporting();
            seaTransport.Transporting();
            roadTransport.Transporting();

            #endregion

            #region AbstractFactory

            if (args[0] == "Windows")
            {
                IGuiElement element = new CreatorWinElements();

                GuiMaster master = new GuiMaster(element);
                master.CreateElements();
                master.Paint();
            }

            if (args[0] == "Mac")
            {
                IGuiElement element = new CreatorMacElements();

                GuiMaster master = new GuiMaster(element);
                master.CreateElements();
                master.Paint();
            }
            else
            {
                throw new FormatException(nameof(args));
            }

            #endregion

            #region Builder

            CarBuilder builder  = new CarBuilder();
            Director   director = new Director(builder);
            Car        car      = builder.GetResult();
            Console.WriteLine(director.ToString());
            CarManualBuilder manualBuilder = new CarManualBuilder();
            director = new Director(manualBuilder);
            Manual manual = manualBuilder.GetResult();
            car.Reset();
            Console.WriteLine(director.ToString());
            manual.Reset();

            #endregion

            #region Prototype

            //массив фигур
            Shape[] shapes = new Shape[3];
            //создаем фигуры
            Triangle  triangle  = new Triangle(12.4, 12.4);
            Rectangle rectangle = new Rectangle(5, 10, "Grey", true);
            Circle    circle    = new Circle(256.6, true, "Red");
            //кладём в массив
            shapes.SetValue(triangle, 0);
            shapes.SetValue(circle, 1);
            shapes.SetValue(rectangle, 2);

            //создаем копии
            List <Shape> shapesCopy = new List <Shape>(shapes.Length);
            foreach (Shape shape in shapes)
            {
                shapesCopy.Add(shape.Clone());
            }
            #endregion

            #region Singleton

            DataBase db = DataBase.GetInstance();
            db.Query("SELECT ...");

            #endregion

            #region Adapter
            RoundHole hole = new RoundHole(125.5);
            RoundPeg  peg  = new RoundPeg(175);
            hole.Fits(peg);

            var smallSqpeg = new SquarePage(2);
            var largeSqpeg = new SquarePage(5);
            //hole.Fits(small_sqpeg) // ошибка компиляции, несовместимые типы
            var smallSqpegAdapter = new SquarePegAdapter(smallSqpeg);
            var largeSqpegAdapter = new SquarePegAdapter(largeSqpeg);
            hole.Fits(smallSqpegAdapter);
            hole.Fits(largeSqpegAdapter);

            #endregion

            #region Bridge

            var tv     = new Tv();
            var remote = new AdvancedRemote(tv);
            remote.TooglePower();

            var radio    = new Radio();
            var remoting = new Remote(radio);
            remoting.ChannelDown();
            #endregion

            #region Composite

            var all = new CompoundGraphic();
            all.Add(new Dot(1, 1));
            all.Add(new structure_patterns.Composite.Circle(5, 3, 10.5));

            var group = new CompoundGraphic();
            group.Add(new structure_patterns.Composite.Circle(1, 1, 135.4));
            group.Add(new Dot(15, 15));
            all.Add(group);

            all.Draw();

            #endregion

            #region Decorator

            var source = new FileDataSource("somefile.dat");
            source.WriteData("salaryrecords");
            var decorator = new CompressionDecorator(source);
            source.WriteData("salaryrecords");
            decorator.Compression();

            #endregion

            #region Facade

            var converter  = new VideoConverter();
            var mp4        = converter.Convert("youtubevideo.org", "mp4");
            var fileStream = mp4.OpenRead();
            fileStream.Lock(1, 1);
            #endregion

            #region Cache
            Forest forest = new Forest();
            forest.PlantTree(1, 1, "green", "apple", "textture nature");
            forest.PlantTree(2, 1, "green", "pineapple", "nature");
            forest.Draw(new object());
            #endregion

            #region Proxy

            var service = new ThirdPartyYoutubeClass();
            var proxy   = new CachedYoutubeClass(service);
            var manager = new YoutubeManager(proxy);
            manager.ReactOnUserInput(123);

            #endregion

            #region ChainOfResponsibility

            var dialog = new Dialog("Help Me from Dialog");
            var panel  = new Panel("Help me from Panel");
            var ok     = new Button("Button Ok help text");
            var cancel = new Button("Button canceled help text");
            panel.Add(ok);
            panel.Add(cancel);
            dialog.Add(panel);
            dialog.ShowHelp();
            #endregion
        }