I have a interface named Parser
. Two classes ParserA, ParserB
implements Parser
.
public interface Parser{
public void initialize();
public int Parse(byte[] data);
}
I have confusion with initialization. ParserA
initializes two Maps
. ParserB
initializes two Maps
. But differentData. Maps
are initialized with constant data. Means not from runtime.
So Should I use approach1 or 2?
Approach1:
class Initializer{
//have two maps as member
}
Class ParserA extents initializer implements Parser{
public int Parse(byte[] data){
}
public void initialize(){
//Initialize those maps
}
}
Similarly for class B
Approach2:
class Constants{
//Static initializer of four maps[two for ParserA, two for ParserB]
}
Class ParserA implements Parser{
public int Parse(byte[] data){
}
public void initialize(){
//Constants.map1 likewise use.
}
}
Similarly for class B
Which is preferred in the above use case?
Q2: I have another utility method in the class Initializer
. Let it be getAttr
which makes use of those two maps. In this scenario, which approach is better?
Q3 If I want multiple threads to use these parsers
and assume I choose approach 1, unnecessary intiailization occurs on each thread. This is what really confusing me.
I am bit more confused.
Assume Animal
is a base class for Tiger, Lion
. Each Animal will have age, numOfLegs
as members. It will make sense to have Animal
class rather than having age, numOfLegs
in each Animal class. So Superclass
wins here. Isn't? If so, My scenario also similar to this I assume.
A Static Initialization Block in Java is a block that runs before the main( ) method in Java. Java does not care if this block is written after the main( ) method or before the main( ) method, it will be executed before the main method( ) regardless.
The reason for this is simple: All objects with static storage duration must be initialized (set to their initial values) before execution of main() starts. So a value which is not known at translation time cannot be used for initialization of static variables.
The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code. Note: We use Initializer Block in Java if we want to execute a fragment of code for every object which is seen widely in enterprising industries in development.
Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. We can inherit static methods in Java.
Since you are asking for opinion, here is my 2 cents:
Both the approaches seem unnecessary. The basic idea of the Parser
interface itself seems a little wrong. If the parsers are going to be statically initialized, why do you expect the users of the parser to call initialize
method? What would the program do if they don't call initialize
and use parse
method?
The purpose of the Parser
interface is to parse a byte[]. Individual implementations should initialize themselves with whatever the logic they want. So, I would suggest that, you remove the initialize method in Parser
and have the ParserA
and ParserB
initialize it with the Maps (or whatever they might need) when they are created.
Something like:
public interface Parser {
int parser(byte[] data);
}
public class ParserA implements Praser {
public ParserA() {
//initialize the maps they use or whatever the data structure that is needed
}
public int parser(byte[] data) {
//logic of parsing
}
}
//similarly for ParserB.
For your Q2: If you don't expose the internal DS and the state of the DS is immutable these Parsers can be shared with multiple threads without any issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With