• 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

Template Method/Singleton with Factory

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am putting together an abstract Singelton, that will then have implementing classes. These classes should only need to override one method(Hence the Template Method) so that it all works for the specific implementation type.

So I have this abstract class



And here is an implementation class



Forget about the implementation of the loadData method, it is using Hibernate to do a search for me.

But notice the static initializer.

OK, so you might guess what I am trying to do, but I am having no luck. I am just missing something that probably is pretty easy.

I am trying to make a call to getInstance of the object, and it will return the Singleton instance. Now the class that is calling getInstance is doing so through Reflection because that class only knows its type as the abstract class type.

Here is my CacheFactory that will either return an instance of that type that exists in a Map it has, or will create a new one and add it to the Map.



The problem right now is that the loadLookup method calling the getInstance static method is returning null.

I obviously want to make sure that when you call getInstance on any type of AbstractLookup and there is no instance of that type yet, that it will create one, and call loadData().

What I am oblivious to?

Thanks

Mark
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know if its a typo on your part but I dont see a 'static' block though in DiscountTypeLookup

 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even with it changed to a static block it won't work. The problem here is the DefaultLookup class is never loaded, the only method called belongs to AbstractLookup. Even it were loaded there's still a fatal flaw in the design: each new class loaded uses the same static variable.

I believe you'll need to create a registry or make getInstance() abstract and move the static variable to the implementing class.



Or a registry.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic