题目:17个人围成一圈,从第一个人开始报数,报到3的退出,一直到剩下最后一个人,用面向对象的思想去做这道题。
前面两篇感觉上还是不够面向对象,至少要有面向对象三大特征:封装,继承,多态吧,第一篇仅是构建了一个双向循环链表数据结构,思想还是面向过程的。
第二篇用到了泛型委托,但是思想和特定技术是不相关的,用到泛型委托未必就是面向对象啊。
所以我也斗胆写了下这道题,请大家指教了:
首先说明,这里用到了状态模式,单例模式,主要还是状态模式,实现了一个简单的状态机,
先上图:
再上代码:
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Test
6 {
7 interface IPersonState
8 {
9 void Handle();
10
11 void Handle(Person person);
12 }
13 }
14
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Test
6 {
7 interface IPersonState
8 {
9 void Handle();
10
11 void Handle(Person person);
12 }
13 }
14
First
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class First:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23 if (person.Next != person)
24 {
25 person.Next.State = new Second();
26 PersonLink.Instance.RootPerson = person.Next;
27 person.Next.Call();
28 }
29 else
30 {
31 person.Next.State = new Last();
32 person.Next.Call();
33 }
34 }
35
36 #endregion
37 }
38}
39
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class First:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23 if (person.Next != person)
24 {
25 person.Next.State = new Second();
26 PersonLink.Instance.RootPerson = person.Next;
27 person.Next.Call();
28 }
29 else
30 {
31 person.Next.State = new Last();
32 person.Next.Call();
33 }
34 }
35
36 #endregion
37 }
38}
39
Second
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Second:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23 if (person.Next != person)
24 {
25 person.Next.State = new Three();
26 PersonLink.Instance.RootPerson = person.Next;
27 person.Next.Call();
28 }
29 else
30 {
31 person.Next.State = new Last();
32 person.Next.Call();
33 }
34 }
35
36 #endregion
37 }
38}
39
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Second:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23 if (person.Next != person)
24 {
25 person.Next.State = new Three();
26 PersonLink.Instance.RootPerson = person.Next;
27 person.Next.Call();
28 }
29 else
30 {
31 person.Next.State = new Last();
32 person.Next.Call();
33 }
34 }
35
36 #endregion
37 }
38}
39
Three
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Three:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23
24 Console.WriteLine(person.Val);
25 person.Prev.Next = person.Next;
26 person.Next.Prev = person.Prev;
27 PersonLink.Instance.RootPerson = person.Next;
28 if (PersonLink.Instance.RootPerson.Next == PersonLink.Instance.RootPerson)
29 {
30 person.Next.State = new Last();
31 }
32 else
33 {
34 person.Next.State = new First();
35 }
36 person.Next.Call();
37 }
38 #endregion
39
40
41 }
42}
43
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Three:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23
24 Console.WriteLine(person.Val);
25 person.Prev.Next = person.Next;
26 person.Next.Prev = person.Prev;
27 PersonLink.Instance.RootPerson = person.Next;
28 if (PersonLink.Instance.RootPerson.Next == PersonLink.Instance.RootPerson)
29 {
30 person.Next.State = new Last();
31 }
32 else
33 {
34 person.Next.State = new First();
35 }
36 person.Next.Call();
37 }
38 #endregion
39
40
41 }
42}
43
Last
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading;
5
6namespace Test
7{
8 class Last:IPersonState
9 {
10 IPersonState 成员#region IPersonState 成员
11
12 public void Handle()
13 {
14 throw new Exception("The method or operation is not implemented.");
15 }
16
17 public void Handle(Person person)
18 {
19 Console.WriteLine("The last "+person.Val.ToString());
20 Thread.Sleep(20000);
21 }
22
23 #endregion
24 }
25}
26
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading;
5
6namespace Test
7{
8 class Last:IPersonState
9 {
10 IPersonState 成员#region IPersonState 成员
11
12 public void Handle()
13 {
14 throw new Exception("The method or operation is not implemented.");
15 }
16
17 public void Handle(Person person)
18 {
19 Console.WriteLine("The last "+person.Val.ToString());
20 Thread.Sleep(20000);
21 }
22
23 #endregion
24 }
25}
26
Person
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Person
8 {
9 private IPersonState _state = null;
10
11 public IPersonState State
12 {
13 get
14 {
15 return _state;
16 }
17 set
18 {
19 this._state = value;
20 }
21 }
22 private Person _pre=null;
23 private Person _next=null;
24 private int _val = 1;
25
26 public Person Prev
27 {
28 get{ return _pre; }
29 set{ _pre = value;}
30 }
31
32 public Person Next {
33 get { return _next; }
34 set { _next = value; }
35 }
36 public int Val {
37 get { return _val; }
38 set { _val = value; }
39 }
40
41 public Person() { }
42 public Person( Person pre,Person next, int val)
43 {
44 Next = next;
45 Val = val;
46 Prev = pre;
47 }
48 public void Call()
49 {
50 this.State.Handle(this);
51 }
52 }
53}
54
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Person
8 {
9 private IPersonState _state = null;
10
11 public IPersonState State
12 {
13 get
14 {
15 return _state;
16 }
17 set
18 {
19 this._state = value;
20 }
21 }
22 private Person _pre=null;
23 private Person _next=null;
24 private int _val = 1;
25
26 public Person Prev
27 {
28 get{ return _pre; }
29 set{ _pre = value;}
30 }
31
32 public Person Next {
33 get { return _next; }
34 set { _next = value; }
35 }
36 public int Val {
37 get { return _val; }
38 set { _val = value; }
39 }
40
41 public Person() { }
42 public Person( Person pre,Person next, int val)
43 {
44 Next = next;
45 Val = val;
46 Prev = pre;
47 }
48 public void Call()
49 {
50 this.State.Handle(this);
51 }
52 }
53}
54
PersonLink
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class PersonLink
8 {
9 private int count = 0;
10 private Person root = null;
11 public Person RootPerson
12 {
13 get
14 {
15 if (root == null)
16 {
17 root = new Person(null,null, 1);
18 }
19 return root;
20 }
21 set
22 {
23 root = value;
24 }
25 }
26
27 public static PersonLink Instance
28 {
29 get
30 {
31 if (_instance == null)
32 {
33 _instance = new PersonLink();
34 }
35 return _instance;
36 }
37 }
38 private static PersonLink _instance = null;
39
40 public PersonLink()
41 {
42
43 }
44
45 public void InitLink(int count)
46 {
47 this.count = count;
48 //初始化数据
49 Person temp = this.RootPerson;
50 for (int i = 2; i <= count; i++)
51 {
52 Person p = new Person(temp,null, i);
53 temp.Next = p;
54 temp = p;
55 }
56 temp.Next =this.RootPerson;
57 RootPerson.Prev = temp;
58 }
59
60
61 internal void Start()
62 {
63 this.RootPerson.State = new First();
64 this.RootPerson.Call();
65 }
66
67 }
68}
69
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class PersonLink
8 {
9 private int count = 0;
10 private Person root = null;
11 public Person RootPerson
12 {
13 get
14 {
15 if (root == null)
16 {
17 root = new Person(null,null, 1);
18 }
19 return root;
20 }
21 set
22 {
23 root = value;
24 }
25 }
26
27 public static PersonLink Instance
28 {
29 get
30 {
31 if (_instance == null)
32 {
33 _instance = new PersonLink();
34 }
35 return _instance;
36 }
37 }
38 private static PersonLink _instance = null;
39
40 public PersonLink()
41 {
42
43 }
44
45 public void InitLink(int count)
46 {
47 this.count = count;
48 //初始化数据
49 Person temp = this.RootPerson;
50 for (int i = 2; i <= count; i++)
51 {
52 Person p = new Person(temp,null, i);
53 temp.Next = p;
54 temp = p;
55 }
56 temp.Next =this.RootPerson;
57 RootPerson.Prev = temp;
58 }
59
60
61 internal void Start()
62 {
63 this.RootPerson.State = new First();
64 this.RootPerson.Call();
65 }
66
67 }
68}
69
调用:
Program
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 PersonLink.Instance.InitLink(17);
12 PersonLink.Instance.Start();
13 }
14 }
15}
16
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 PersonLink.Instance.InitLink(17);
12 PersonLink.Instance.Start();
13 }
14 }
15}
16
运行结果:
源代码:/Files/hongyin163/Test.rar
不知道大家感觉怎样?