Skip to content

Commit ee09e63

Browse files
committed
update strategy pattern example
1 parent c1cd7c4 commit ee09e63

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

Assets/Behavioral Patterns/Strategy Pattern/Example2.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class Animal_BadExample
5+
{
6+
// bad code:
7+
public virtual void Fly()
8+
{
9+
Debug.Log ("Can Fly");
10+
}
11+
}
12+
13+
public class Dog_BadExample : Animal_BadExample
14+
{
15+
// bad code:
16+
public override void Fly()
17+
{
18+
// override
19+
}
20+
}
21+
22+
public class Cat_BadExample : Animal_BadExample
23+
{
24+
// bad code:
25+
public override void Fly()
26+
{
27+
// override
28+
}
29+
}
30+
31+
public class Bird_BadExample : Animal_BadExample
32+
{
33+
public override void Fly()
34+
{
35+
}
36+
}
37+
38+
// Rembember:
39+
// ALWAYS
40+
// eliminate duplicate code
41+
// eliminate technices that allow one class to affect others

Assets/Behavioral Patterns/Strategy Pattern/Example2/BadCodeExample.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//-------------------------------------------------------------------------------------
2+
// StrategyPatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
namespace StrategyPatternExample2
9+
{
10+
public class StrategyPatternExample2 : MonoBehaviour
11+
{
12+
void Start()
13+
{
14+
// create dog and bird example objects
15+
Animal sparky = new Dog();
16+
Animal tweety = new Bird();
17+
18+
Debug.Log("Dog: " + sparky.TryToFly());
19+
Debug.Log("Bird: " + tweety.TryToFly());
20+
21+
// change behaviour of dog
22+
sparky.SetFlyingAbility(new ItFlys());
23+
24+
Debug.Log("Dog: " + sparky.TryToFly());
25+
Debug.Log("Bird: " + tweety.TryToFly());
26+
}
27+
}
28+
29+
30+
// Using Interfaces for decoupling
31+
// putting behaviour that varies in different classes
32+
public interface IFly
33+
{
34+
string Fly();
35+
}
36+
37+
// Class that holds behaviour for objects that can fly
38+
class ItFlys : IFly
39+
{
40+
public string Fly()
41+
{
42+
return "Flying high";
43+
}
44+
}
45+
46+
// Class that holds behaviour for objects that can not fly
47+
class CantFly : IFly
48+
{
49+
public string Fly()
50+
{
51+
return "I can't fly";
52+
}
53+
}
54+
55+
class FlyingSuperFast : IFly
56+
{
57+
public string Fly()
58+
{
59+
return "Fly super fast";
60+
}
61+
}
62+
63+
64+
// Classes that hold an instance of the behaviours above:
65+
public class Animal
66+
{
67+
// hereby adding the behaviour
68+
// it also can change that way
69+
public IFly flyingType;
70+
71+
public string TryToFly()
72+
{
73+
return flyingType.Fly();
74+
}
75+
76+
public void SetFlyingAbility(IFly newFlyingType)
77+
{
78+
this.flyingType = newFlyingType;
79+
}
80+
}
81+
82+
// derived objects that vary from the base Animal:
83+
public class Dog : Animal
84+
{
85+
public Dog()
86+
{
87+
flyingType = new CantFly();
88+
}
89+
}
90+
91+
public class Bird : Animal
92+
{
93+
public Bird()
94+
{
95+
flyingType = new ItFlys();
96+
}
97+
}
98+
}
99+
100+
// Rembember:
101+
// ALWAYS
102+
// eliminate duplicate code
103+
// eliminate technices that allow one class to affect others

Assets/Behavioral Patterns/Strategy Pattern/Example2/StrategyPatternExample2.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Behavioral Patterns/Strategy Pattern/Example2/StrategyPatternExample2.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)