This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Builder pattern basic question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In bruce eckel book, builder pattern returns a product which can be Book or Magazine or WebSite . all having a parent Media Type. This makes sense to me.
So in builder class, return type points to Media (parent) rather than specific child class like Book or Magazine or WebSite
when i was trying to do a sample excercise, in which one Builder that puts the words into a java.util.TreeSet, and another that produces a word count to java.util.HashMap
my question:
One builder returns TreeSet , another builder retruns HashMap
how do I map to a common base product. I want to do something similar to
Media -- Magazine, Web, Book. where Media is shown as Return type in each Builder class
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can perhaps use instanceof operation to infer what the runtime type is for each object in the returned collection.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this topic to OO, Patterns, UML and Refactoring forum....
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Introducing a common base type only makes sense if the product needs to be processed by common code. In that case what it should look like would heavily depend on how it gets used. Otherwise, you could simply declare Object as return type or even don't declare a common query method at all.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!