• 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

Factory methods

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While googling for Factory Methods, I found that it is used to instantiate a class, and also the below piece of code.(on javafactory.com)


Still I could not understand the use of factory methods.I feel, rather than using them, if we just remove the private access modifier from the constructor, it will serve the porpose.
I know I am missing soemthing. Can you pls help me to find out the actual use of Factory Methods?
edit:I wanted to know about them when I was studynig Calendar,DateFormat classes from K&B.
[ November 27, 2007: Message edited by: Varalakshmi Ramanarayan ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The factory pattern is used to separate the bit of code that instantiates a concrete class based on some input at runtime!

For example, below a guitar object is created based on the type of music it is best suited to play (which is provided as input).



Advocates of factory pattern would move the above part to a separate class. Why? because this bit of code is a good candidate to [bold]change[/bold]! If you create a new ElectricGuitar class which is suited to play "rock", the above class will have to be modified.

Using the factory pattern:


Now you have contained the change in a separate class. Hope it helps.

Vic
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Hercule Poirot", please sleuth your way to your private messages and read the one I just sent you.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some information

Creational Patterns - Factory Pattern

Factory of what? Of classes. In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.

Let�s take an example to understand this pattern.

Example: Let�s suppose an application asks for entering the name and sex of a person. If the sex is Male (M), it displays welcome message saying Hello Mr. <Name> and if the sex is Female (F), it displays message saying Hello Ms <Name>.

The skeleton of the code can be given here.
public class Person {
// name string
public String name;
// gender : M or F
private String gender;

public String getName() {
return name;
}

public String getGender() {
return gender;
}
}// End of class

This is a simple class Person having methods for name and gender. Now, we will have two sub-classes, Male and Female which will print the welcome message on the screen.
public class Male extends Person {
public Male(String fullName) {
System.out.println("Hello Mr. "+fullName);
}
}// End of class

Also, the class Female
public class Female extends Person {
public Female(String fullNname) {
System.out.println("Hello Ms. "+fullNname);
}
}// End of class

Now, we have to create a client, or a SalutationFactory which will return the welcome message depending on the data provided.
public class SalutationFactory {
public static void main(String args[]) {
SalutationFactory factory = new SalutationFactory();
factory.getPerson(args[0], args[1]);
}

public Person getPerson(String name, String gender) {
if (gender.equals("M"))
return new Male(name);
else if(gender.equals("F"))
return new Female(name);
else
return null;
}
}// End of class

This class accepts two arguments from the system at runtime and prints the names.

Hope this help
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varalakshmi,

I was pondering this very same question a few days back. You might have a look at this older thread here. Perhaps it will give you some additional general ideas.

Also, I am thinking that our questions regarding any Software Design Patterns might be a little out of scope for this SCJP exam forum. I'm new here as well, but I'm guessing that maybe the Java Intermediate forum might be a place to ask.

Cheers,
Marcus
 
Varalakshmi Ramanarayan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you..
It helped me..
reply
    Bookmark Topic Watch Topic
  • New Topic