Given two classes:
First class performs AES encryption / decryption and returns encrypted / decrypted data given a certain key and chain.
Second class gathers data to be encrypted and then passes it to the encryption / decryption class.
Is it proper design to call directly the encryption class from the class that gathers the data or should there be an object between the two classes which abstracts the process further? Should I have one abstract class instance and one encryption instance to handle all of these types of requests during the program’s lifetime?
Personally, I would create some kind of abstract interface representing an encryption algorithm, with a factory function taking the key and producing a concrete instance of an encryption algorithm with a key installed. So the ‘second class’ here would call directly to the ‘first class’, but there would be a ‘third class’ in charge of instantiating the class. Something like:
C#’s
System.Security.Cryptographylibraries use a similar approach – see, eg,System.Security.Cryptography.SymmetricAlgorithm. Note however that since C# supports introspection, there’s no need for a factory class – instead there’s simply a static method taking a name. With C++ a full factory class is needed.