package
com.exmpl.templatePattern;
//This is a POJO class for employee.
public class Employee {
private String userName;
private String password;
private String empName;
private int empId;
private String empRole;
public String getUserName()
{
return userName;
}
public void setUserName(String
userName) {
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String
password) {
this.password = password;
}
public String getEmpName()
{
return empName;
}
public void setEmpName(String
empName) {
this.empName = empName;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpRole()
{
return empRole;
}
public void setEmpRole(String
empRole) {
this.empRole = empRole;
}
@Override
public String toString() {
return "Employee
[userName=" + userName + ", password=" + password
+
",
empName=" + empName + ", empId=" + empId + ", empRole="
+
empRole + "]";
}
}
--------------------------------------------------------------------
// This class is responsible for creating a template.
package
com.exmpl.templatePattern;
public abstract class TemplateClass {
public boolean isAuth = false;
// Template method
public void
displayEmployeeDetails() {
initializeEmployeeDetails();
authenticateEmployee();
if(isAuth == true)
performOperation();
}
public abstract void
initializeEmployeeDetails();
public abstract void performOperation();
public abstract void
authenticateEmployee();
}
--------------------------------------------------------------------------
// This class extends template class and define it's abstract method.
package
com.exmpl.templatePattern;
public class EmployeeOne extends TemplateClass {
Employee
emp = new Employee();
@Override
public void initializeEmployeeDetails()
{
emp.setUserName("admin");
emp.setPassword("admin");
emp.setEmpName("Mr.
Stive");
emp.setEmpId(0001);
emp.setEmpRole("Developer");
}
@Override
public void authenticateEmployee() {
if (emp.getUserName().equalsIgnoreCase("admin")
&&
emp.getPassword().equalsIgnoreCase("admin")) {
isAuth = true;
}else{
System.out.println("Invalid user
name and/or password");
}
}
@Override
public void performOperation() {
System.out.println("Employee
details is as follows: "+emp);
}
}
package
com.exmpl.templatePattern;
public class EmployeeTwo extends TemplateClass {
Employee
emp = new Employee();
@Override
public void
initializeEmployeeDetails() {
emp.setUserName("adminadmin");
emp.setPassword("adminadmin");
emp.setEmpName("Mr.
Mark");
emp.setEmpId(0001);
emp.setEmpRole("Manager");
}
@Override
public void
authenticateEmployee() {
if (emp.getUserName().equalsIgnoreCase("admin")
&&
emp.getPassword().equalsIgnoreCase("admin")) {
isAuth = true;
}else{
System.out.println("Invalid user
name and/or password");
}
}
@Override
public void performOperation() {
System.out.println("Employee
details is as follows: "+emp);
}
}
-------------------------------------------------------------------------------
// From this class we are calling template method with different instance.
package
com.exmpl.templatePattern;
public class ClientClass {
public static void main(String[] args)
{
TemplateClass
empOne = new EmployeeOne();
System.out.println("Details of
Employe one: ");
empOne.displayEmployeeDetails();
TemplateClass
empTwo = new EmployeeTwo();
System.out.println("Details of
Employe two: ");
empTwo.displayEmployeeDetails();
}
}
---------------------------------------------------------------------------------
Output ==>
Details of Employe one:
Employee details is as follows: Employee [userName=admin, password=admin, empName=Mr. Stive, empId=1, empRole=Developer]
Details of Employe two:
Invalid user name and/or password