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.


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.

Sunday, January 18, 2015

Getting brightness keys to work on Xubuntu

Well, it's not even an original content or anything. Just noting it down since blogs always come and go. So I kept a reference.
I own a machine whose brightness keys don't work out of the box in Linux. So, I used to employ xbacklight in order to change brightness. Obviously it wasn't a good solution. It just allowed me to change to a state but not keep that state or even use the brightness controller keys. Here's a great solution that solves all of it. The initial problem: I can't change the brightness of my laptop screen with Ubuntu 13.04.
Apparent solution: xbacklight -set 41 (for a moderate brightness)
Problem: Didn't work. Xbacklight did not have any function, no matter what argument was given.
Solution: Search the line within /etc/default/grub and change it to
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash acpi_osi=Linux acpi_backlight=vendor"
After a sudo update-grub, xbacklight works as intended, and the screen brightness keys on my Laptop also work normal. Very nice. If your screen brightness changes again after reboot, you can enter the xbacklight line to the startup applications. After login, your eyes will thank you.


Thursday, December 18, 2014

Postgresql different syntaxes

As I am currently working on a project involving PostgreSql, I came to learn some very important commands that I use. here's a list of those commands.