Skip to content

Commit 98779f3

Browse files
committed
策略模式
1 parent 2e4fc1f commit 98779f3

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

DesignPatterns/StrategyPattern/StrategyPattern/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ class Program
1010
{
1111
static void Main(string[] args)
1212
{
13+
Context context;
14+
context = new Context(new ConcreteStrategyA());
15+
context.ContextOperation();
16+
17+
context= new Context(new ConcreteStrategyB());
18+
context.ContextOperation();
19+
20+
context= new Context(new ConcreteStrategyC());
21+
context.ContextOperation();
22+
1323
}
1424
}
1525
}

DesignPatterns/StrategyPattern/StrategyPattern/Strategy.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,29 @@ public void ContextOperation()
3131
this.strategy.Algorithm();
3232
}
3333
}
34+
35+
36+
class ConcreteStrategyA : Strategy
37+
{
38+
public override void Algorithm()
39+
{
40+
Console.WriteLine("算法A");
41+
}
42+
}
43+
44+
class ConcreteStrategyB : Strategy
45+
{
46+
public override void Algorithm()
47+
{
48+
Console.WriteLine("算法B");
49+
}
50+
}
51+
52+
class ConcreteStrategyC : Strategy
53+
{
54+
public override void Algorithm()
55+
{
56+
Console.WriteLine("算法C");
57+
}
58+
}
3459
}

0 commit comments

Comments
 (0)