Should I use a static initializer or superclass

I have an interface called Parser . Two classes ParserA, ParserB implement Parser .

 public interface Parser{ public void initialize(); public int Parse(byte[] data); } 

I have a confusion with initialization. ParserA initializes two Maps . ParserB initializes two Maps . But differentData. Maps initialized with persistent data. The tool is not on time to run.

Therefore Should I use approach 1 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 preferable in the above use case?

Q2: I have another utility method in the Initializer class. Let it be getAttr that uses these two mappings. In this scenario, which approach is better?

Q3 If I want multiple threads to use these parsers and suppose I choose approach 1, unnecessary intiailization occurs in each thread. This is what really baffles me.

I'm a little confused.

Suppose Animal is the base class for Tiger, Lion . Each Animal will have age, numOfLegs as members. It makes sense to have an Animal class, not age, numOfLegs in every Animal class. So, Superclass wins here. Is not it so? If so, my script also looks like this, I guess.

+7
java
source share
2 answers

Since you are asking for opinions, here are my 2 cents:

Both approaches seem unnecessary. The basic idea behind the Parser interface seems a bit wrong. If parsers are statically initialized, why do you expect parser users to call the initialize method? What will the program do if they do not call initialize and use the parse method?

The purpose of the Parser interface is to parse byte []. Individual implementations must initialize themselves with whatever logic they want. So, I would suggest that you delete the initialize method in Parser and initialize it with ParserB using 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 do not expose the internal DS, and the state of the DS is unchanged, these Parsers can be shared between multiple threads without any problems.

+2
source share

I would choose the third option

 Class ParserA{ // two maps declaration static { // maps initialization } public int ParseA(byte[] data){ //parse the byte array } public xxxx getAttr(){ } } 

A static block is executed only when the class loader loads the class, which is executed only once, regardless of the number of threads. This allows you to initialize the static attributes of each parser.

+2
source share

All Articles