关于某道C#上机题的OO - 策略模式
前段时间看到某人《关于某道C#上机题的OO》 ,后来又有人用了装饰模式做这题,我这里来个策略模式,不习惯废话直接上代码,不知道算不算策略模式,请高人指点。
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 namespace ConsoleApp
5 {
6 public class Program
7 {
8 static void Main(string[] args)
9 {
10 Game game = new Game(17);
11 //设定策略
12 game.Strategy = new StrategyA();
13 game.GameOver += new EventHandler(game_GameOver);
14 game.Start();
15 Console.Read();
16 }
17 static void game_GameOver(object sender, EventArgs e)
18 {
19 Console.WriteLine("Last Person:" + sender);
20 }
21 }
22 /// <summary>
23 /// 策略接口
24 /// </summary>
25 public interface IStrategy
26 {
27 /// <summary>
28 /// 运行策略
29 /// </summary>
30 /// <param name="g"></param>
31 void Work(Game g);
32 }
33 /// <summary>
34 /// 策略A
35 /// 从第一个人开始报数,报到3的倍数退出,一直到剩下最后一个人,用面向对象的思想去做这道题。
36 /// </summary>
37 public class StrategyA : IStrategy
38 {
39 private List<Person> over = new List<Person>();
40 private int k = 0;
41 public void Work(Game game)
42 {
43 foreach (Person p in game.Players)
44 {
45 p.Say(++k);
46 if (k % 3 == 0)
47 over.Add(p);
48 }
49 game.Players.RemoveAll(o => over.Contains(o));
50 }
51 }
52 public delegate void EventHandler(object sender , EventArgs e);
53 /// <summary>
54 /// 游戏
55 /// </summary>
56 public class Game
57 {
58 public Game(int num)
59 {
60 Players = new List<Person>();
61 for (int i = 0; i < num; i++) {
62 Players.Add(new Person(i + 1));
63 }
64 }
65 /// <summary>
66 /// 游戏策略
67 /// </summary>
68 public IStrategy Strategy { get; set; }
69 /// <summary>
70 /// 游戏玩家
71 /// </summary>
72 public List<Person> Players { get; set; }
73 /// <summary>
74 /// 游戏结束事件
75 /// </summary>
76 public event EventHandler GameOver;
77 /// <summary>
78 /// 开始游戏
79 /// </summary>
80 public void Start()
81 {
82 if (Strategy != null)
83 {
84 while (Players.Count > 1)
85 {
86 Strategy.Work(this);
87 }
88 GameOver(this.Players.First().Id, new EventArgs());
89 }
90 }
91 }
92 /// <summary>
93 /// 玩家
94 /// </summary>
95 public class Person
96 {
97 public Person(int id)
98 {
99 this.Id = id;
100 }
101 /// <summary>
102 /// 玩家ID
103 /// </summary>
104 public int Id { get; set; }
105 /// <summary>
106 /// 玩家报数
107 /// </summary>
108 /// <param name="num"></param>
109 public void Say(int num)
110 {
111 Console.WriteLine(string.Format("{0}:{1}", Id, num));
112 }
113 }
114 }
115
2 using System.Collections.Generic;
3 using System.Linq;
4 namespace ConsoleApp
5 {
6 public class Program
7 {
8 static void Main(string[] args)
9 {
10 Game game = new Game(17);
11 //设定策略
12 game.Strategy = new StrategyA();
13 game.GameOver += new EventHandler(game_GameOver);
14 game.Start();
15 Console.Read();
16 }
17 static void game_GameOver(object sender, EventArgs e)
18 {
19 Console.WriteLine("Last Person:" + sender);
20 }
21 }
22 /// <summary>
23 /// 策略接口
24 /// </summary>
25 public interface IStrategy
26 {
27 /// <summary>
28 /// 运行策略
29 /// </summary>
30 /// <param name="g"></param>
31 void Work(Game g);
32 }
33 /// <summary>
34 /// 策略A
35 /// 从第一个人开始报数,报到3的倍数退出,一直到剩下最后一个人,用面向对象的思想去做这道题。
36 /// </summary>
37 public class StrategyA : IStrategy
38 {
39 private List<Person> over = new List<Person>();
40 private int k = 0;
41 public void Work(Game game)
42 {
43 foreach (Person p in game.Players)
44 {
45 p.Say(++k);
46 if (k % 3 == 0)
47 over.Add(p);
48 }
49 game.Players.RemoveAll(o => over.Contains(o));
50 }
51 }
52 public delegate void EventHandler(object sender , EventArgs e);
53 /// <summary>
54 /// 游戏
55 /// </summary>
56 public class Game
57 {
58 public Game(int num)
59 {
60 Players = new List<Person>();
61 for (int i = 0; i < num; i++) {
62 Players.Add(new Person(i + 1));
63 }
64 }
65 /// <summary>
66 /// 游戏策略
67 /// </summary>
68 public IStrategy Strategy { get; set; }
69 /// <summary>
70 /// 游戏玩家
71 /// </summary>
72 public List<Person> Players { get; set; }
73 /// <summary>
74 /// 游戏结束事件
75 /// </summary>
76 public event EventHandler GameOver;
77 /// <summary>
78 /// 开始游戏
79 /// </summary>
80 public void Start()
81 {
82 if (Strategy != null)
83 {
84 while (Players.Count > 1)
85 {
86 Strategy.Work(this);
87 }
88 GameOver(this.Players.First().Id, new EventArgs());
89 }
90 }
91 }
92 /// <summary>
93 /// 玩家
94 /// </summary>
95 public class Person
96 {
97 public Person(int id)
98 {
99 this.Id = id;
100 }
101 /// <summary>
102 /// 玩家ID
103 /// </summary>
104 public int Id { get; set; }
105 /// <summary>
106 /// 玩家报数
107 /// </summary>
108 /// <param name="num"></param>
109 public void Say(int num)
110 {
111 Console.WriteLine(string.Format("{0}:{1}", Id, num));
112 }
113 }
114 }
115
StrategyA 实现接口 IStrategy 遵循开闭原则,如果我们要换一个规则只要添加一个类实现IStrategy即可。
之前还有用 循环链表 来完成这道题,晚上再发上来。