static void Main(string[] args) { List <Item> shippingItems = new List <Item>(); shippingItems.Add(new Item { Name = "Some Documents", ItemWeight = 0.5, ItemType = ItemCategory.Documents }); shippingItems.Add(new Item { Name = "Sony Viao Laptop", ItemWeight = 3, ItemType = ItemCategory.Electronics }); shippingItems.Add(new Item { Name = "Flower Vace", ItemWeight = 5, ItemType = ItemCategory.Fragile }); Context ctx = new Context(); ctx.SetStrategy(new StrategyConcrete_USPSShipping()); ctx.PrintShippingMethod(); ctx.PrintShippingCost(shippingItems); ctx.SetStrategy(new StrategyConcrete_FedexGroundShipping()); ctx.PrintShippingMethod(); ctx.PrintShippingCost(shippingItems); ctx.SetStrategy(new StrategyConcrete_FedexAirShipping()); ctx.PrintShippingMethod(); ctx.PrintShippingCost(shippingItems); Console.ReadKey(); }
static void Main(string[] args) { Context ct = new Context(); ct.SetStrategy(new StrategyA()); ct.ContextInterface(); ct.SetStrategy(new StrategyB()); ct.ContextInterface(); ct.SetStrategy(new StrategyC()); ct.ContextInterface(); Console.ReadKey(); }
public void Test() { Context context = new Context(); context.SetStrategy(new ConcreteStrategy()); //可以在运行时指定类型,通过配置文件和反射机制实现 context.Algorithm(); //使用算法 }
static void Main(string[] args) { // The client code picks a concrete strategy and passes it to the // context. The client should be aware of the differences between // strategies in order to make the right choice. var context = new Context(); Console.WriteLine("Client: Strategy is set to normal sorting."); context.SetStrategy(new ConcreteStrategyA()); context.DoSomeBusinessLogic(); Console.WriteLine(); Console.WriteLine("Client: Strategy is set to reverse sorting."); context.SetStrategy(new ConcreteStrategyB()); context.DoSomeBusinessLogic(); }
static void Main(string[] args) { Console.WriteLine("***************************************************************"); Console.WriteLine("* Captura la opción del formato de imagen que deseas subir: *"); Console.WriteLine("* 1 = BMP *"); Console.WriteLine("* 2 = PNG *"); Console.WriteLine("* 3 = JPEG *"); Console.WriteLine("***************************************************************"); string text; do { Context context = new Context(); text = Console.ReadLine(); switch (text) { case "1": context.SetStrategy(new BmpStrategy()); break; case "2": context.SetStrategy(new PngStrategy()); break; case "3": context.SetStrategy(new JpegStrategy()); break; default: Console.WriteLine("La opción no es válida"); context = null; break; } if (context != null) { Console.WriteLine("Captura el nombre de la imagen"); string fileName = Console.ReadLine(); context.SaveImage(fileName); } } while (text != null); }