Reputation: 33563
I defined an interface IPersistent
that has two methods load
and save
. Using these methods I can load or save the implementing class using whatever method they want.
public interface IPersistent {
void save();
void load(int id);
}
What I want now, is to implement a deleteAll
method, but it would have to be a static method, as it acts on class level, rather than object level. I tried using a static method in the interface, but that is not possible, as I would need to implement it in the interface, instead of any implementing class.
Typical usage would be something like this:
class Foo implements IPersistent {
void load(int id) { ... }
void save() { ... }
static void deleteAll() { ... }
}
List<foo> fooList = ...;
Foo.deleteAll();
for (Foo f: fooList) {
f.save();
}
How would I implement this?
Upvotes: 4
Views: 219
Reputation: 5140
As static method can't be overridden, your idea is quite complex to implement in Java.
I think about an other way, using generic static method and annotations.
public final class PersitenceTools {
public static <T extends IPersistent> void deleteAll(Class<T> clazz) {
// stuff
}
}
You could use it like this: PersistenceTools.deleteAll(Foo.class)
.
Note: Since Java 8, you can put this method in the IPersistent
interface, if you want to avoid creating a tools class.
You will probably need some information about Foo
(or other IPersistent
object) in deleteAll
. And you can use the power of annotations for this purpose !
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PersitenceInfo {
String info1();
}
Finally, you will be able to decorate Foo
@PeristenceInfo(info1="Foo")
public class Foo implements IPersistent {
// stuff
}
And get this information in deleteAll
through clazz.getAnnotation(PersistenceInfo.class).info1()
.
Upvotes: 2
Reputation: 109613
Follow the java SE convention as seen in:
Path path = Paths.get("...");
File file; ... Files.copy(...);
Collection colxn = ...; Collections.sort(colxn);
In your case that would be a utility class with static methods:
public class Persistents {
private Persistens() {}
public static void deleteAll() { ... }
}
Upvotes: -1
Reputation: 4658
You should have an abstract class instead of an interface, and make all of the classes utilizing deleteAll()
extend that class.
Upvotes: 2