Tuesday, May 2, 2017

All you need to know in GIT Branching

Git Branching


Git is a free and open source distributed version control system designed to handle from small to very large projects with efficiency. Git is really easy to use and learn.  In this article we are going to cover one important aspect of Git which is called Branching.

Git Branches are separate, independent line of development in a collaborative project. The first branch in every project is called 'master-branch'. Branching is very useful in a research and development project as many developers might work in many features of the final product. So, the developers can finally merge there independent work in to the 'Master' branch and develop a fully functional product without knowing what the other developer has done.  

Listing Branches


To list the branches and tags in the current project please use the following command.
git branch

Creating a Branch


Just use the following command with an appropriate branch name to 'branch-name'.
git branch <branch-name>
After creating the branch use following command to push the changes in the newly created branch into the remote-git-repository.
git push --set-upstream origin <branch-name> 

Renaming Branches 


To rename the branches we have first rename the local branch and push it to the remote repository. To rename the local branch we can use two methods.

If in the branch which need to be renamed we can use : 
git branch -m <new-name>
Otherwise you have to specify the old branch name also :
git branch -m <old-name> <new-name> 
(-m = option is stand to 'move')

Then we have to remove the old-branch from remote repository and add the new-branch.
git push origin: <old-name> <new-name>
Then finally we have to set the upstream to the new-branch.
git push  origin -u <new-name>

Deleting Branches 


To delete a branch you have to first remove the branch from the local repository and finally remove it from the remote repository. The latter step might need some privileges from the system-administrator unless you are the owner of the repository.

First remove the branch from the local repository.
git branch -d <branch-name>
(-d = stands for 'delete')

Then remove it from the remote repository.
git push --delete <branch-name>

Changing the Branch 


To change your current branch you can use 'checkout' command.
git checkout <branch-name>


Merging Branches
 


Merging meaning connecting components in two git branches into one. Following command will merge the content in 'another-branch' into the current branch.
git merge <another-branch>

Conclusion


I hope that this will be helpful when learning git branching. You can have more details from 'git' manual pages or git home page.


Monday, May 1, 2017

State Design Pattern

State Design Pattern


If you are a programmer, Design Pattern are some main aspects that you need to study and grow up on. Even though number of Design-Pattern exists in this article we are going to explore a Behavioral Design Pattern called State Pattern

According to Wikipedia  state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern's super-class.

If you are familiar with State Machines, then State Pattern is not a harder aspect that you cannot master. State Pattern is well suited when the behavior of an object changes in accordance with the internal state of the object.  So, if anyone wants to build an Object-Oriented State Machine, then the most suitable structure to be used is State Pattern.

Structure of State Pattern


In State Pattern we have an 'Context' class which interact with client. You can think of this like the 'State Machine'. Then define an 'State' Abstract Class. Implement this class for different states that the Machine should change. Define an Abstract State Specific Behavior in 'State' class and override it in every sub-class. Have a pointer to 'Current State' inside the 'Context' Class. When the internal state of the machine should change, adjust the 'Current State' pointer to the next specified state of the machine. 

State Pattern does not specify where state transitions will be defined. There are two choices: inside the 'Context' class or Each individual state derived class. Advantage of the latter is that we can add new states easily. But each State derived class has knowledge of its siblings, which introduces a dependency between sub-classes.  

Here is the Class Diagram for State Pattern.


Is it a Good Choice ? 


Any concept is a good choice in its own domain. So, use this Pattern if an only if the behavior of objects in the domain is dependent of the internal state of the object.
Here is a code example for State Pattern.