Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Doubt in Design Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Design Pattern "Factory"" Watch "Doubt in Design Pattern "Factory"" New topic
Author

Doubt in Design Pattern "Factory"

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
My concept is not clear in this design pattern, i am unable to understand code in the class with main method, i will be thankful if anyone could help me out

Here are some class for a Pizza Factory
















I have doubt mainly in the functionality of these lines



Whats the point of passing the factory reference in the PizzaStore constructor. Can't the code run without passing the reference ??
Can't we remove this from the PizzaStore class and write the code in PizzaTestDrive class as




Regards
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
I tried doing that but met with the following error

Exception in thread "main" java.lang.NullPointerException
at PizzaStore.orderPizza(PizzaStore.java:9)
at PizzaTestDrive.main(PizzaTestDrive.java:7)

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If you don't pass the factory instance, then you will get the NullPointerException in the below line

since the SimplePizzaFactory reference will be pointing to null
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
Hi,

I'd like to help you, but I even don't get the source compiled, because you are missing the file: CheesePizza.java
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
sorry the remaining classes are






Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

shivang sarawagi wrote:I have doubt mainly in the functionality of these lines



Whats the point of passing the factory reference in the PizzaStore constructor. Can't the code run without passing the reference ??

Well, can you imagine a pizza store without a pizza maker ? No
You need to pass the knowledge of how the cheese, Veg, etc (and its passed in the form of factory.)


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Design Patterns are very intriguing. I love it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
A factory method is often called before any instances exist. Factory methods are therefore usually made static, and called on the name of the class. In the very iffy, unscientific and misleading Campbell Ritchie classification of methods, factory methods are usually 1368.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

What you're seeing here is the separation of concerns - the principle that classes ought to have a single responsibility. Yes, you could put the createPizza logic from SimplePizzaFactory into PizzaStore. And that way PizzaStore wouldn't need to have a factory object passed in. But this design separates the logic of determining which type of pizza to create from the actions that are performed on the pizza to prepare it for delivery.

Where you'd really see the benefit is if the system expanded. Let's say the company opened a pizza restaurant. They'd still want the same logic for selecting a particular sub-type of pizza, but the preparation steps would be different. So you could then create a PizzaRestaurant class and pass it the same SimplePizzaFactory.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Matthew Brown wrote:Where you'd really see the benefit is if the system expanded. Let's say the company opened a pizza restaurant. They'd still want the same logic for selecting a particular sub-type of pizza, but the preparation steps would be different. So you could then create a PizzaRestaurant class and pass it the same SimplePizzaFactory.

and I just had a Pizza in my lunch.. yummy.. and guess whats the factory might get used here, Its IndiaPizzaFactory, they put lots of Paneer on Cheese Pizza
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Paneer??? It’s supposed to be mozarella. But I suppose paneer made from buffalo milk would taste similar.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Campbell Ritchie wrote:But I suppose paneer made from buffalo milk would taste similar.

Exactly, Paneer is something that is discovered in India and we Indian can use it with any food. Pizza is no exception..

I think OP have enough idea now, why we need to pass factory object to pizza store. Given a same setup of Domino's (the PizzaStore class), the way pizza are created here in India (IndiaPizzaFactory class) and that in UK (UKPizzaFactory class) is different..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Three ways to do that, and believe it or not, I can make use of the previous silly banter.You can decide whether you like those methods or not. There is something suspect about requiring a cast, so that might want changing. But you can see the design idea, I hope.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Design Pattern "Factory"