java - How should I implement a singleton class if I need to use method overloading in its construction? -


i'm trying implement effect:

  1. base class has method getfilename()
  2. two derived classes b , c have overridden implementations of method, returning file names specific b , c.
  3. class needs use services of singleton class s
    • the reason want singleton because (a) want guarantee constructed once , (b) want eager initialization of class, happens @ app startup, , not @ first use.
  4. class s needs work work based on file name (e.g., read in contents of file) - depends on of a's subclasses used.

this seems present unavoidable conundrum, because:

  • most implementations of singleton static based (pure static class; or enum static parameter passing)

  • static classes/methods/blocks cannot call non-static methods...

  • ... , making getfilename() static make sure cannot use inheritance overrides!

how can implement design? (i'm open changing design if better pattern available)

... needs use services of singleton ... depends on of a's subclasses used:

that means singleton not problem, acquisition of correct class based on type asking!

your design tightly coupled way trying it. need decouple service consumers of service, singleton not important in exercise.

what need form of dependency injection.

this type of problem guice created solve being able provide classes injected based on classes type in binding. said ...

most people not realize java has supported di via constructor. guice makes less hard coded, still dependency injected instance.

guice make trivial injecting correct service based on class type. can done without di framework/library. if using guice considered heavy handed case can still done easily.

below 1 way without framework/library:

public class solution {     static class singleton     {         public static final singleton instance;         static { instance = new singleton(); }          private singleton() { /* important */ }         public void dowhatever(@nonnull final b b) { /* whatever */ }         public void dowhatever(@nonnull final c c) { /* whatever */ }     }      static abstract class     {         private final singleton s;          public a(final singleton s) { this.s = s; }          public abstract string getfilename();     }      static class b extends     {         public b(final singleton s) { super(s); }          @override         public string getfilename() { /* code goes here */ }     }      static class c extends     {         public c(final singleton s) { super(s); }          @override         public string getfilename() { /* code goes here */ }     } } 

the singleton anti-patterns mention that:

the singleton pattern should hidden behind factory pattern. consumers of needs have 1 , 1 should not care if there 1 , 1. should care that object conforms contract of interface.

my implementation naive factory create in static block. create on first use not better.

using enum create singleton objects misuse of semantics of enum , anti-pattern , impossible unit test.

same all static utility class approach, impossible unit test or replace different implementation. a combination of 2 complete abomination impossible unit test , complete nightmare maintain!

how determine subclass of a singleton works on easy:

that overloading shown in code above.

anything else not doing right. instanceof fail, reflection bigger fail.

selecting logic based on type can done overloading methods, or generics or appropriate design pattern.

strategy pattern account , make n number of subclasses manageable , extensible @ runtime.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -