Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Builder pattern basic question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this topic to OO, Patterns, UML and Refactoring forum....
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic