示例#1
0
        public static void Main()
        {
            var documents = new List<Manuscript>();
            var formatter = new FancyFormatter(); // new BackwardsFormatter();

            var faq = new Faq(formatter) { Title = "The Bridge Pattern FAQ" };
            faq.Questions.Add("What is it?", "A design pattern");
            faq.Questions.Add("When do we use it?", "When you need to separate an abstraction from an implementation.");
            documents.Add(faq);

            var book = new Book(formatter)
            {
                Title = "Lots of Patterns",
                Author = "John Sonmez",
                Text = "Blah blah blah..."
            };
            documents.Add(book);

            var paper = new TermPaper(formatter)
            {
                Class = "Design Patterns",
                Student = "Joe N00b",
                Text = "Blah blah blah...",
                References = "GOF"
            };
            documents.Add(paper);

            foreach (var doc in documents)
            {
                doc.Print();
            }

            // Wait for user
            Console.ReadKey();
        }
示例#2
0
        public static void FirstDemo()
        {
            var standartFormatter = new StandartFormatter();
            var reverseFormatter  = new ReverseFormatter();
            var crazyFormatter    = new CrazyFormater();
            var book = new Book(standartFormatter)
            {
                Author = "Shawn O Briang", Text = "Cool book about how i lost my life", Title = "7 Circles Of Hell"
            };
            var termPaper = new TermPaper(reverseFormatter)
            {
                Class = "Designs Patterns", Student = "Vladimir Voev", Text = "Bridge Pattern", Reference = "WWW=)"
            };
            var faq = new Faq(crazyFormatter)
            {
                Title = "Bridge Pattern ??"
            };

            faq.Questions.Add("What is this", "A Dessign Pattern");
            faq.Questions.Add("When is good to be used", "When you need to separate an abstraction from an implementation");

            var printer = new List <Printer>();

            printer.Add(book);
            printer.Add(termPaper);
            printer.Add(faq);
            GenerateReport(printer);
        }
示例#3
0
        public void Test()
        {
            var connection = new NhConnection("hibernate.cfg.xml");

            connection.Connecting();
            ISession session = connection.GetSessionFactory().OpenSession();

            var cert = session.Query <Certificate>()
                       .Fetch(x => x.Group)
                       .ThenFetch(x => x.Students)
                       .First();

            cert.Group.Students[0].TermPapers.DoForEach(x => { });

            session.Clear();
            session.Dispose();

            session = connection.GetSessionFactory().OpenSession();

            ITransaction transaction = session.BeginTransaction();

            var paper = new TermPaper {
                Name = "ALALA", Student = cert.Group.Students[0]
            };

            cert.Group.Students[0].TermPapers.Add(paper);

            var group = session.Merge(cert.Group);

            session.Update(group);
            session.Update(group.Certificate);

            transaction.Commit();
            transaction.Dispose();
            session.Dispose();
            return;


            var certificate = session.Query <Certificate>().Fetch(x => x.Group).ThenFetch(x => x.Students).ToList().First();

            session.Dispose();

            certificate.Group.Students.Clear();
            //var student = new Student { Name = "the new maratoss", Group = certificate.Group };
            //certificate.Group.Students.Add(student);

            session = connection.GetSessionFactory().OpenSession();
            session.Update(certificate);
            session.Flush();

            session.Dispose();
        }
示例#4
0
        private async Task HandleKeywords(TermPaperAddCommand command, TermPaper termPaper)
        {
            var existingKeywords = await _keywordRepository.Value.GetKeywordsByValueList(command.Keywords);

            var newKeywords = command.Keywords.Except(existingKeywords.Select(ek => ek.Value));

            foreach (var keyword in existingKeywords)
            {
                termPaper.AddKeyword(new TermPaperKeyword(keyword));
            }
            foreach (var keyword in newKeywords)
            {
                termPaper.AddKeyword(new TermPaperKeyword(new Keyword(keyword)));
            }
        }
示例#5
0
        private async Task HandleCourse(TermPaperAddCommand command, TermPaper termPaper)
        {
            var course = await _courseRepository.Value.GetByName(command.Course);

            if (course == null)
            {
                var area = new Area(command.Area);
                await _areaRepository.Value.AddAsync(area);

                course = new Course(area, command.Course);
                await _courseRepository.Value.AddAsync(course);
            }

            termPaper.SetCourse(course);
        }
