Design pattern "Strategy"

    Hello everyone! Today I would like to tell you about design pattern strategy. In my opinion, this pattern is very interesting and usefull in day-to-day tasks!

    Strategy is behavioral pattern. It means, that this pattern determinate algorythms and ways to realization of interaction different objects and classes. You can use this pattern, when the behavior of your program depends on the situation. For example, you have several ways to implement some feature and your choice depends on the situation - it's a good reason to use strategy pattern!

    Now, let's review the components of this pattern. I would like to show all examples in the java language, but I try to describe it, if you are not familiar with it.
  1. interface Strategey - interface, contains only one method - execute(), which execute some action;
  2. class Context - class, contains some Strategy, which you can change depends on the situation, and method, which execute some strategy;
  3. set of classes, which implements Strategy (ConcreteStrategy) - realizations of interface Strategy, or, in other words, the set of classes, which determ the behavior of program.
    Now, I would like to show you example of the using this pattern. Imagine, that we would like to write compressor, but there are many methods to compress file (for example RAR, ZIP etc), and we whant to use different methods depends on the situation. Let's rewiev the code:

public interface Strategy {
    void execute(File file);
}
public class Context {
    private Strategy strategy;

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public void execute(File file) {
        strategy.execute(file);
    }
}
public class RarCompressionStrategy implements Strategy{
    @Override
    public void execute(File file) {
        // There should be rar-compression algorithm
    }
}
public class ZipCompressionStrategy implements Strategy {
    @Override
    public void execute(File file) {
        // There should be zip-compression algorithm
    }
}
public class Main {
    public static void main(String[] args) throws IOException {

        //Creating the context object
        Context context = new Context();

        // Give the user the choice of algorithm.
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please, select the compression algorithm");
        String answer = reader.readLine();

        if (answer.toLowerCase().equals("rar")) {
            context.setStrategy(new RarCompressionStrategy());
        } else if (answer.toLowerCase().equals("zip")) {
            context.setStrategy(new ZipCompressionStrategy());
        } else {
            // if the user entered incorrect data
            throw new IllegalArgumentException();
        }

        //Executing some strategy
        context.execute(new File("some file"));
    }
}

    In this case, user chose the method of compressing. I hope, this example was interesting for you!

No comments:

Post a Comment