From the course: Java Design Patterns: Behavioral Part 2

Implementing the Strategy pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Implementing the Strategy pattern

- [Instructor] In this example, I'm going to create an application that can encrypt files in different ways using the strategy pattern. So I want to be able to encrypt these files, but I want to be able to choose at run time which type of encryption I want to use. So the first thing I'm going to do is I'm going to create an interface for the strategy. In my case, the strategy is the type of encrypter. So I'm going to create a new Java interface, and I'm going to call it encrypter. In here I'm going to create a single method called encryptFile. Normally, this would probably be a void method, and in the implementations of this, I'd have some codes to encrypt the file, but to keep this demo simple, I'm just going to return a string, which is going to describe what type of encryption is being used. So I'm going to do string encryptFile. So now I need to create the concrete implementations of this. The first type of encryption I might want to use is AES. So I'm going to create a new class, and call it AESEncrpter. And this is going to implement my encrypter interface. So I need to implement the encryptFile method. So in here, I'm going to do public string encryptFile. Inside this method, I'm just going to return a string that says applying AES encryption. Now I can create a second concrete implementation. So I'm going to create another Java class. And this one's going to be called RSAEncrypter. So, again, this is going to implement the encrypter interface. And again, I need to implement the encryptFile method, so I'm going to do public string encryptFile. And this time, I'm going to return a string that says applying RSA encryption. So now I have my small family of algorithms, which I could easily expand if I wanted to by adding more implementations of my encrypted interface. So now I'm going to create the context, which is going to be the file itself. So I'm going to create another new class, and this one is going to be called file. This class is going to have a field for the file name. So I'll do private string file name, and I'm going to create a constructor that passes in the file name. So I'll do public file, and pass in a string called fileName. And then inside this constructor, I can do this.fileName equals fileName. Now I just need my encrypt method. So underneath the constructor, I'm going to do public_void encrypt. And I'm going to pass an encrypter object into this method. So I'm going to pass in an encrypter called encrypter. so I'll be able to pass in any implementation of my encrypter interface. And inside this method, I'm going to system.out.printline and pass in encrypter.encryptFile. This will print out which type of encryption it's applying. Then I'm going to add the word to, so I'm going to do plus, then the string to, and then plus, and then the file name, so it will tell me which type of encryption it's applying to this exact file. Now all I need to do is try out this code. So I'm going to create one more class, which I'll call main. And in here, I'm going to have a main method. So I'll do public static void main. Inside my main method, I'm going to create a new file variable, so I'll do file file equals new file. I need to pass in the name of the file, so I'm just going to call this one test.pdf. Now I need to create the encrypter that I want to use. So let's create an AES encrypter object. So I'm going to do AESEncrypter, AESEncrypter equals new AESEncrypter. And now I can do file.encrypt and pass in AESEncrypter. So now let's run the app. I can see it prints out in the console that it's applying AES encryption to test.pdf. So this is a simple example of how to group algorithms with a strategy pattern so that they can be used interchangeably at run time. Another option would have been to define all the encrypters inside the file class, but this would have made it larger and less flexible and harder to maintain. This way, I've encapsulated the logic for the different encrypters in their own classes.

Contents