Monday, September 12, 2016

Builder Design Pattern

Builder Pattern says that "construct a complex object from simple objects using step-by-step approach"

Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.
There are three major issues with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.
  1. Too Many arguments to pass from client program to the Factory class that can be error prone because most of the time, the type of arguments are same and from client side it’s hard to maintain the order of the argument.
  2. Some of the parameters might be optional but in Factory pattern, we are forced to send all the parameters and optional parameters need to send as NULL.
  3. If the object is heavy and its creation is complex, then all that complexity will be part of Factory classes that is confusing.
We can solve the issues with large number of parameters by providing a constructor with required parameters and then different setter methods to set the optional parameters. The problem with this approach is that the Object state will be inconsistent until unless all the attributes are set explicitly.
Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.

For example, you can consider construction of a home. Home is the final end product (object) that is to be returned as the output of the construction process. It will have many steps, like basement construction, wall construction and so on roof construction. Finally, the whole home object is returned. Here using the same process, you can build houses with different properties.
GOF says,

Advantage of Builder Design Pattern
The main advantages of Builder Pattern are as follows:
  • It provides clear separation between the construction and representation of an object.
  • It provides better control over construction process.
  • It supports to change the internal representation of objects.
Builder Design Pattern Example in JDK
DocumentBuilderFactory, StringBuffer, StringBuilder are some examples of builder pattern usage in java API.
Some of the builder pattern example in Java classes are;
  • java.lang.StringBuilder#append() (unsynchronized)
  • java.lang.StringBuffer#append() (synchronized)
Usage examples:

  1. Builder pattern may be very useful while writing Unit Tests. When in order to construct the object under the test, need to pass a lot of parameters to the constructor and some of these parameters are completely irrelevant for the specific test. Builder class creation with separate methods for each parameter that should be tested, which returns by the end complete object under the test will help to write many UTs effectively, without duplicating the code.
  2. Building an XML document with HTML elements (<html>,<h1>,<h2>, <body>,<p> and etc)
  3. Building a smartphone object with attributes like RAM, size, resolution, OS, waterproof and so on.



Tuesday, September 6, 2016

Unix - Useful Commands

This quick guide lists commands, including a syntax and brief description. For more detail, use −
$man command

Files and Directories
These commands allow you to create directories and handle files.
Command
Description
cat
Display File Contents
cd
Changes Directory to dir name
chgrp
change file group
chmod
Changing Permissions
cp
Copy source file into destination
file
Determine file type
find
Find files
grep
Search files for regular expressions.
head
Display first few lines of a file
ls
Display information about file type.
mkdir
Create a new directory dirname
more
Display data in paginated form.
mv
Move (Rename) a oldname to newname.
pwd
Print current working directory.
rm
Remove (Delete) filename
rmdir
Delete an existing directory provided it is empty.
tail
Prints last few lines in a file.
touch
Update access and modification time of a file.

Manipulating data
The contents of files can be compared and altered with the following commands.
Command
Description
awk
Pattern scanning and processing language
cmp
Compare the contents of two files
comm
Compare sorted data
cut
Cut out selected fields of each line of a file
diff
Differential file comparator
expand
Expand tabs to spaces
join
Join files on some common field
sort
Sort file data
split
Split file into smaller files
wc
Count words, lines, and characters
vi
Opens vi text editor
vim
Opens vim text editor

Compressed Files
Files may be compressed to save space. Compressed files can be created and examined −
Command
Description
compress
Compress files
gunzip
Uncompress gzipped files
gzip
GNU alternative compression method
uncompress
Uncompress files
unzip
List, test and extract compressed files in a ZIP archive
zcat
Cat a compressed file
zcmp
Compare compressed files
zdiff
Compare compressed files
zmore
File perusal filter for crt viewing of compressed text

Network Communication
These following commands are used to send and receive files from a local UNIX hosts to the remote host around the world.
Command
Description
ftp
File transfer program
rcp
Remote file copy
rlogin
Remote login to a UNIX host
rsh
Remote shell
telnet
Make terminal connection to another host
ssh
Secure shell terminal or command connection
scp
Secure shell remote file copy
sftp
secure shell file transfer program

Misc Commands
These commands list or alter information about the system
Command
Description
chgrp
Change the group ownership of a file
chown
Change owner
date
Print the date
du
Print amount of disk usage
echo
Echo arguments to the standard options
exit
Quit the system
Show group memberships
homequota
Show quota and file usage
iostat
Report I/O statistics
kill
Send a signal to a process
last
Show last logins of users
logout
log off UNIX
netstat
Show network status
passwd
Change user password
passwd
Change your login password
printenv
Display value of a shell variable
ps
Display the status of current processes
top
Display all system processes
umask
Show the permissions that are given to view files by default
uname
Display name of the current system
uptime
Get the system up time
useradd
Create a user account
users
Print names of logged in users
vmstat
Report virtual memory statistics
w
Show what logged in users are doing
who
List logged in users