Skip to content

Commit e8688bd

Browse files
committed
Merge branch 'master' of github.com:dynamicy/DesignPatternStudy
2 parents 208e2b1 + 924f193 commit e8688bd

File tree

12 files changed

+293
-0
lines changed

12 files changed

+293
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CommandPattern
6+
{
7+
public class AttackCommand : Command
8+
{
9+
public AttackCommand(ReceiveRole pRole)
10+
: base(pRole)
11+
{
12+
}
13+
14+
public override void Execute()
15+
{
16+
mReceiveRole.Attack();
17+
}
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CommandPattern
6+
{
7+
public abstract class Command
8+
{
9+
protected ReceiveRole mReceiveRole;
10+
11+
public Command(ReceiveRole pRole)
12+
{
13+
mReceiveRole = pRole;
14+
}
15+
16+
public abstract void Execute();
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CommandPattern
6+
{
7+
public class Invoker
8+
{
9+
private List<Command> mCommands = new List<Command>();
10+
11+
public void SetCommand(Command pCommand)
12+
{
13+
mCommands.Add(pCommand);
14+
}
15+
16+
public void CancelCommand(Command pCommand)
17+
{
18+
mCommands.Remove(pCommand);
19+
}
20+
21+
public void NotifyExecute ()
22+
{
23+
foreach (Command tCommand in mCommands)
24+
{
25+
tCommand.Execute();
26+
}
27+
}
28+
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CommandPattern
6+
{
7+
public class MoveForwardCommand : Command
8+
{
9+
public MoveForwardCommand(ReceiveRole pRole)
10+
: base(pRole)
11+
{
12+
}
13+
14+
public override void Execute()
15+
{
16+
mReceiveRole.Moveward();
17+
}
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CommandPattern
6+
{
7+
public class ReceiveRole
8+
{
9+
public void Moveward()
10+
{
11+
Console.WriteLine("Move forward");
12+
}
13+
14+
public void TurnLeft()
15+
{
16+
Console.WriteLine("Turn left");
17+
}
18+
19+
public void TurnRight()
20+
{
21+
Console.WriteLine("Turn right");
22+
}
23+
24+
public void Attack()
25+
{
26+
Console.WriteLine("Attack");
27+
}
28+
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CommandPattern
6+
{
7+
public class TurnLeftCommand : Command
8+
{
9+
public TurnLeftCommand(ReceiveRole pRole)
10+
: base(pRole)
11+
{
12+
}
13+
14+
public override void Execute()
15+
{
16+
mReceiveRole.TurnLeft();
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CommandPattern
6+
{
7+
public class TurnRightCommand : Command
8+
{
9+
public TurnRightCommand(ReceiveRole pRole)
10+
: base(pRole)
11+
{
12+
}
13+
14+
public override void Execute()
15+
{
16+
mReceiveRole.TurnRight();
17+
}
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace IteratorPattern
6+
{
7+
public abstract class Aggregate
8+
{
9+
public abstract Iterator CreateIterator();
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Collections;
5+
6+
namespace IteratorPattern
7+
{
8+
public class ConcreteAggregate : Aggregate
9+
{
10+
private ArrayList mItems = new ArrayList();
11+
12+
public override Iterator CreateIterator()
13+
{
14+
return new ConcreteIterator(this);
15+
}
16+
17+
public int Count
18+
{
19+
get { return mItems.Count; }
20+
}
21+
22+
public object this[int index]
23+
{
24+
get { return mItems[index]; }
25+
set { mItems.Insert(index, value); }
26+
}
27+
}
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace IteratorPattern
6+
{
7+
public class ConcreteIterator : Iterator
8+
{
9+
private ConcreteAggregate mAggregate;
10+
private int mCurrent = 0;
11+
12+
public ConcreteIterator(ConcreteAggregate pAggregate)
13+
{
14+
this.mAggregate = pAggregate;
15+
}
16+
17+
public override object First()
18+
{
19+
return mAggregate[ConvertIndex(0)];
20+
}
21+
22+
public override object Next()
23+
{
24+
object tResult = null;
25+
if (mCurrent < mAggregate.Count - 1)
26+
{
27+
tResult = mAggregate[ConvertIndex(++mCurrent)];
28+
}
29+
30+
return tResult;
31+
}
32+
33+
public override object CurrentItem()
34+
{
35+
return mAggregate[ConvertIndex(mCurrent)];
36+
}
37+
38+
public override bool IsDone()
39+
{
40+
return mCurrent >= mAggregate.Count;
41+
}
42+
43+
private int ConvertIndex(int pIndex)
44+
{
45+
int nNewIndex = 0;
46+
if (0 == pIndex % 2)
47+
{
48+
nNewIndex = pIndex / 2;
49+
}
50+
else
51+
{
52+
nNewIndex = (pIndex - 1) / 2 + ((0 == mAggregate.Count % 2) ? mAggregate.Count / 2 : (mAggregate.Count - 1 / 2));
53+
}
54+
55+
return nNewIndex;
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)