-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategyExample02.java
More file actions
158 lines (136 loc) · 4.02 KB
/
StrategyExample02.java
File metadata and controls
158 lines (136 loc) · 4.02 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
context : code that will remain unchanged
behaviour/strategy : code that will change
Suppose we are making a game where a person can kick, punch, jump and roll. But he can kick and jump in different style.
That means jump and kick can be changed in different situation. So this is our strategy because these can be changed.
We can keep evrery kind of jump and kick method in Fighter class. But this is a bad design because there can be many cases
where a person don't have to perform front kick or any other kick or any jump style from the given jump style, but he is extending
all the methods if we keep these methods in Figher class.
Again we can use class for every single jump and kick style. But this won't give us any facility as a person can kick and jump in
multiple style and person class can't extend more than one class accrording to OOP. So we have to use interface to solve this problem.
To understand the solution see the below code.
Note that if we want, we can avoid multiple classes for different kick and jump style in the below code and we can keep the method of
different style in kick interface and jump interface and the interfaces can be implemented by Person. But this is a bad design also.
If we do this, code duplicity will happen. Suppose person01 and person02 both have only front kick style and long jump style.Then we
have to implement the same method that means we have to write the same code for the two classes.
*/
interface KickBehavior
{
public void kick();
}
class FrontKick implements KickBehavior
{
public void kick()
{
System.out.println("Front Kick");
}
}
class SideKick implements KickBehavior
{
public void kick()
{
System.out.println("Side Kick");
}
}
class BackKick implements KickBehavior
{
public void kick()
{
System.out.println("Back Kick");
}
}
interface JumpBehavior
{
public void jump();
}
class ShortJump implements JumpBehavior
{
public void jump()
{
System.out.println("Short Jump");
}
}
class LongJump implements JumpBehavior
{
public void jump()
{
System.out.println("Long Jump");
}
}
abstract class Fighter
{
KickBehavior kickBehavior;
JumpBehavior jumpBehavior;
public Fighter(KickBehavior kickBehavior,
JumpBehavior jumpBehavior)
{
this.jumpBehavior = jumpBehavior;
this.kickBehavior = kickBehavior;
}
public void punch()
{
System.out.println("Default Punch");
}
public void kick()
{
kickBehavior.kick();
}
public void jump()
{
jumpBehavior.jump();
}
public void roll()
{
System.out.println("Default Roll");
}
public void setKickBehavior(KickBehavior kickBehavior)
{
this.kickBehavior = kickBehavior;
}
public void setJumpBehavior(JumpBehavior jumpBehavior)
{
this.jumpBehavior = jumpBehavior;
}
public abstract void display();
}
class Person01 extends Fighter
{
public Person01(KickBehavior kickBehavior,
JumpBehavior jumpBehavior)
{
super(kickBehavior,jumpBehavior);
}
public void display()
{
System.out.println("Person01");
}
}
class Person02 extends Fighter
{
public Person02(KickBehavior kickBehavior,
JumpBehavior jumpBehavior)
{
super(kickBehavior,jumpBehavior);
}
public void display()
{
System.out.println("Person02");
}
}
public class StrategyExample02 {
public static void main(String[] args) {
JumpBehavior shortJump = new ShortJump();
JumpBehavior LongJump = new LongJump();
KickBehavior FrontKick = new FrontKick();
KickBehavior BackKick = new BackKick();
Fighter Person01 = new Person01(FrontKick,shortJump);
Person01.display();
Person01.punch();
Person01.kick();
Person01.jump();
Person01.setJumpBehavior(LongJump);
Person01.setKickBehavior(BackKick);
Person01.jump();
Person01.kick();
}
}