• 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

What's the best creational pattern?

 
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I managed to do Builder Design with subclasses but the constructors are public and every concrete class has a Builder class so all I'm doing is not using constructors. My mentor told me that I can use the Factory Design Pattern but I can't figure out how to initialize the additional field of the child classes. Is this doable? and if yes, how?

Here are a couple of the classes related to the problem





So If I have let's say a PersonFactory how do I initialize the additional fields when getting a Client object from the factory? If I add ALL the field in the factory for all my classes the code is prone to throw NullPointerException :/
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darko Jakimovski wrote:So If I have let's say a PersonFactory how do I initialize the additional fields when getting a Client object from the factory?


Why on earth would you want to do that? A superclass should have no knowledge whatsoever of any specific subclasses. Any code in a superclass that references a specific subclass is a code smell that stinks to high heaven. It means there's something terribly wrong with your inheritance design.

*Edited: was "should have knowledge" -- corrected to "should have no knowledge" (Thanks, Ron, for the heads up)
 
Darko Jakimovski
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I clearly formulated my question wrong. Let's say that we have an interface called Shape, and a void draw() method, three classes implement the Shape interface: Circle, Square and Triangle from there we have a class called ShapeFactory which has a method getShape(Shape shape) that has an Enum parameter and based on the given argument the method returns an object of the corresponding type. My question is, how do I initialize any additional fields of a subclass object in this case? Is that even possible?
 
Bartender
Posts: 1359
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darko Jakimovski wrote:I clearly formulated my question wrong. Let's say that we have an interface called Shape, and a void draw() method, three classes implement the Shape interface: Circle, Square and Triangle from there we have a class called ShapeFactory which has a method getShape(Shape shape) that has an Enum parameter and based on the given argument the method returns an object of the corresponding type. My question is, how do I initialize any additional fields of a subclass object in this case? Is that even possible?



Not sure if a good idea, but you could try to create a factory like this:


and try to use proper setter methods on concrete class iterating over props parameter values. For example for a circle you may have in props a property 'radius' and you should try to invoke a setRadius method.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Junilu has said, the Shape interface doesn't “know” how many classes implement it. What is going to happen when somebody creates an Ellipse class?
The only way you can have a superclass “know” how many subclasses it has is to give the superclass only private constructors, and have several subclasses all being nested classes. You can use factory methods to create instances of those subclasses. See Joshua Bloch Effective Java pages 5‑16 (2/e (2008) or 3/e (2017)).
 
Darko Jakimovski
Ranch Hand
Posts: 60
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell, you saved the day! I found everything I needed in that book! Cheers man, you are a beast!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darko Jakimovski wrote:Thanks . . . everything I needed in that book! . . .

That's a pleasure And isn't that a good book!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic