Strategy pattern is a behavioral design pattern. Strategy pattern is use while we want to change the implementations of an algorithm at runtime, without causing tight coupling.
The definition of Strategy provided by Gang of Four book on Design Patterns states:
Defines a set of encapsulated algorithms that can be swapped to carry out a specific behavior.
Where Should We Use This Pattern?
If we have multiple algorithm for same task and we have to decide (let’s use decide) at run time. Which algorithm should be use?
Example:
Like compression algorithm we have different option to compress as file or folder such as ZIP, GZIP, LZ4, RAR, TAR, 7Z, GZ so hare user have choice whatever the format he can select at runtime.
Another example data encryption class that can encrypts data using different encryption algorithms, such as AES, Triple DES, and Blowfish.
EncryptorWithoutPertan.java
package com.shubh.strategy.patern; public class EncryptorWithoutPertan { private String algoName; private String text; public void getEncryptedData() { if (algoName.equals("Aes")) { // using AES algorithm System.out.println("Output : " + text + " is Encrypted by AES algorithm"); } else if (algoName.equals("Blowfish")) { // using Blowfish algorithm System.out.println("Output : " + text + " is Encrypted by Blowfish algorithm"); } } public String getAlgoName() { return algoName; } public void setAlgoName(String algoName) { this.algoName = algoName; } public String getText() { return text; } public void setText(String text) { this.text = text; } }EncryptorMainTest.java
package com.shubh.strategy.patern;
public class EncryptorMainTest {
public static void main(String[] args) {
EncryptorWithoutPertan aseWP = new EncryptorWithoutPertan();
aseWP.setAlgoName("Aes");
aseWP.setText("Hello Data Test");
aseWP.getEncryptedData();
EncryptorWithoutPertan BlowfishWP = new EncryptorWithoutPertan();
BlowfishWP.setAlgoName("Blowfish");
BlowfishWP.setText("Hello Data Test");
BlowfishWP.getEncryptedData();
}
}
In the above code EncryptorWithoutPertan.java class we need to add if and else condition to implement the different algorithm. if we further want to add more algorithm then we need to modify this class.Now Applying the Strategy Pattern :
EncryptionStrategyPattern.java
package com.shubh.strategy.patern;
public interface EncryptionStrategyPattern {
public void getEncryptedData(String str);
}
Aes.javapackage com.shubh.strategy.patern;
public class Aes implements EncryptionStrategyPattern {
public void getEncryptedData(String str) {
System.out.println("####### Encrypted data By AES algorithm #####");
System.out.println("Output : " + str + " is Encrypted by AES algorithm");
}
}
Blowfish.java
package com.shubh.strategy.patern;
public class Blowfish implements EncryptionStrategyPattern {
public void getEncryptedData(String str) {
System.out.println("####### Encrypted data By Blowfish algorithm #####");
System.out.println("Output : " + str + " is Encrypted by Blowfish algorithm");
}
}
EncryptorStrategyClass.java
package com.shubh.strategy.patern;
public class EncryptorStrategyClass {
private EncryptionStrategyPattern strategy;
private String text;
public EncryptorStrategyClass(EncryptionStrategyPattern strategy) {
this.strategy = strategy;
}
public void getEncryptedData() {
strategy.getEncryptedData(text);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Junit Test For StrategyPattern
EncryptorStrategyPatternTest.java
package com.shubh.strategy.patern;
import org.junit.Test;
public class EncryptorStrategyPatternTest {
@Test
public void getEncryptedDataTest() throws Exception {
EncryptionStrategyPattern aes = new Aes();
EncryptorStrategyClass aesEncryptor = new EncryptorStrategyClass(aes);
aesEncryptor.setText("This is plain text");
aesEncryptor.getEncryptedData();
EncryptionStrategyPattern blowfishStrategy = new Blowfish();
EncryptorStrategyClass blowfishEncryptor = new EncryptorStrategyClass(blowfishStrategy);
blowfishEncryptor.setText("This is plain text");
blowfishEncryptor.getEncryptedData();
}
}
Note that using strategy pattern. I have create two seperate class Aes and Blowfish to implement the two algorithm. if in future i need to add one more algorithm then i just need to add one more implementation class of EncryptionStrategyPattern and no need to change in other class.
No comments:
Post a Comment