• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can this be called using Factory design pattern ?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have a super abstract class "Employee" and two subclasses "Worker" and "Manager" that implement some functions defined in "Employee".
I have a "EmployeeFactory" class to generate an instance of either "Worker" or "Manager". it simply takes a parameter variable "title" and do
*******************
public Employee get_Instance(String title) {
Employee e;
if(title.equals("manager"))
e = new Manager();
if(title.equals("worker"))
e = new Worker();
return e;
}
************************
Can this be called using Factory pattern ? does the method need to handle the case when 'title" is any sort of strange input ?
Thanks,
yang
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ian,
1) You didn't mention whether EmployeeFactory is abstract (which is one of the requirements of the pattern).
2) Also, I believe you'll need two concrete class WorkerFactory and ManagerFactory, both subclassing EmployeeFactory.
Thus you client may look something like this:

3) If somebody passes in some unknown constant, it's better to throw an exception.
Cheers,
Pho
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pho, what you describe, is called the Abstract Factory Pattern, I think.
For a plain Factory Pattern, Ian's code seems ok, apart from the fact that getInstance should throw an exception, like you suggested, if the argument is unknown.
The Factory Pattern returns Instances of an abstract type or interface and decides on the actual type from the argument passed to the factory method.
Greetings, Chris
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Christel,
You are correct. My apologies for the mix-up.
Thanks
Pho
reply
    Bookmark Topic Watch Topic
  • New Topic