Friday, May 16, 2014

S.O.L.I.D. Design Priniciples

Being a Software Developer or a Software Architect you must think what end product you are giving to your client; and to provide a good product our design must be SOLID.

Lets see what happens if our design is not good

Below picture looks funny but, its a perfect example of bad design, and I really don't need to write consequences of having such design :)



This is what can happen if you do not care about design of your software.
To avoid such design, people had come up few principles that is called as SOLID Design principles.
Now let see what are those.

1. Single Responsibility Principle:-
What this principle says is give one and only one responsibility to a "module" or "class" or "function". Therefor in future if at all you want to remove that function or you want to make any changes to function it will NOT affect any other functionality of entire system. Same is applicable for module or class as well.

Now lets see how could be bad design in programmatic way
Objective:- after user login display following statistics 
  1. count of all jobseekers
  2. count of all employers
  3. count of all active jobs

public void login(String username, String password){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ABC");
java.sql.Statement st= con.createStatement();

boolean isCorrect = false;
//check for login
rs=st.executeQuery("select userName, pass from login_tbl where userName='"+username+"' and pass='"+password+"'");
while(rs.next()) {
isCorrect =true;
}
if(isCorrect){
//fetch count of all employers
rs=st.executeQuery("select count(*)employer from employer_com");

while(rs.next()) {
dbemployer=rs.getString("employer");
}

//fetch count of all jobseekers
rs=st.executeQuery("select count(*)jobseeker from resume_rsm");

while(rs.next()) {
dbjobseeker=rs.getString("jobseeker");
System.out.println("no of employer is"+dbjobseeker);
}

//fetch count of all all active jobs
rs=st.executeQuery("select count(*)Active from job_job1 where job_deadline>='"+systdate+"'");

while(rs.next()) {
dbactive=rs.getString("Active");
System.out.println("no of Active jobs is"+dbactive);
}
}
}catch(Exception e){
}
}
In above code to achieve the objective I have wrote all my business logic in single function itself.
Now suppose requirement has got changed "Instead of after login, display above statistics after click on my profile except active jobs".

Now what will you do? If you have such code written, I guess you have re-write your code.
So it is always advised to have single responsibility for one function/class.
In above code you can have separate functions for each and every part of your objective.

Like have functions
  1. dataBaseConnection() -- to connect database
  2. login() -- to check login business login
  3. jobSeekerCount() -- count of all jobseekers
  4. employersCount() -- count of all employers
  5. activeJobCount() -- count of all active jobs
1. Open and Closed Principle:-
Our code must be open for extension but should be closed for any modification.

Now what is that mean?
Let me describe it using following picture.










Monday, September 23, 2013

Core Java Basics

Hi, its been long time I did not write any blog. So thought to start with core java basics.
Whenever you heard term Java , the first question arises in mind is "What is Java?"
So let me explain about it first.

What is Java?

In very simple world if I will say, it is a programming language which allow you write program in one plate form (i.e. any OS ) and it's compiled code can run on any other plate form.

Java is Object Oriented Programming language; but it is not pure object oriented. 

As like any other language java also has script and grammar.
OOPs is scripting and there shall be rules to achieve OOps for Java. There is no such description anywhere as I told in above line :) but I just want you make it as easy as possible, you should be able to relate the thing which you know.