-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strategy.cs
112 lines (97 loc) · 1.98 KB
/
Strategy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
namespace DesignPatterns
{
public abstract class Duck
{
protected IQuack Quack { get; set; }
protected IFly Fly { get; set; }
public void Quacking()
{
Quack.Quack();
}
public void Flying()
{
Fly.Fly();
}
public void ChangeFly(IFly fly)
{
Fly = fly;
}
public void ChangeQuack(IQuack quack)
{
Quack = quack;
}
}
public class RedHeadDuck : Duck
{
public RedHeadDuck()
{
Quack = new Quacking();
Fly = new FlyWithWings();
}
}
public class WoodenDuck : Duck
{
public WoodenDuck()
{
Quack = new Silent();
Fly = new FlyNoWay();
}
}
public interface IQuack
{
public void Quack();
}
public class Quacking : IQuack
{
public void Quack()
{
Console.WriteLine("Quack");
}
}
public class Squeak : IQuack
{
public void Quack()
{
Console.WriteLine("Squeak");
}
}
public class Silent : IQuack
{
public void Quack()
{
Console.WriteLine("Silent");
}
}
public interface IFly
{
public void Fly();
}
public class FlyWithWings : IFly
{
public void Fly()
{
Console.WriteLine("Fly");
}
}
public class FlyNoWay : IFly
{
public void Fly()
{
Console.WriteLine("No Fly");
}
}
public static class Strategy
{
public static bool Run()
{
var woodenDuck = new WoodenDuck();
var redHeadDuck = new RedHeadDuck();
woodenDuck.Flying();
woodenDuck.ChangeFly(new FlyWithWings());
woodenDuck.Flying();
redHeadDuck.Quacking();
return true;
}
}
}