Reputation: 505
I'm writing a small program in Java for an assignment in a OOAD class and I have a problem understanding exactly what constitutes a proper factory design pattern and how to implement it in my program.
It's a small program for payroll calculation. There is a base class called Employee and three derived (sub) classes called Hourly, Commissioned and Salaried. They each have different types of data types for payments. I'm only allowed to instantiate one object in the main class, and in the whole program. Through Polymorphism the objects should be shared between classes and I am to write a Factory method to create the objects to be assigned to THE Object.
I have the base class:
public class Employee {
protected Employee empFactory(int empType){
if (empType == 1)
return new Hourly();
if (empType == 2)
return new Commissioned();
if (empType == 3)
return new Salaried();
else
return null;
}
public static void main(String[] args) {
// TODO code application logic here
}
And three derived classes:
public class Commissioned extends Employee
public class Hourly extends Employee
public class Salaried extends Employee
I've left out some of the datatypes and other details, I think this should be enough info. Is the factory method I have a "real" one? I've been reading about abstract and concrete factory methods and concrete products etc, but I'm still confused, don't know how to implement it. Any help or pointers would be greatly appreciated!
Upvotes: 3
Views: 3336
Reputation: 625
Please find below example which describe factory pattern.
interface vehicle {
void start();
void stop();
}
class car implements vehicle {
public void start() {
System.out.println("Start car");
}
public void stop() {
System.out.println("Stop car");
}
}
class bike implements vehicle {
public void start() {
System.out.println("Start bike");
}
public void stop() {
System.out.println("Stop bike");
}
}
class factory {
public static vehicle getVehicle(int ch) { //(?)sould be interface type
vehicle v = null;//local always inialize otherwise error
//car c = new car();
//bike b = new bike();
if(ch == 1) {
v = new car();
} else {
v = new bike();
}
return v;
}
}
class factorymain {
public static void main(String[] args) {
vehicle v;
v = factory.getVehicle(Integer.parseInt(args[0]));
v.start();
v.stop();
}
}
Upvotes: 0
Reputation: 137727
You've almost got a factory method there, but not quite. The problem is that the empFactory
is not static, which means that to make an Employee with it right now requires an existing Employee. Change it to static, and you've taken the first step into the Factory pattern.
The next step is to delegate the logic of the empFactory
method to a separate class, a factory class. That enables a great deal more complexity (without exposing it to the caller) and is the whole core of the Factory pattern.
Upvotes: 4
Reputation: 1
Assuming you know generics you can do a much better factory method that allows you to construct any object of a subclass of Employee.
public static <T extends Employee> T empFactory(Class<T> clazz) {
try {
return clazz.newInstance();
}
catch(Exception e){
return null;
}
}
And a quick example:
public static void main(String[] args) {
Employee emp = Employee.empFactory(Hourly.class);
System.out.println("The salary is " + emp.getSalary());
}
This allows you to instantiate any Employee derived class without having to change the factory method every time you add a new derived class. It also frees you from the ugly (int, derived class) association because you can instantiate an object by simply specifying the derived class you want.
You can also cast the instance if you need to access class specific methods:
Hourly hh = (Hourly) emp;
System.out.println("Salary is " + hh.getSalaryH());
Note: My Employee class has a getSalary() method and my Hourly class has a getSalaryH() method.
Upvotes: 0