• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Generic builder pattern with abstract class

 
Greenhorn
Posts: 9
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have two classes that inherit abstract class and implement interface. In abstract class I have variables common for both implementation. I also need to construct classes using same builder, which is generic and static in abstract class. Unfortunately it's impossible in Java to call constructor for generic parameter. I want to avoid making specific builder for all implementation. Is there some way to do this?

It looks like this (this is just example but shows what is my problem).




 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid that wouldn't be possible. But suppose even if it was possible, and you created `Builder` instance of super class. Then how would you set attributes for subclasses? Wouldn't be possible right? Only Builder class specific to subclass will do it.

Also, if your super class is abstract, you should also make your super `Builder` class abstract. Else, which object would you expect it's `build()` method to create?
 
Saloon Keeper
Posts: 15252
349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
R. Jain, that's not completely true. It would probably be possible through reflection, and the Builder class doesn't need to be abstract. The object created would be determined by the type parameter.

This would however be a completely messy, unnecessary and fragile solution though.

Alex, I would really discourage you from doing this. Why do you need a builder to create subtypes?
 
Alex Miel
Greenhorn
Posts: 9
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm aware of reflection and I'd rather not to use them. I need common builder, because sub classes have the same variables but different implementation of interface. I simply don't want to duplicate code. I was also thinking about one subclass without abstract class between and strategy pattern for different implementation.
 
Stephan van Hulst
Saloon Keeper
Posts: 15252
349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use composition over inheritance:

You should always be wary of extending classes.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Miel wrote:I have two classes that inherit abstract class and implement interface. In abstract class I have variables common for both implementation. I also need to construct classes using same builder, which is generic and static in abstract class. Unfortunately it's impossible in Java to call constructor for generic parameter. I want to avoid making specific builder for all implementation. Is there some way to do this?


Yes but, like the others, I suspect it's not what you want.

1. Builders are usually used to create complex objects - especially ones that have many "default" values - not generic ones.
2. If the subtypes have identical contents, why are they subtypes? You may have a reason, but I think you need to explain it.

What is possible is that your Bar class constructor takes a Builder<? extends Bar> and uses it to initialize its fields, while the subclass has something like a make(Builder<SubClass>) method (defined as abstract in Bar) that uses a "dummy" constant of its own type. Your generic Builder could then call:
return T.make(this);
in its build() method. But it's quite tortuous, and I suspect there's a more elegant solution.

Winston
 
There is no "i" in denial. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic