Showing posts with label Design patterns. Show all posts
Showing posts with label Design patterns. Show all posts

Saturday, January 24, 2015

The Strategy pattern

Formal definition: The Strategy pattern defines a family of algorithms, encapsulates each one and makes them interchangeable. Strategy lets the algorithms vary independently from clients that use it.

The idea: We want to code. Albeit peacefully. We want to just put in the necessary additions and deletions using the same concepts, hit save and BAM!! Everything has to work at that point. The one thing that could come in our way is that though the overall coding nature stays the same the underlying implementations and ideas have changed. How do we make the code behave the same even though some underlying components are contradicting with our current implementations? The first thing that could come to mind is to add conditions, dirty ones. Then, we get a bit more gentle and push those conditions inside the class hideously. But what about the next iteration? No, we need something better.
Wait, why are we not letting the component decide what it wants to show? We just add a Generalization layer that allows us to use the same Client code while beyond that, the components tell what the client code shows. For example:
If you have worked with form validations, how often have you faced the problem of having inadequate information in the validation mechanism. Such as, Text input fields, Email fields, Textarea all have similar implementations that's fine. So, we can just iterate over each field, create an object of validator, run it on that field's data and done. But, what about Checkboxes? You need to check how many of them are ticked, you need only one of them ticked. What about Payment data? What if in the next iterations you need Combined field types to be validated? These changes are big and they will turn monstrous.
But if we create validators of each field types and bind their validations "Strategies" to them, then all we really need to do is check which field type we are currently checking, create a validator of that type and run that on that field. Since we also create a generalization layer it's easy to communicate which type we want the validator to have.

Example:

My dirty footsteps into the world of Design Patterns

Obviously the monks are not happy that someone this benign and idiotic has stepped into their holy lands but you know, let's just chill. I'm only writing here about what I have come to understand about design patterns and their various implementations. Currently I'm learning using Java and it's a great experience so far.

Design patterns are: Solutions you can reuse in your architecture to cut down "re invention" times. You might be facing problems trying to find a proper structure which you can follow in your software project so that changes don't make you lose sleep. Well, Patterns are solutions for different cases that may or may not (but most of the time they do) cover yours. Algorithms solve problems for programmers. Design patterns solve structuring problems so to speak.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...