示例#6
0
        private async Task HandleStudents(TermPaperAddCommand command, TermPaper termPaper)
        {
            var studentA = new Student(command.Student1);

            studentA.SetTermPaper(termPaper);

            await _studentRepository.Value.AddAsync(studentA);

            if (!string.IsNullOrWhiteSpace(command.Student2))
            {
                var studentB = new Student(command.Student2);
                studentB.SetTermPaper(termPaper);

                await _studentRepository.Value.AddAsync(studentB);
            }
        }
        public ActionResult Get()
        {
            try
            {
                StringBuilder     sb        = new StringBuilder();
                List <Manuscript> documents = new List <Manuscript>();
                var formatter = new StandardFormatter();

                var faq = new FAQ(formatter);
                faq.Title = "The Bridge Pattern FAQ";
                faq.Questions.Add("What is it?", "A design pattern");
                faq.Questions.Add("When do we use it?", "When you need to separate an abstraction from an implementation.");
                documents.Add(faq);

                var book = new Book(formatter)
                {
                    Title  = "Lots of Patterns",
                    Author = "Alfonso Gomez",
                    Text   = "Blah blah blah..."
                };
                documents.Add(book);

                var paper = new TermPaper(formatter)
                {
                    Class      = "Design Patterns",
                    Student    = "Jose Sanchez",
                    Text       = "Blah blah blah...",
                    References = "GOF"
                };
                documents.Add(paper);


                foreach (var doc in documents)
                {
                    sb.Append(doc.Print());
                }

                return(Ok(sb.ToString()));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
示例#8
0
        private async Task HandleAdvisors(TermPaperAddCommand command, TermPaper termPaper)
        {
            var advisor = new Advisor(command.Advisor.ToLower().Trim().Replace(" ", "."), "123", command.Advisor);
            await _advisorRepository.Value.AddAsync(advisor);

            var termPaperAdvisor = new TermPaperAdvisor(AdvisorType.Leader, termPaper, advisor);

            termPaper.AddAdvisor(termPaperAdvisor);

            if (!string.IsNullOrWhiteSpace(command.CoAdvisor))
            {
                var coAdvisor = new Advisor(command.Advisor.ToLower().Trim(), "123", command.Advisor);
                await _advisorRepository.Value.AddAsync(coAdvisor);

                var termPaperCoAdvisor = new TermPaperAdvisor(AdvisorType.CoLeader, termPaper, coAdvisor);
                termPaper.AddAdvisor(termPaperCoAdvisor);
            }
        }
示例#9
0
        private void CreateManuscriptsWith(IFormatter formatter)
        {
            var paper = new TermPaper(formatter)
            {
                Class = "Author Class", References = "Paper References", Student = "Paper Author", Text = "Paper Text"
            };
            var faq = new Faq(formatter)
            {
                Title = "FAQ Title", Questions = new Dictionary <string, string> {
                    { "A", "1" }, { "B", "2" }
                }
            };
            var book = new Book(formatter)
            {
                Author = "Author Connor", Text = "Book Text", Title = "Book Title"
            };

            _documents.Add(paper);
            _documents.Add(faq);
            _documents.Add(book);
        }
示例#10
0
        static void Main(string[] args)
        {
            List <Document>  documents = new List <Document>();
            IFormatterBridge formatter = new StandardFormatter();

            var book = new Book(formatter)
            {
                Author = "Dan Brown",
                Title  = "The Da Vinci Code",
                Text   = "Blah blah blah ..."
            };

            documents.Add(book);

            var paper = new TermPaper(formatter)
            {
                Class      = "Software Engineering",
                Student    = "Jeremy Hall, Clara Knight",
                References = "SWE101",
                Text       = "Blah blah blah ..."
            };

            documents.Add(paper);

            var faq = new FAQ(formatter)
            {
                Title = "Design Patterns"
            };

            faq.Questions.Add("Who owns this?", "No one");
            faq.Questions.Add("Who created this?", "No one");
            documents.Add(faq);

            foreach (var document in documents)
            {
                document.Print();
            }

            System.Console.ReadLine();
        }
示例#11
0
        private static void DemoBridgePattern()
        {
            List <Manuscript> documents = new List <Manuscript>();
            //var formatter = new StandardFormatter();
            BackwardsFormatter formatter = new BackwardsFormatter();

            FAQ faq = new FAQ(formatter)
            {
                Title = "The Bridge pattern FAQ",
            };

            faq.Questions.Add("What is it?", "A design pattern");
            faq.Questions.Add("When do we use it?", "When you need to separate an abstraction from an implementation");
            documents.Add(faq);

            Book book = new Book(formatter)
            {
                Title  = "Lots of patterns",
                Author = "PA",
                Text   = "qwerty qazwsx",
            };

            documents.Add(book);

            TermPaper paper = new TermPaper(formatter)
            {
                Class      = "Design Patterns",
                Student    = "P",
                Text       = "qwerty wsx",
                References = "GOF",
            };

            documents.Add(paper);

            foreach (Manuscript doc in documents)
            {
                doc.Print();
            }
        }
示例#12
0
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("Documents and formatters example");

            var documents = new List <Document>();
            var formatter = new FancyFormatter();

            var faq = new FAQ(formatter);

            faq.Title = "The Bridge Pattern FAQ";
            faq.Questions.Add("What is it?", "A design pattern.");
            faq.Questions.Add("When do we use it?", "When you need to separate an abstraction from an implementation.");

            var book = new Book(formatter)
            {
                Title  = "Head First Design Patterns",
                Author = "Elisabeth Robson & Eric Freeman",
                Text   = "Text...",
            };

            var paper = new TermPaper(formatter)
            {
                Class      = "Design Patterns",
                Student    = "John Doe",
                Text       = "Text about design patterns...",
                References = "GOF",
            };

            documents.Add(faq);
            documents.Add(book);
            documents.Add(paper);

            foreach (var document in documents)
            {
                document.Print();
            }
        }
示例#13
0
        internal static void Main(string[] args)
        {
            var documents = new List<Manuscript>();

            ///Mojem da podadem koito si iskame Formatter tuka i vsichko ste se formatira po razlichen nachin
            var formatter = new FancyFormatter();

            var faq = new FAQ(formatter) { Title = "The Bridge Pattern FAQ" };
            faq.Questions.Add("What is it?", "A design pattern");
            faq.Questions.Add("When do we use it?", "When you need to separate an abstraction from an implementation.");
            documents.Add(faq);

            var book = new Book(formatter)
            {
                Title = "Lots of Patterns",
                Author = "John Sonmez",
                Text = "Blah blah blah..."
            };
            documents.Add(book);

            var paper = new TermPaper(formatter)
            {
                Class = "Design Patterns",
                Student = "Joe N00b",
                Text = "Blah blah blah...",
                References = "GOF"
            };
            documents.Add(paper);

            foreach (var doc in documents)
            {
                doc.Print();
            }

            // Wait for user
            Console.ReadKey();
        }
示例#14
0
        static void Main(string[] args)
        {
            var documents        = new List <Manuscript>();
            var formatter        = new StandardFormatter();
            var reverseFormatter = new ReverseFormatter();

            var faq = new FAQ(formatter);

            faq.Title = "The Bridge Pattern FAQ";
            faq.Questions.Add("What is it?", "A design pattern");
            faq.Questions.Add("When do we use it?", "When you need to separate an bastraction from an implementation");
            documents.Add(faq);

            var book = new Book(formatter)
            {
                Title  = "Lots of Patterns",
                Author = "John Sonmez",
                Text   = "Bla bla bla"
            };

            documents.Add(book);

            var paper = new TermPaper(reverseFormatter)
            {
                Class      = "Design Patterns",
                Student    = "Joe N00b",
                Text       = "Bla bla bla",
                References = "GOF"
            };

            documents.Add(paper);

            foreach (var doc in documents)
            {
                doc.Print();
            }
        }
示例#15
0
        static void Main(string[] args)
        {
            var level1 = new Level1 {
                Name   = "Level1",
                Level2 = new Level2 {
                    Name = "Level2",
                    List = new List <string> {
                        "aaa", "bbb", "ccc"
                    },
                    Level3 = new Level3 {
                        Name = "Level3"
                    }
                }
            };

            var level1_samevalue = new Level1
            {
                Name   = "Level1",
                Level2 = new Level2
                {
                    Name = "Level2",
                    List = new List <string> {
                        "aaa", "bbb", "ccc"
                    },
                    Level3 = new Level3
                    {
                        Name = "Level3"
                    }
                }
            };

            if (level1.ObjectValuesCompare(level1_samevalue))
            {
                Console.WriteLine("Same Values");
            }
            else
            {
                Console.WriteLine("Different Values");
            }
            #region Creational Patterns

            #region Abstract Factory

            Console.WriteLine("*********** Abstract Factory Pattern starts ***********");

            var phoneClient1 = new PhoneClient(MANUFACTURER.NOKIA);
            var normalPhone1 = phoneClient1.GetPhone(PHONETYPE.NORMAL) as INormalPhone;
            var normalPhone2 = phoneClient1.GetPhone(PHONETYPE.SMART) as ISmartPhone;
            Console.WriteLine(normalPhone1.Name());
            Console.WriteLine(normalPhone2.Name());

            var phoneClient2 = new PhoneClient(MANUFACTURER.SONYERICSSON);
            var normalPhone3 = phoneClient2.GetPhone(PHONETYPE.NORMAL) as INormalPhone;
            var normalPhone4 = phoneClient2.GetPhone(PHONETYPE.SMART) as ISmartPhone;
            Console.WriteLine(normalPhone3.Name());
            Console.WriteLine(normalPhone4.Name());

            Console.WriteLine("*********** Abstract Factory Pattern ends ***********");

            #endregion

            #region Factory Method

            Console.WriteLine("*********** Factory Method Pattern starts ***********");

            // Note: constructors call Factory Method
            var documentList = new List <Document>();
            documentList.Add(new Resume());
            documentList.Add(new Report());

            // Display document pages
            foreach (Document document in documentList)
            {
                Console.WriteLine("\n" + document.GetType().Name + "--");
                foreach (Page page in document.Pages)
                {
                    Console.WriteLine(" " + page.GetType().Name);
                }
            }

            Console.WriteLine("*********** Factory Method Pattern ends ***********");

            #endregion

            #region Singleton

            Console.WriteLine("*********** Singleton Pattern starts ***********");

            Singleton <DbConnection> .Instance.Connect();

            Console.WriteLine(string.Format("IsSameInstance = {0}", Singleton <DbConnection> .Instance == Singleton <DbConnection> .Instance));

            Console.WriteLine("*********** Singleton Pattern ends ***********");

            #endregion

            #region Builder

            Console.WriteLine("*********** Builder Pattern starts ***********");

            VehicleBuilder builder;
            // Create shop with vehicle builders
            var shop = new Shop();

            // Construct and display vehicles
            builder = new ScooterBuilder();
            shop.Construct(builder);
            builder.Vehicle.Show();

            builder = new CarBuilder();
            shop.Construct(builder);
            builder.Vehicle.Show();

            builder = new MotorCycleBuilder();
            shop.Construct(builder);
            builder.Vehicle.Show();

            Console.WriteLine("*********** Builder Pattern ends ***********");

            #endregion

            #endregion

            #region Behavioral Patterns

            #region ChainOfResponsibility

            Console.WriteLine("*********** Chain of Responsibility Pattern starts ***********");

            // Setup Chain of Responsibility
            Approver directorLarry    = new CompanyDirector();
            Approver vicePresidentSam = new CompanyVicePresident();
            Approver presidentTammy   = new CompanyPresident();

            directorLarry.SetSuccessor(vicePresidentSam);
            vicePresidentSam.SetSuccessor(presidentTammy);

            // Generate and process purchase requests
            Purchase p = new Purchase(2034, 350.00, "Assets");
            directorLarry.ProcessRequest(p);

            p = new Purchase(2035, 32590.10, "Project X");
            directorLarry.ProcessRequest(p);

            p = new Purchase(2036, 122100.00, "Project Y");
            directorLarry.ProcessRequest(p);

            Console.WriteLine("*********** Chain of Responsibility Pattern ends ***********");

            #endregion

            #region Command

            Console.WriteLine("*********** Command Pattern starts ***********");

            // Create user and let it compute
            var user = new User();

            // User presses calculator buttons
            user.Compute('+', 100);
            user.Compute('-', 50);
            user.Compute('*', 10);
            user.Compute('/', 2);

            // Undo 4 commands
            user.Undo(4);
            // Redo 3 commands
            user.Redo(3);

            Console.WriteLine("*********** Command Pattern ends ***********");

            #endregion

            #region Interpreter

            Console.WriteLine("*********** Interpreter Pattern starts ***********");

            string roman   = "MCMXXVIII";
            var    context = new Context(roman);

            // Build the 'parse tree'
            var tree = new List <Expression>();
            tree.Add(new ThousandExpression());
            tree.Add(new HundredExpression());
            tree.Add(new TenExpression());
            tree.Add(new OneExpression());

            foreach (Expression exp in tree)
            {
                exp.Interpret(context);
            }

            Console.WriteLine("{0} = {1}", roman, context.Output);

            Console.WriteLine("*********** Interpreter Pattern ends ***********");

            #endregion

            #region Mediator

            Console.WriteLine("*********** Mediator Pattern starts ***********");

            // Create chatroom
            IChatroom chatroom = new Chatroom();

            // Create participants and register them
            Participant George = new GraduateStudentParticipant("George");
            Participant Paul   = new GraduateStudentParticipant("Paul");
            Participant Ringo  = new GraduateStudentParticipant("Ringo");
            Participant John   = new GraduateStudentParticipant("John");
            Participant Yoko   = new UnderGraduateStudentParticipant("Yoko");

            chatroom.Register(George);
            chatroom.Register(Paul);
            chatroom.Register(Ringo);
            chatroom.Register(John);
            chatroom.Register(Yoko);

            // Chatting participants
            Yoko.Send("John", "Hi John!");
            Paul.Send("Ringo", "You need to study");
            Ringo.Send("George", "Hello");
            Paul.Send("John", "How are you");
            John.Send("Yoko", "I can explain you Maths");

            Console.WriteLine("*********** Mediator Pattern ends ***********");

            #endregion

            #region Observer

            Console.WriteLine("*********** Observer Pattern starts ***********");

            // Create IBM stock and attach investors
            Stock ibm = new IBM("IBM", 120.00);
            ibm.Attach(new Investor("Sorros"));
            ibm.Attach(new Investor("Berkshire"));

            // Fluctuating prices will notify investors
            ibm.Price = 120.10;
            ibm.Price = 121.00;
            ibm.Price = 120.50;
            ibm.Price = 120.75;

            Console.WriteLine("*********** Observer Pattern ends ***********");

            #endregion

            #region Visitor

            Console.WriteLine("*********** Visitor Pattern starts ***********");

            // Setup employee collection
            Employees e = new Employees();
            e.Attach(new Clerk());
            e.Attach(new Director());
            e.Attach(new President());

            // Employees are 'visited'
            e.Accept(new IncomeVisitor());
            e.Accept(new VacationVisitor());

            Console.WriteLine("*********** Visitor Pattern ends ***********");

            #endregion

            #endregion

            #region Structural Patterns

            #region Bridge

            Console.WriteLine("*********** Bridge Pattern starts ***********");

            var documents = new List <Manuscript>();
            var formatter = new FancyFormatter();

            var faq = new FAQ(formatter);
            faq.Title = "The Bridge Pattern FAQ";
            faq.Questions.Add("What is it?", "A design pattern");
            faq.Questions.Add("When do we use it?", "When you need to separate an abstraction from an implementation.");
            documents.Add(faq);

            var book = new Book(formatter)
            {
                Title  = "Lots of Patterns",
                Author = "John Sonmez",
                Text   = "Blah blah blah..."
            };
            documents.Add(book);

            var paper = new TermPaper(formatter)
            {
                Class      = "Design Patterns",
                Student    = "Joe N00b",
                Text       = "Blah blah blah...",
                References = "GOF"
            };
            documents.Add(paper);

            foreach (var doc in documents)
            {
                doc.Print();
            }

            Console.WriteLine("*********** Bridge Pattern ends ***********");

            #endregion

            #region Decorator

            Console.WriteLine("*********** Decorator Pattern starts ***********");

            // create a Simple Cake Base first
            var cBase = new CakeBase();
            PrintProductDetails(cBase);

            // add cream to the cake
            var creamCake = new CreamDecorator(cBase);
            PrintProductDetails(creamCake);

            // now add a Cherry on it
            var cherryCake = new CherryDecorator(creamCake);
            PrintProductDetails(cherryCake);

            // now add Scent to it
            var scentedCake = new ArtificialScentDecorator(cherryCake);
            PrintProductDetails(scentedCake);

            // Finally add a Name card on the cake
            var nameCardOnCake = new NameCardDecorator(scentedCake);
            PrintProductDetails(nameCardOnCake);

            // now create a simple Pastry
            var pastry = new PastryBase();
            PrintProductDetails(pastry);

            // add cream and cherry only on the pastry
            var creamPastry  = new CreamDecorator(pastry);
            var cherryPastry = new CherryDecorator(creamPastry);
            PrintProductDetails(cherryPastry);

            Console.WriteLine("*********** Decorator Pattern ends ***********");

            #endregion

            #endregion

            #region Service Locator Pattern

            //http://www.c-sharpcorner.com/UploadFile/dacca2/service-locator-design-pattern/
            Console.WriteLine("*********** Service Locator Pattern starts ***********");

            ServiceLocator serviceLocator = new ServiceLocator();
            IServiceA      serviceA       = serviceLocator.GetService <IServiceA>();
            serviceA.Execute();

            IServiceB serviceB = serviceLocator.GetService <IServiceB>();
            serviceB.Execute();

            Console.WriteLine("*********** Service Locator Pattern ends ***********");
            #endregion

            Console.ReadKey();
        }
示例#16
0
 public void SetTermPaper(TermPaper termPaper)
 {
     TermPaper = termPaper;
 }