Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy Pattern and context class

Reading about the strategy design pattern, was thinking if it is possible to have two context.

Looking forward for opinions if the below code-design utilises the Strategy design pattern.

Or strategy design pattern forces us to have only one context meaning that the design below is incorrect.

public interface Algorithm {}
public class FirstAlgorithm implements Algorithm {}
public class SecondAlgorithm implements Algorithm {}


public interface Cryptography {}

public class DecryptionContext implements Cryptography {
    public DecryptionContext(Algorithm algorithm) {
        this.algorithm = algorithm;
    }
}

public class EncryptionContext implements Cryptography {
public EncryptionContext(Algorithm algorithm) {
        this.algorithm = algorithm;
    }

}
like image 663
IonKat Avatar asked Mar 07 '26 03:03

IonKat


2 Answers

Used strategy within Algorithm, but with your example it's a little bit forced since it's not mandatory to bound Contructor with Algorithm.

It's just an basic example of how to use Strategy.

public interface Algorithm
{
     public String doOperation(...);
}
public class FirstAlgorithm implements Algorithm { ... decrypt ...}
public class SecondAlgorithm implements Algorithm { ... encrypt ...}


//public interface Cryptography {}
//another option is to implement Algorithm on both Enc/Dec - class,
//it's only of your design and purpose
public class DecryptionContext  {
    ...
    public DecryptionContext(Algorithm algorithm,String strEncrypt) 
    {
        this.algorithm = algorithm;
        ...
    }
}

public class EncryptionContext  {
public EncryptionContext(Algorithm algorithm, String strDecrypt) {
        this.algorithm = algorithm;
        ...
    }

}

class Main
{
   ...
   Algorithm ae = new SecondAlgorithm();
   EncryptionContext ec = new EncryptionContext(ae, "abc");
   //this op can be done even inside EncryptionContext ... but the purpose is understanding strategy 
   String res = ec.ae.doOperation(ec.strDecrypt); //abc -> efg
   //since only need input string could be rewrite as
   //String res = ae.doOperation(ec.strDecrypt)
   ae = new FirstAlgorithm();
   DecryptionContext dc = new DencryptionContext(ae, res);
   res = dc.ae.doOperation(dc.strEncrypt); //efg->abc
   
}
like image 106
Traian GEICU Avatar answered Mar 09 '26 16:03

Traian GEICU


Strategy Pattern aims to implement an algorithm in multiple ways without clients being aware of its details. In your example, Encryption and Decryption are two different contexts and thus they will do two different things. Assuming your client will use Cryptography interface, you need to define encrypt and decrypt methods. In this case, you force EncryptionContext class to implement decrypt and DecryptionContext class to implement encrypt methods which would not be correct. That is why, I do not think this design couples with Strategy Pattern.

like image 28
Onur Arı Avatar answered Mar 09 '26 16:03

Onur Arı



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!