• Post Reply Bookmark Topic Watch Topic
  • New Topic

Factory Pattern  RSS feed

 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am trying to understand the sequence of the return type PersonFP. Can someone please guide?

Here is the code.




In the PersonFP class, I have the get methods to retrieve the name and event. However, I am not sure what does this, "public PersonFP getPerson(String name, String event)" mean? The return type is PersonFP. So does it mean that it will get the return value and store into the get methods? I am confused here...Please someone guide me.

Thanks!
Best Regards
SK
 
Prasanna Venkatesh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Safian Kishna ,

For me ur input is not clear ...

.

The signature may have significance with the logic used to write the program .Unless until it have document .I will be hard to decode it..

But I can answer ur question by explaning Factory Pattern ..

See we can use the to get an instance of MaleFP class , In more generic term to get instance of PersonFP class .
This is possible because of inheritance .
According to the logic of this program ...

when this line get executed

-Itz constructor get callled
As a result it will displace then supplied..
I hope this helps u.
 
mohit bahl
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Shafian Kisna
I don't think the code that you have provided is complete, that is why i think you are getting confused.

The PersonFP getPerson(String name, String event) method is just checking the event (command line argument) and if it is "K" then it returns a new MaleFP object. Here if you pass both name and event in to your constructor(change the constructor of MaleFP to take 2 arguments) and set these values in name and event instance variables then the get method of the PersonFP class will give the values of these instance variables when you call these get methods.
You will also have to keep the returned value of MaleFP object in your main method if you want to use it further in your program.
Also check your given code for compile time errors.

hope this helps

cheers!
mohit
 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your responses. I am afraid this is the complete code. My confusion is public PersonFP getPerson(String name, String event) -- this method will give a return type of PersonFP, right?
But the class PersonFP contains the given code.


So what does this mean when the class returns another class as the return type?

Thanks for your help.

Best Regards
SK
 
Prasanna Venkatesh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Safia ..
It means that fucntion will return a instance ( means object) of the specified class or some classes in the same hirarchy (MaleFP)..

Regards
Prasanna
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Venkatesh is quite right.
You pass a name, then you pass K for a male, as command-line arguments.

You then give the "K" the misleading title of "event." It is not actually an event. Call the parameter something explanatory, like "sex."

You will have problems with the getPerson method. You have a return value for "K," but you don't have a return value for "L." The compiler will pick that up and throw error. You will have to add a line in your getPerson method, at the end

You have been told about factory methods, but what you have here is not a factory method. If it were a factory method you would pass "MaleFP" to the method and it would work out from that which class to instantiate.

You have also misunderstood the function of a constructor method. A constructor is a method which has no return type, and whose name (identifier) is exactly the same as the name of the class (capitalisation included). What you have inside a constructor is code to set up (and maybe check and verify) the values of the fields to their initial value. So it would be appropriate to have a consrtuctor like this:Only I would call "event" something different.

You ought not to print out from inside your constructor method; have a public void print() method in the PersonFP class.

As Prasanna Venkatesh has already told you, what you are doing is calling a method which creates an object, and sends it back. You can then do anything you want with any of the non-private methods of this object.

As Mohit Bahl has told you, you are not keeping the object in your main method. You have got a PersonFP object there, but have not given it a name (reference). That means you can't use it twice. If you want to use it twice, you will have to change the second line in your main method, fromto
Then, you can add lines like:to your main method and see what happens.

CR
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Step1 :
In PersonFP class

i You can make the class PersonFP to be abstract.
ii Remove the declaration for event and also getEvent() which are not
required.
iii Add the following
- parameterized constructor
public PersonFP(String name) {this.name = name; }
- abstract method declaration display()

In MaleFP class

i Instead of printing. pass the param name to its super class
like super(name);

ii implement the display() method of PersonFP with the body containing
System.out.println(getName()+" is Male.");

Do similar modifications to class FemaleFP also.

In Factory Pattern class
i declare an object 'person' of type 'PersonFP'


ii define methods setPerson and getPerson
public void setPerson(String name, String sex){ .... }
check: if sex is M then instantiate MaleFP(name) and assign to person
else if sex is F then instantiate FemaleFP(name) and assign
to person

public PersonFP getPerson() { .... }
return person object

iii In main method do the following
FactoryPattern fp = new FactoryPattern();
fp.setPerson(args[0], args[1]);
fp.getPerson().display();

I think would help you understand Factory Pattern
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!