Imagine a situation when classes differ only in their behavior. The algorithms implementing the behavior can be kept in separate classes in order for an appropriate behavior to be selected at runtime. The Strategy pattern defines an interface common to all algorithms.
What's behavior here could be something else: algorithm, approach, strategy, operation, etc.—why this is called the Strategy pattern. It could be several methods and not, just as here, a move() method.
1.
public
interface
Behavior
2.
{
3.
int
moveCommand();
4.
}
1.
public
class
AggressiveBehavior
implements
Behavior
2.
{
3.
public
int
moveCommand()
4.
{
5.
System.out.print(
" [Aggressive Behavior] if another robot found, attack him."
);
6.
return
1
;
7.
}
8.
}
1.
public
class
DefensiveBehavior
implements
Behavior
2.
{
3.
public
int
moveCommand()
4.
{
5.
System.out.print(
" [Defensive Behavior] if another robot found, run away."
);
6.
return
-
1
;
7.
}
8.
}
1.
public
class
NormalBehavior
implements
Behavior
2.
{
3.
public
int
moveCommand()
4.
{
5.
System.out.print(
" [Normal Behavior] if another robot found, ignore him."
);
6.
return
0
;
7.
}
8.
}
Robot is the object that can exhibit any one of the defined behaviors:
01.
public
class
Robot
02.
{
03.
private
Behavior behavior;
04.
private
String name;
05.
06.
public
Robot( String name )
07.
{
08.
this
.name = name;
09.
}
10.
11.
public
void
move()
12.
{
13.
int
command = behavior.moveCommand();
14.
}
15.
16.
public
Behavior getBehavior() {
return
behavior; }
17.
public
String getName() {
return
name; }
18.
19.
public
void
setBehavior( Behavior behavior ) {
this
.behavior = behavior; }
20.
public
void
setName ( String name ) {
this
.name = name; }
21.
}
01.
public
class
Main
02.
{
03.
public
static
void
main( String[] args )
04.
{
05.
System.out.println(
"S t r a t e g y P a t t e r n"
);
06.
System.out.println(
"==============================="
);
07.
System.out.println();
08.
09.
System.out.println(
"Instantiate robots by name and establish their behavior:"
);
10.
System.out.println(
"---------------------------------------------------------"
);
11.
Robot big =
new
Robot(
"Big Robot"
); big.setBehavior(
new
AggressiveBehavior() );
12.
Robot george =
new
Robot(
"George v.2.1"
); george.setBehavior(
new
DefensiveBehavior() );
13.
Robot r2d2 =
new
Robot(
"R2D2"
); r2d2.setBehavior(
new
NormalBehavior() );
14.
15.
System.out.println(
" "
+ big.getName() +
" ("
+ big.getBehavior().getClass().getSimpleName() +
")"
);
16.
System.out.println(
" "
+ george.getName() +
" ("
+ george.getBehavior().getClass().getSimpleName() +
")"
);
17.
System.out.println(
" "
+ r2d2.getName() +
" ("
+ r2d2.getBehavior().getClass().getSimpleName() +
")"
);
18.
19.
System.out.println();
20.
21.
System.out.println(
"Move the robots to see their behavior. Based on current position a robot"
);
22.
System.out.println(
"decides his next move. The result returned by the behavior object (robot)"
);
23.
System.out.println(
"is sent to his movement mechanism:"
);
24.
System.out.println(
"-------------------------------------------------------------------------"
);
25.
big.move(); System.out.println(
" ("
+ big.getName() +
")"
);
26.
george.move(); System.out.println(
" ("
+ george.getName() +
")"
);
27.
r2d2.move(); System.out.println(
" ("
+ r2d2.getName() +
")"
);
28.
29.
System.out.println();
30.
System.out.println(
"The three objects (robots) move with these behaviors:"
);
31.
System.out.println(
"-----------------------------------------------------"
);
32.
System.out.println(
" "
+ big.getName() +
" is frightened."
);
33.
System.out.println(
" "
+ george.getName() +
" becomes angry because attacked by other robots."
);
34.
System.out.println(
" "
+ r2d2.getName() +
" keeps his calm."
);
35.
System.out.println();
36.
37.
System.out.println(
"Now switch two of the robots' behavior:"
);
38.
System.out.println(
"---------------------------------------"
);
39.
big.setBehavior(
new
DefensiveBehavior() );
40.
System.out.println(
" Changed "
+ big.getName() +
"'s behavior to "
+ big.getBehavior().getClass().getSimpleName() +
"."
);
41.
42.
george.setBehavior(
new
AggressiveBehavior() );
43.
System.out.println(
" Changed "
+ george.getName() +
"'s behavior to "
+ george.getBehavior().getClass().getSimpleName() +
"."
);
44.
System.out.println();
45.
46.
System.out.println(
"Move the robots again to see the difference:"
);
47.
System.out.println(
"--------------------------------------------"
);
48.
big.move(); System.out.println(
" ("
+ big.getName() +
")"
);
49.
george.move(); System.out.println(
" ("
+ george.getName() +
")"
);
50.
r2d2.move(); System.out.println(
" ("
+ r2d2.getName() +
")"
);
51.
}
52.
}
Output:
S t r a t e g y P a t t e r n =============================== Instantiate robots by name and establish their behavior: --------------------------------------------------------- Big Robot (AggressiveBehavior) George v.2.1 (DefensiveBehavior) R2D2 (NormalBehavior) Move the robots to see their behavior. Based on current position a robot decides his next move. The result returned by the behavior object (robot) is sent to his movement mechanism: ------------------------------------------------------------------------- [Aggressive Behavior] if another robot found, attack him. (Big Robot) [Defensive Behavior] if another robot found, run away. (George v.2.1) [Normal Behavior] if another robot found, ignore him. (R2D2) The three objects (robots) move with these behaviors: ----------------------------------------------------- Big Robot is frightened. George v.2.1 becomes angry because attacked by other robots. R2D2 keeps his calm. Now switch two of change the robots' behavior: ---------------------------------------------- Changed Big Robot's behavior to DefensiveBehavior. Changed George v.2.1's behavior to AggressiveBehavior. Move the robots again to see the difference: -------------------------------------------- [Defensive Behavior] if another robot found, run away. (Big Robot) [Aggressive Behavior] if another robot found, attack him. (George v.2.1) [Normal Behavior] if another robot found, ignore him. (R2D2)