策略模式(Strategy)

复制代码
 1 /*
 2  * 在图书销售时,根据不同类型的图书有不同的折扣,计算金额时必须区别对待,
 3  * 例如计算机类图书7折,英语类图书6折。应用策略模式,用C#控制台应用程序
 4  * 实现该设计。
 5  */
 6 using System;
 7 using System.Collections.Generic;
 8 using System.Linq;
 9 using System.Text;
10 
11 namespace Strategy
12 {
13     abstract class Strategy
14     {
15         public abstract double AlgorithmInterface(double Money);
16     }
17     class CSStrategy : Strategy
18     {
19           public override double AlgorithmInterface(double Money)
20         {
21             Console.WriteLine("计算机类书打七折。");
22             return (Money*0.7);
23         }
24     }
25     class EngStrategy : Strategy
26     {
27        public override double AlgorithmInterface(double Money)
28         {
29             Console.WriteLine("英语类书打六折。");
30             return (Money * 0.6);
31         }
32     }
33     class Context
34     {
35         Strategy strategy;
36         public Context(Strategy strategy)
37         {
38             this.strategy = strategy;
39         }
40         public double GetResult(double Money)
41         {
42             return strategy.AlgorithmInterface(Money);
43         }
44     }
45     class Program
46     {
47         static void Main(string[] args)
48         {
49             Context context;
50             context = new Context(new CSStrategy());
51             Console.WriteLine("需支付" + context.GetResult(100) + "");
52 
53             context = new Context(new EngStrategy());
54             Console.WriteLine("需支付" + context.GetResult(100) + "");
55         }
56     }
57 }
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/archive/2012/05/16/2505570.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值