public void ResolveStrategyAlgorithms(TransportTypesEnum transportType)
        {
            switch (transportType)
            {
            case TransportTypesEnum.Bus:
                this.ContextStrategy.setStrategy(new PublicAlgorithm());
                break;

            case TransportTypesEnum.Car:
                this.ContextStrategy.setStrategy(new CarAlgorithm());
                break;

            case TransportTypesEnum.Bicycle:
                this.ContextStrategy.setStrategy(new BicycleAlgorithm());
                break;

            case TransportTypesEnum.Walk:
                this.ContextStrategy.setStrategy(new WalkAlgorithm());
                break;

            default:
                this.ContextStrategy = null;
                throw new NotSupportedException("The transport type is not supported! Please use an existing transport type.");
            }
        }
        static void Main(string[] args)
        {
            var typeOfCarrier = Console.ReadLine();

            if (string.IsNullOrEmpty(typeOfCarrier))
            {
                return;
            }

            var shippingStrategies = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetInterface(nameof(IShippingStrategy)) != null).ToList();

            var shippingStrategyToInstantiate = shippingStrategies.FirstOrDefault(x => x.Name.ToLower().Contains(typeOfCarrier.ToLower()));

            if (shippingStrategyToInstantiate == null)
            {
                return;
            }

            var shippingStrategyToInject = Activator.CreateInstance(shippingStrategyToInstantiate) as IShippingStrategy;

            var context = new StrategyContext(shippingStrategyToInject);

            Console.WriteLine(context.CalculateShippingCost());

            Console.ReadKey();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            StrategyContext strategyContext = new StrategyContext();

            strategyContext.Write("简单消息");
            strategyContext.Write("复杂消息:捕获到一个未知的异常!");
            Console.ReadKey();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            StrategyContext context1 = new StrategyContext(new StrategyA());

            context1.Run();
            StrategyContext context2 = new StrategyContext(new StrategyB());

            context2.Run();

            Console.Read();
        }
        public static void Main(string[] args)
        {
            var strategyContext = new StrategyContext(new FullSeasonDiscount());

            strategyContext.ContextInterface(100);

            Console.WriteLine();

            strategyContext = new StrategyContext(new WinterDiscount());
            strategyContext.ContextInterface(100);

            Console.WriteLine();

            strategyContext = new StrategyContext(new MerryChristmasDiscount());
            strategyContext.ContextInterface(100);


            Console.ReadKey();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("输入客户类型(vip,svip):");
            string customer = Console.ReadLine();

            StrategyContext context;

            decimal amount = 100M;

            if (customer == "vip")
            {
                context = new StrategyContext(new VipDiscountStrategy());
                amount  = context.ExecuteStrategy(amount);
            }
            else if (customer == "svip")
            {
                context = new StrategyContext(new SVipDiscountStrategy());
                amount  = context.ExecuteStrategy(amount);
            }
            Console.WriteLine(amount);
            Console.ReadKey();
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            var reports = new List <DeveloperReport>
            {
                new DeveloperReport {
                    Id = 1, Name = "Developer4", Level = "Junior", HourlyRate = 20.5, WorkingHours = 130
                },
                new DeveloperReport {
                    Id = 2, Name = "Developer1", Level = "Junior", HourlyRate = 30, WorkingHours = 110
                },
                new DeveloperReport {
                    Id = 3, Name = "Developer3", Level = "Senior", HourlyRate = 32.5, WorkingHours = 180
                },
                new DeveloperReport {
                    Id = 3, Name = "Developer2", Level = "SuperSenior", HourlyRate = 32.5, WorkingHours = 140
                },
            };

            // Create StrategyContext 'context' object for calling CommonMethod i.e.
            var context = new StrategyContext(new Strategy1());

            var juniorTotal = context.CommonStrategyEntry(reports);

            context.SetContext(new Strategy2());
            var seniorTotal = context.CommonStrategyEntry(reports);

            context.SetContext(new Strategy3());
            var superSeniorTotal = context.CommonStrategyEntry(reports);

            Console.WriteLine($"Total amount for junior salaries is: {juniorTotal}");
            Console.WriteLine($"Total amount for senior salaries is: {seniorTotal}");
            Console.WriteLine($"Total amount for senior salaries is: {superSeniorTotal}");
            Console.WriteLine($"Total cost for all the salaries is: {juniorTotal + seniorTotal + superSeniorTotal}");

            Console.ReadLine();
        }
 public Client()
 {
     ContextStrategy = new StrategyContext();
 }