I have two classes for records: saving to the database and saving to a file. Both classes implement the RecordsSaver interface:

 public interface RecordsSaver { public void saveRecond(); } 

The problem is that each of the classes should be required to implement the static generation method getRecordsSaver . However, if the interface implements such a static method, it can no longer be overridden in a class, similarly to abstract classes.

Tell me how to either override the method, or is there any other way out of this situation?

  • one
    And the use of all this looks like yours? FileRecordSaver.getRecordSaver( record ).saveRecord() ? - zRrr
  • How about looking towards the factory design pattern? - cache
  • @cache, even if you go this way (it even seems to me to be somewhat preferable, the problem remains the same). - user189127
  • @zRrr, well ... yes :). I came to a more correct decision, now I will write back. - user189127

1 answer 1

I was a little wrong in the idea of ​​working with the database (I wanted to add files to be available from everywhere).

The most correct option was not to use static methods in the database, but to create a class that manages the work with the database. As a result, there is no need to try to make a single database object (ie, a factory method or static internal variables), since only one instance will be created in the control class. Now all the code refers to the control class. And the main advantage that I have learned from this is that when the work with the database changes, only one class changes.