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.

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.

Credits: http://www.ryocentral.info/2013/07/getting-xbacklight-and-brightness-keys.html

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.

Thursday, October 30, 2014

Assigning to a variable from a pipe in Z Shell

Unfortunately or sadly, this could not be done by me for my use case in Bash. I'm looking for a good solution in Bash too. Just to note here. To read from the pipe you just have to use the Read command. It only works this way in Zsh so far.
blkid $dev | sed -n 's/.*UUID=\"\([^\"]*\)\".*/\1/p' | read var;
Now you can easily use that $var in that scope like
echo $var

Z Shell, the fancy in Linux terminal

Z Shell is probably now the best known Shell option for Unix users all over the globe. That too contributed by the fact that most people take the Terminal they get as full and final and never give a second thought about it's improvement. But geeks can't resist exploring and the first option they get astounds them because Z Shell, if compared to Bash, is simple a super set of it's functionality. It gives us a great Terminal which is intelligent and customizable to the point where people might ask you "Which terminal is this?".

Installation: This is fairly simple. Just a quick apt-get
sudo apt-get install zsh
Configuration: After you have installed Z Shell, the very first thing you have to do launch is type in your terminal
zsh
The first you do this, you will get some configuration manual that is navigated using numbers. The procedures are very self explanatory. It helps you configure various Options such as
  • How far back in time do you want to remember commands
  • If you want the shell to search history using partially entered commands (It's neat and powerful)
  • Should all of your terminals be in sync while running
The whole configuration is saved into the file at '/home//.zshrc'.
After you have configured the shell, you can make it your default shell using the command
chsh  -s /bin/zsh

Configuration repositories: Some great configurations for Zsh have come up with full modular support and themes that will make your experience with Z a breeze. These are the most popular to repositories. There are some others that maintain Z shell configurations around the net.
Using these pre-built configurations is very easy. Such as, configuring with Prezto. You need to type in the following after launching Z Shell. The commands will create Z Shell configurations in your home directory.
git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"
setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done
This will simple clone the repository into your home folder and then, link the configurations in place of your current configurations.


Sunday, October 26, 2014

NodeJS permission issues troubleshooting path

It was such a terrifying experience to have node and especially yeoman not work because of weird permission issues. I fixed the issues finally and kind of think there's pattern to it. This is not a full and final cookbook type of thingy but you might wanna check this method out in case you are stuck with the same issues. First of all
1. If the problem is not in your project directory then it must me in your ~/.config and ~/.npm directories. As you may have seen many newbies of node opt to the sudo method of installing things. But this causes problem regarding permission when yeoman comes in as some of the commands need access to areas where root is not permitted and if you do not use sudo before yo, you get errors about some of the errors where you need root. This mess up is simply caused by sometimes using sudo and sometimes not. Which you have to do because you do not own the ~/.npm directory. Just own it using
sudo chown -R `whoami` ~/.npm
2. If the problem still persists, check your project directories node_modules directory if it has any subdirectory or file deep inside where access is needed it has root privileges required. So you can use the same owning method there too
sudo chown -R `whoami` ~/.npm
3. Always try to install non globally. This will save you a lot of trouble.

Credits: Stackoverflow [ http://stackoverflow.com/questions/16151018/npm-throws-error-without-sudo ]

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...