Why Software needs an Architecture - Proper method explained ? - Testability, Modifiablity, Readability, Maintainability etc.. All abilities !
Hey Readers, This post is the continuation of the previous post on "Why Software needs an Architecture ?" Before continuing on this post, please read that so that the its going to be more interesting.
In previous post we discussed about the importance of architecture, role of a developer in architecture implementation and a proof through a code using a use case of OTP sender. We have seen a improper code and found what are all the problems it is giving us though it served the purpose of sending OTP.
In this pose we will see on implementing the same in the proper way, considering certain, design patterns like, strategy pattern, factory pattern / Creator Pattern, Manager / Controller Pattern.
Let's us consider,
In previous post we discussed about the importance of architecture, role of a developer in architecture implementation and a proof through a code using a use case of OTP sender. We have seen a improper code and found what are all the problems it is giving us though it served the purpose of sending OTP.
In this pose we will see on implementing the same in the proper way, considering certain, design patterns like, strategy pattern, factory pattern / Creator Pattern, Manager / Controller Pattern.
Let's us consider,
- Testability as major NFR (Non functional requirement) in this post.
- Modifiability
- Re-Usability
- Extendability
- Additional / incremental changes ability.
Below is the live IDE for the code to run and test.
Below directory structure can be seen inside the proper folder as shown above
Lets see what is the functionality of various classes in these files !! No fear for the word jargon over there, trust me its going to be very simple.
Below is the code if you would like to take a look if not click here to jump to the next section
Comparing the previous post to the solution here,
We can clearly observe that more testable and more reliable and modifiable nature is obtained to the same simple use case just by modifying the way the code units work.
Now the code has become more testable and other non functional requirements got added,
Now we are able to test for various scenarios as mentioned above, if OTP (One Time Password) generated or not, mail sent or not, mail server error handled or not etc.
Hope this post would have placed a good level of impact in the minds on how much the architecture is important to the software and how much it smooth-ens our life and SDLC (Software development life cycle) a lot. Lets us all think in the different perspective of seeing the code and try to arrange the code in more fruitful ways. Lets explore more and more design patterns and architectural patterns since patterns are the experience proven solutions given by by our software ancestors and experts to the common problems faced.
Happy coding and software !! Stay tuned for more interesting posts.
- core - contains all the core of the system - controller, managers goes here mainly.
- strategy - contains all the strategy classes, that will define the strategy to extend the otp sending to various channels like email, sms (Today), but can also add Whatsapp, telegram, etc later has strategy and no changes in the core
Lets see what is the functionality of various classes in these files !! No fear for the word jargon over there, trust me its going to be very simple.
Just for Fun ! |
- otp.factory.ts
- Responsibility: To create the objects of email otp sender strategy and sms otp sender strategy based on the condition sent by the controller (OTPManager) and provide the objects.
- Advantages :
- Follows Creator pattern & Singleton - All advantages of creator pattern applies.
- Any point in time in unit testing to mock a object, we need to mock the factory so has to manufacture :-P our own objects !
- Responsibility of creation should not be given to the business logic classes, so to avoid coupling and modifiability any time. Any time I need to change the reference of the object only in the factory.
- otp.factory.spec.ts
- Responsibility : Unit test file that unit test the working of the factory.
- Scenarios tested -
- should getInstance method provides the instance of OTPFactory
- should getInstance return the object
- getGenerator should return the generator object for various configuration option
- otpmanager.ts
- Responsibility : To be the controller that handles the entire use case. It co-ordinates with other units in the code and generate the strategy object and invoke the strategy to send the otp. It also helps in sending the configuration that the user is willing. Also follow Singleton pattern
- Advantages:
- All the advantages of Controller pattern applies here
- Coordinator helps in getting the things done and makes modifiable more easy just by changing the flow.
- Abstracts the business logic and other structural logic to keep the boundary maintained.
- Handles the errors and exceptions in the single place and more reliable.
- otpmanager.spec.ts
- Responsibility : Unit test file that unit test the working of the otpmanager.
- Scenarios tested -
- sendOtp should send the sms and return true
- sendOtp should send the email and return true
- sendOtp should send the sms & email and return true
- securityManager.ts
- Responsibility : Maintains all the crypto algorithms to encrypt and decrypt the otp. Later this manager can be used for enforcing the SSL certificate and much more.
- Advantages:
- Single responsibility principle taking care of the security needs in one place
- Follows the singleton pattern
- Provides the re-usability of the encryption and decryption through out the project.
- Provides the modifiable nature to change the security algorithms in one place and that get applied every where with out much changes.
- securityManager.spec.ts
- Responsibility : Unit test file that unit test the working of the securityManager.
- Scenarios tested -
- encrypt and decrypt should work properly
- util.ts
- Responsibility : Any re-usable util methods will go here. In our use case, to generate the OTP of 4 digit it works.
- Advantages:
- Its main advantage is re-usable and modifiable at one place.
- util.spec.ts
- Responsibility : Unit test file that unit test the working of the securityManager.
- Scenarios tested -
- generateRawOtp should return some random number of 4 digit in string format
Below is the code if you would like to take a look if not click here to jump to the next section
Let us now start with the strategy folder
- email.otpsender.ts
- Responsibility: This is the strategy class that implements the logic to send the otp through mail. Initialization of the mail server communication and sending the mail are all handled in this.
- Advantages :
- Follows the Strategy pattern and have all its advantages.
- Provides the isolation of channel logic away from the business logic.
- Provides the re-usability of strategy (email channel) for various purpose independently also.
- email.otpsender.spec.ts
- Responsibility : Unit test file that unit test the working of the email.otpsender strategy class.
- Scenarios tested -
- Should return status as sent if the email is success
- Should return error if email is not sent successfully
- generateOTP() method should return the encrypted OTP
- getTransporter() method should return the Mail object
- sms.optsender.ts
- Similar to the above email strategy, this is for the email strategy.
- Advantages:
- Even though I did not implement the logic to send the sms in this example, I have the benefit of switching off on any time, now this acts like a stub. This is one of the power of isolating as strategies.
- sms.optsender.spec.ts
- Responsibility : Unit test file that unit test the working of the sms.optsender strategy class.
- Scenarios tested -
- Send should get rejected saying "Method not implemented"
Comparing the previous post to the solution here,
We can clearly observe that more testable and more reliable and modifiable nature is obtained to the same simple use case just by modifying the way the code units work.
Now the code has become more testable and other non functional requirements got added,
Now we are able to test for various scenarios as mentioned above, if OTP (One Time Password) generated or not, mail sent or not, mail server error handled or not etc.
Hope this post would have placed a good level of impact in the minds on how much the architecture is important to the software and how much it smooth-ens our life and SDLC (Software development life cycle) a lot. Lets us all think in the different perspective of seeing the code and try to arrange the code in more fruitful ways. Lets explore more and more design patterns and architectural patterns since patterns are the experience proven solutions given by by our software ancestors and experts to the common problems faced.
Happy coding and software !! Stay tuned for more interesting posts.
Comments
Post a Comment