A strategy is a set of actions to achieve a given target. We say that we develop action strategies, which is basically an action plan.
In programming, a pattern of strategy helps in achieving given target by dividing a given issue into smaller and less complicated ones, which in turn helps to solve them. Consider this with the example below. We have declared two interfaces.
Shooting
public interface Shooting {
public void shot();
}
and FightWithMeleeWeapons
public interface FightWithMeleeWeapons {
public void blow();
}
As can be seen, they relate to the use of certain types of weapons. The Shooting Interface is implemented by two classes:
PistolShot
public class PistolShot implements Shooting{
@Override
public void shot() {
System.out.println("Pistol Shot");
}
}
CarabineShot
public class CarabineShot implements Shooting{
@Override
public void shot() {
System.out.println("Carabine Shot");
}
}
In the same way we create Knife and Katana classes
public class Knife implements FightWithMeleeWeapons{
@Override
public void blow() {
System.out.println("cios nożem");
}
}
public class Katana implements WalkaBroniaBiala{
@Override
public void cios() {
System.out.println("Cios kataną");
}
}
It allows you to simulate the operation of two types of weapons. However, we still ask ourselves: How to use them? Well, the best solution is to create a control class that will be the owner of these classes. In my short project it will be the Gangster abstract class
public abstract class Gangster {
Shooting shooting;
FightWithMeleeWeapons fightWithMeleeWeapons;
public void running(){
System.out.println("run");
}
public void swim(){
System.out.println("swim");
}
public void setShooting(Shooting shooting){
this.shooting=shooting;
}
public void setFightWithMeleeWeapons(FightWithMeleeWeapons fightWithMeleeWeapons){
this.fightWithMeleeWeapons=fightWithMeleeWeapons;
}
public void shot(){
shooting.shot();
}
public void walkaBroniaBiala(){
fightWithMeleeWeapons.blow();
}
}
However, as we well know, an abstract class cannot have objects, therefore it is necessary to create classes inheriting from it. In my project these are the MichaelCorleone and VitoCorleone classes.
public class MichaelCorleone extends Gangster{
public MichaelCorleone(){
shooting= new PistolShot();
fightWithMeleeWeapons= new Knife();
}
}
public class VitoCorleone extends Gangster{
public VitoCorleone(){
shooting=new CarabineShot();
fightWithMeleeWeapons= new Katana();
}
}
These classes only have a constructor that creates class objects and assigns them to the variables inherited from the gangster class. Finally, let's test the program
public class TestGangster {
public static void main(String[] args){
Gangster Michael= new MichaelCorleone();
System.out.println("Michael Corleone");
Michael.shot();
Michael.setShooting(new StrzalZKarabinu());
Michael.shot();
Michael.fightWithMeleeWeapons();
Gangster Vito= new VitoCorleone();
System.out.println("Vito Corleone");
Vito.shot();
Vito.setShooting(new StrzalZPistoletu());
Vito.shot();
Vito.fightWithMeleeWeapons();
}
}
First, we create class variables MichaelCorleone and VitoCorleone. This allows you to take shots. It is also possible to change weapons using the Shooting method. The result of the operation is as follows.
Michael Corleone
Pistol Shot
Carabine Shot
Knife blow
Vito Corleone
Carabine Shot
Pistol Shot
Katana blow
As can be seen, the application strategy for the project has taken effect. Objects assigned to the Gangster class de facto belong to it and their users can decide whether they choose this or that implementation of the interface, just as the Gangster can choose this or that weapon.