• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Design Pattern "Factory"

 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the remaining classes are






 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Design Patterns are very intriguing. I love it
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paneer??? It’s supposed to be mozarella. But I suppose paneer made from buffalo milk would taste similar.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic