/// <summary>
        /// Retrieves a discount strategy associated to the customer type
        /// </summary>
        /// <param name="customerType">Based on the type of customer, return the appropriate discount strategy</param>
        /// <returns>the appropriate discount strategy</returns>
        public static IDiscountStrategy GetDiscountStrategyForCustomerType(StrategyPattern.Enumerations.EventEnumerations.CustomerType customerType)
        {
            //Retrieve the enumeration's attributes specified in Enumerations/EventEnumerations.cs
                FieldInfo field = customerType.GetType().GetField(customerType.ToString());
                object[] attribs = field.GetCustomAttributes(typeof(DiscountTypeAttribute), false);

                //If no attribute was specified, then return the normal price by default
                if (attribs.Length == 0)
                    return new NullDiscountStrategy();

                return Activator.CreateInstance((attribs[0] as DiscountTypeAttribute).DiscountStrategy) as IDiscountStrategy;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("DESIGN PATTERNS DEMYSTIFIED !!!");
            IPattern pattern;

            pattern = new StrategyPattern();
            pattern.Implement();

            pattern = new DependencyInjection();
            pattern.Implement();

            pattern = new ObserverPattern();
            pattern.Implement();

            Console.ReadLine();
        }
        public IEnumerable<Event> GetEvents(StrategyPattern.Enumerations.EventEnumerations.CustomerType customerType)
        {
            //Hydrate list with mock data. Each event has a base price (used for the standard customer type)
            IEnumerable<Event> events = new List<Event>()
            {
               {new Event{ Name = "Symposium on Software Development", Date = new DateTime(2015, 12, 15), TicketPrice = new Price{ Cost = 150},
                Location = new Location{ Address1 = "55 test street", Address2="", Name="The Blue Theater", State="NY", Zipcode="11757" }}},
               {new Event{ Name = "SQL Injection and XSS Attacks", Date = new DateTime(2016, 1, 21), TicketPrice = new Price{ Cost = 75} ,
                Location = new Location{ Address1 = "55 test street", Address2="", Name="The Blue Theater", State="NY", Zipcode="11757" }}},
               {new Event{ Name = "Security in Software Development", Date = new DateTime(2015, 11, 14), TicketPrice = new Price{ Cost = 250},
                Location = new Location{ Address1 = "55 test street", Address2="", Name="The Blue Theater", State="NY", Zipcode="11757" }}}
               };

            //Determine which strategy to apply based on the customer type
            IDiscountStrategy discountStrategy = DiscountFactory.GetDiscountStrategyForCustomerType(customerType);

            //Apply the discount selected to each product, and return the discounted collection
            return events.ApplyDiscount(discountStrategy);
        }
Beispiel #4
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine(_configuration["Logging:LogLevel:Default"]);

            IDesignPattern specificationPattern = new SpecificationPattern();

            specificationPattern.ExecuteSample();

            IDesignPattern factoryMethod = new FactoryMethodPattern();

            factoryMethod.ExecuteSample();

            IDesignPattern singletonPattern = new SingletonPattern();

            singletonPattern.ExecuteSample();

            IDesignPattern commandPattern = new CommandPattern();

            commandPattern.ExecuteSample();

            IDesignPattern chainOfResponsibilityPattern = new ChainOfResponsibilityPattern();

            chainOfResponsibilityPattern.ExecuteSample();

            IDesignPattern decoratorPattern = new DecoratorPattern();

            decoratorPattern.ExecuteSample();

            IDesignPattern strategyPattern = new StrategyPattern();

            strategyPattern.ExecuteSample();

            IDesignPattern abstractFactoryPattern = new AbstractFactoryPattern();

            abstractFactoryPattern.ExecuteSample();

            IDesignPattern compositePattern = new CompositePattern();

            compositePattern.ExecuteSample();

            IDesignPattern bridgePattern = new BridgePattern();

            bridgePattern.ExecuteSample();

            IDesignPattern observerPattern = new ObserverPattern();

            observerPattern.ExecuteSample();

            IDesignPattern statePattern = new StatePattern();

            statePattern.ExecuteSample();

            IDesignPattern mediatorPattern = new MediatorPattern();

            mediatorPattern.ExecuteSample();

            IDesignPattern visitorPattern = new VisitorPattern();

            visitorPattern.ExecuteSample();

            IDesignPattern momentoPattern = new MomentoPattern();

            momentoPattern.ExecuteSample();

            return(Task.CompletedTask);
        }