28. Project Directory Structure for Realtime
Saturday, July 23, 2011
Hi Guys !
It's been long time , I think you guys are waiting for my posts, I just went through your comments and decided to post something which is useful to your future studies.
In this post i will demonstrate how the real time project will be organized, for this you can use any kind of IDE like Netbeans or Eclipse.For this time I will show you using Eclipse, since this IDE is very popular in development, so lets get started !!!
So, If we want to start a project, first we must identify the domain, which means the main object or the data you are going to deal with, let us say for instance ...if we want to calculate the payroll of an employee...our domain will be "Employee" or to provide an interface to an user to register in your website "User" will be our domain.
After identifying the "domain", we must think in such a way that "How to Implement the chosen Domain".For this identify the data which is best suited for your domain.
For example if our domain is "Employee", which means the best possible data which resides inside that domain will be "employeeId, firstName, lastName and salary etc..".After identifying the data for the domain, provide a public interface to access the data in implementation and call this implementatation as "EmployeeImpl or DomainImpl".
Now, we need to identify the "Services" we must provide to the implementation of our "Domain".The Services in the sense, the data manipulations and retrial of the data which can occur in our domain.For instance if we want to "Add" an employee to the database or to "Remove" an employee from the memory...or some times to "list all the employees" which are part of our domain, these are called services.
When we identify the "Services" for our domain, it is not difficult to provide the "implementation for our identified Services", we call this as "ServiceImpl".
In "Service Implementation" we can describe in what way we are providing the implementation, for instance if we want to implement a service called "Add Employee", this service needs to add a new "Employee" to memory, in the same way "List All Employees" service needs to display all the employees which are available in database to the UI(User Interface).
Finally, we need to identify what are the "Utilities"which provide basic interface to the end user to supply data to the "Domain". for example providing an UI (User Interface) to the user to enter data to the domain model.So by the help of the above entities we can create a project structure using "Domain, DomainImpl, Service, ServiceImpl and Utils".
So have a look at the fallowing image to understand the directory structure to implement "Employee" domain.
After creating the structure, write the code as I given below.
Employee.java
The above Employee code is a domain interface, it consists of the basic public interface to implement the domain, now we need to implement this interface to a class called EmployeeImpl.java and override the methods as shown below.
EmployeeImpl.java
And now write services for Employee domain and implement those services, for this we need to write an interface called "EmployeeService.java" and this will be implemented in "EmployeeServiceImpl.java" and override the methods.Now have a look at the fallowing code.
EmployeeService.java
Now !!!, write a class "EmployeeServiceImpl", implement the above interface and override the methods in this class as I shown below.
EmployeeServiceImpl.java
After writing the above code, in Utils package create a class called "KeyBoard.java" to provide input for accepting an Integer and String values.
Have a look at the fallowing code, so that you can get an idea.
KeyBoard.java
Now write "EmployeeIO.java" to give input and to display output as I shown below.
EmployeeIO.java
And finally write your Main class to execute this application, the code fallows below.... EmployeeMain.java
So that's it guys...keep visiting the blog for updates !!!!
It's been long time , I think you guys are waiting for my posts, I just went through your comments and decided to post something which is useful to your future studies.
In this post i will demonstrate how the real time project will be organized, for this you can use any kind of IDE like Netbeans or Eclipse.For this time I will show you using Eclipse, since this IDE is very popular in development, so lets get started !!!
So, If we want to start a project, first we must identify the domain, which means the main object or the data you are going to deal with, let us say for instance ...if we want to calculate the payroll of an employee...our domain will be "Employee" or to provide an interface to an user to register in your website "User" will be our domain.
After identifying the "domain", we must think in such a way that "How to Implement the chosen Domain".For this identify the data which is best suited for your domain.
For example if our domain is "Employee", which means the best possible data which resides inside that domain will be "employeeId, firstName, lastName and salary etc..".After identifying the data for the domain, provide a public interface to access the data in implementation and call this implementatation as "EmployeeImpl or DomainImpl".
Now, we need to identify the "Services" we must provide to the implementation of our "Domain".The Services in the sense, the data manipulations and retrial of the data which can occur in our domain.For instance if we want to "Add" an employee to the database or to "Remove" an employee from the memory...or some times to "list all the employees" which are part of our domain, these are called services.
When we identify the "Services" for our domain, it is not difficult to provide the "implementation for our identified Services", we call this as "ServiceImpl".
In "Service Implementation" we can describe in what way we are providing the implementation, for instance if we want to implement a service called "Add Employee", this service needs to add a new "Employee" to memory, in the same way "List All Employees" service needs to display all the employees which are available in database to the UI(User Interface).
Finally, we need to identify what are the "Utilities"which provide basic interface to the end user to supply data to the "Domain". for example providing an UI (User Interface) to the user to enter data to the domain model.So by the help of the above entities we can create a project structure using "Domain, DomainImpl, Service, ServiceImpl and Utils".
So have a look at the fallowing image to understand the directory structure to implement "Employee" domain.
After creating the structure, write the code as I given below.
Employee.java
package domain; /** * @author Sudarsan Domain Interface Employee */ public interface Employee { public int getId(); public void setId(int id); public String getFirstName(); public void setFirstName(String firstName); public String getLastName(); public void setLastName(String lastName); public void setEmail(String email); public String getEmail(); public int getPriority(); public void setPriority(int priority); public String getDesignation(); public void setDesignation(String designation); public String toDisplayString(); }
The above Employee code is a domain interface, it consists of the basic public interface to implement the domain, now we need to implement this interface to a class called EmployeeImpl.java and override the methods as shown below.
EmployeeImpl.java
package domain.impl; import java.io.Serializable; import domain.Employee; /** * @author Sudarsan Implementing the domain interface Employee */ public class EmployeeImpl implements Employee, Serializable { private static final long serialVersionUID = 1L; private int id; private String firstName; private String lastName; private String email; private int priority; private String designation; @Override public int getId() { return id; } @Override public void setId(int id) { this.id = id; } @Override public String getFirstName() { return firstName; } @Override public void setFirstName(String firstName) { this.firstName = firstName; } @Override public String getLastName() { return lastName; } @Override public void setLastName(String lastName) { this.lastName = lastName; } @Override public String getEmail() { return email; } @Override public void setEmail(String email) { this.email = email; } @Override public int getPriority() { return priority; } @Override public void setPriority(int priority) { this.priority = priority; } @Override public String getDesignation() { return designation; } // A method to set Employee Priority based on his designation @Override public void setDesignation(String designation) { if ("CEO".equalsIgnoreCase(designation)) { setPriority(1); } else if ("MD".equalsIgnoreCase(designation)) { setPriority(2); } else if ("HR".equalsIgnoreCase(designation)) { setPriority(3); } else { setPriority(4); } this.designation = designation; } @Override public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("EmployeeImpl [id="); buffer.append(id); buffer.append(", firstName="); buffer.append(firstName); buffer.append(", lastName="); buffer.append(lastName); buffer.append(", email="); buffer.append(email); buffer.append(", priority="); buffer.append(priority); buffer.append(", designation="); buffer.append(designation); buffer.append("]"); return buffer.toString(); } // To print the employee @Override public String toDisplayString() { StringBuffer buffer = new StringBuffer(); buffer.append(this.getId()); buffer.append("\t"); buffer.append(this.getFirstName()); buffer.append("\t"); buffer.append(this.getLastName()); buffer.append("\t"); buffer.append(this.getEmail()); buffer.append("\t"); buffer.append(this.getDesignation()); return buffer.toString(); } }
And now write services for Employee domain and implement those services, for this we need to write an interface called "EmployeeService.java" and this will be implemented in "EmployeeServiceImpl.java" and override the methods.Now have a look at the fallowing code.
EmployeeService.java
package service; import java.util.List; import domain.Employee; /** * @author Sudarsan Interface for Employee Services */ public interface EmployeeService { // We can add more methods in future, like removeEmployee, UpdateEmployee List<Employee> getAll(); void addEmployee(Employee employee); }
Now !!!, write a class "EmployeeServiceImpl", implement the above interface and override the methods in this class as I shown below.
EmployeeServiceImpl.java
package service.impl; import java.util.ArrayList; import java.util.List; import service.EmployeeService; import domain.Employee; /** * @author Sudarsan Implementing EmployeeService interface */ public class EmployeeServiceImpl implements EmployeeService { private List<Employee> employees = new ArrayList<Employee>(); @Override public List<Employee> getAll() { return this.employees; } @Override public void addEmployee(Employee employee) { this.employees.add(employee); } }
After writing the above code, in Utils package create a class called "KeyBoard.java" to provide input for accepting an Integer and String values.
Have a look at the fallowing code, so that you can get an idea.
KeyBoard.java
package utils; import java.util.Scanner; /** * @author Sudarsan Utility Class to take input from the KeyBoard */ public class KeyBoard { public static int readInt(String message) { System.out.print(message); Scanner scanner = new Scanner(System.in); try { int number = Integer.parseInt(scanner.next()); return number; } catch (NumberFormatException e) { System.out.println("Invalid Number"); return readInt(message); } } public static String read(String message) { System.out.print(message); Scanner scanner = new Scanner(System.in); return scanner.nextLine(); } }
Now write "EmployeeIO.java" to give input and to display output as I shown below.
EmployeeIO.java
package utils; import java.util.List; import domain.Employee; import domain.impl.EmployeeImpl; /** * @author Sudarsan Utility Class to manage Input and Output */ public class EmployeeIO { private static final long serialVersionUID = 1L; /* Method to read employee data from keyboard */ public static Employee read() { Employee employee = new EmployeeImpl(); employee.setId(KeyBoard.readInt("Enter Id :")); employee.setFirstName(KeyBoard.read("Enter First Name :")); employee.setLastName(KeyBoard.read("Enter Last Name :")); employee.setEmail(KeyBoard.read("Enter Email :")); employee.setDesignation(KeyBoard.read("Enter Designation :")); return employee; } /* Method to print employee data to console */ public static void out(List<Employee> employees) { StringBuilder builder = new StringBuilder(); builder.append("Id\tFirst Name\tLast Name\tEmail\tDesignation\n"); for (int i = 0; i < employees.size(); i++) { builder.append(employees.get(i).toDisplayString()); builder.append("\n"); } System.out.println(builder); } }
And finally write your Main class to execute this application, the code fallows below.... EmployeeMain.java
package utils; import service.EmployeeService; import service.impl.EmployeeServiceImpl; import domain.Employee; /** * @author Sudarsan Starting Point */ public class EmployeeMain { public static void main(String[] args) { int option = 1; /*Calling the EmployeeService*/ EmployeeService empService = new EmployeeServiceImpl(); do { Employee employee = EmployeeIO.read(); /* Invoking the method addEmployee on empService object */ empService.addEmployee(employee); option = KeyBoard .readInt("Do you want add another employee(1/0) :"); } while (option == 1); /* Invoking the method getAll on employeeService object */ EmployeeIO.out(empService.getAll()); } }This is the structure everyone fallows in real time, so get acquainted with this..if you get the clear idea on this application, it is very easy to use and understand the frameworks like "Struts, Spring and Wicket etc". "Spring and Spring MVC" fallows the same pattern as I explained above.
So that's it guys...keep visiting the blog for updates !!!!
Labels:
Development
hi sir
i think it is so helpful to me.
it is new frame work for me please add some more examples.
thank u sir
Sure Gupta !, Keep visiting my blog, you will get updates !