I have an interface like this:
public interface Byteable<T> {
byte[] toBytes();
T fromBytes(byte[] bytes);
}
which, like the name implies, transforms an object to a byte pattern and can recreate an object from a given byte-array (or throw some kind of exception if not). I like the toBytes() method and it must be non-static, because it can only be applied to objects. But how can I create a static method so that I can call
ClassName.fromBytes(...);
How can I ensure, that every class implementing the interface has both of those functions or how can I create a static method that uses the non-static fromBytes(...)
? Is it good practice to create a default constructor and the do something like
new ClassName().fromBytes();
Is there any good practice? I guess an abstract class isn't a solution either, because it can not have static methods, can it?
you can create an abstract class that will contain your both method as abstract and you will extend that abstract class in your desired class.
public abstract class Byteable<T> {
abstract byte[] toBytes();
abstract T fromBytes(byte[] bytes);
}
public YourClass extends Byteable<YourClass>{
//provide implementation for abstract methods
public byte[] toBytes(){
//write your conversion code
}
public YourClass fromBytes(byte[] bytes){
//write your conversion code
}
}
Now you can create an object of your class and use both the methods.
YourClass obj = new YourClass();
obj.toBytes()
obj.fromBytes(....)
And you can do the same with any desirable class just need to implement or give functionality to class to perform these tasks.
Other than this you can write one generic transform class :
public class Byteable<I,O> {
O toBytes(I obj){
// Your transformation code
}
T fromBytes(O bytes){
// Your implementation
}
}
Byteable<YourClass,byte[]> transformer = new Byteable<YourClass,byte[]>();
transformer.toBytes(new YourClass())
transformer.fromBytes(....);
I hope it's helpful for you.