This pattern was applied in Functionalities_Employee.java
, to solve the problem of multiple methods, with the same purpose, accumulated in the same class. Enabling the system to be swapped between Salaried, Commissioned and Hourly methods. Lastly, decrease the amount of conditional functions in the Main.java
.
This pattern was applied to the following code fragment:
for(index = 0;index < 'employetypeArrayList'.size();index++)
{
'employeetype' = 'employeetypeArrayList'.get(index);
if(employee.getId() == id)
{
...
}
}
Who was present in the following methods of the class Functionalities_Employee.java
:
public void Remove_employee(int id)
public void Change_employee(int id)
public void Add_service(int id)
public void Add_sale(int id,int day)
public void Add_timecard(int id,int day)
To group this fragments of code and turn them in a method called: private int SearchEmployee(int id)
.
private int SearchEmployee(int id)
{
int index,index_in = -1;
for(index = 0;index < 'employeetypeArraylist'.size();index++)
{
'employeetype' = 'employeetypeArraylist'.get(index);
if('employeetype'.getId() == id)
{
index_in = index;
break;
}
}
return index_in;
}
This new method is present in CommissionedStrategy.java
, HourlyStrategy.java
and SalariedStrategy.java
. So we can simple invoke this method when we need to search an employee.
This pattern was applied in public void add_timecard(ArrayList<Hourly> hourlies, int id, int day)
to turn this method into a smaller and simple one,public void Add_timecard(int id,int day)
, who can simple invoke two new created methods: private void Check_In(int index,int day)
and private void Check_Out(int index,int day)
.