Strategy
Strategy pattern is also called “policy”.
Suppose one is speeding when driving, a police would possibly stop him. It’s possible that the police is easy, who will let him go without any ticket. Also it’s possible he will meet a hard officer, and get a ticket. He wouldn’t know until he actually gets caught, that is, run-time.
Here is a simple example about Strategy design pattern.
Define a interface Strategy, which has one method processSpeeding()
Define a interface Strategy, which has one method processSpeeding()
package com.programcreek.designpatterns.strategy;
public interface Strategy {
//defind a method for police to process speeding case.
public void processSpeeding(int speed);
}
Now we have two kinds of police officers.
package com.programcreek.designpatterns.strategy;
public class EasyPolice implements Strategy{
@Override
public void processSpeeding(int speed) {
System.out.println("This is your first time, be sure don't do it again!");
}
}
package com.programcreek.designpatterns.strategy;
public class HardPolice implements Strategy{
@Override
public void processSpeeding(int speed) {
System.out.println("Your speed is "+ speed+ ", and should get a ticket!");
}
}
Define a situation in which a police officer will be involved to process speeding.
package com.programcreek.designpatterns.strategy;
public class Situation {
private Strategy strategy;
public Situation(Strategy strategy){
this.strategy = strategy;
}
public void handleByPolice(int speed){
this.strategy.processSpeeding(speed);
}
}
Finally, try the result.
public class Main {
public static void main(String args[]){
HardPolice hp = new HardPolice();
EasyPolice ep = new EasyPolice();
// In situation 1, a hard officer is met
// In situation 2, a easy officer is met
Situation s1 = new Situation(hp);
Situation s2 = new Situation(ep);
//the result based on the kind of police officer.
s1.handleByPolice(10);
s2.handleByPolice(10);
}
}
Output is:
Your speed is 10, and should get a ticket!This is your first time, be sure don’t do it again!
Suppose one is speeding when driving, a police would possibly stop him. It’s possible that the police is easy, who will let him go without any ticket. Also it’s possible he will meet a hard officer, and get a ticket. He wouldn’t know until he actually gets caught, that is, run-time.
Here is a simple example about Strategy design pattern.
Define a interface Strategy, which has one method processSpeeding()
Define a interface Strategy, which has one method processSpeeding()
package com.programcreek.designpatterns.strategy;
public interface Strategy {
//defind a method for police to process speeding case.
public void processSpeeding(int speed);
}
Now we have two kinds of police officers.
package com.programcreek.designpatterns.strategy;
public class EasyPolice implements Strategy{
@Override
public void processSpeeding(int speed) {
System.out.println("This is your first time, be sure don't do it again!");
}
}
package com.programcreek.designpatterns.strategy;
public class HardPolice implements Strategy{
@Override
public void processSpeeding(int speed) {
System.out.println("Your speed is "+ speed+ ", and should get a ticket!");
}
}
Define a situation in which a police officer will be involved to process speeding.
package com.programcreek.designpatterns.strategy;
public class Situation {
private Strategy strategy;
public Situation(Strategy strategy){
this.strategy = strategy;
}
public void handleByPolice(int speed){
this.strategy.processSpeeding(speed);
}
}
Finally, try the result.
public class Main {
public static void main(String args[]){
HardPolice hp = new HardPolice();
EasyPolice ep = new EasyPolice();
// In situation 1, a hard officer is met
// In situation 2, a easy officer is met
Situation s1 = new Situation(hp);
Situation s2 = new Situation(ep);
//the result based on the kind of police officer.
s1.handleByPolice(10);
s2.handleByPolice(10);
}
}
Output is:
Your speed is 10, and should get a ticket!This is your first time, be sure don’t do it again!