Should I use static initializer or super class

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.


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.


I'd go for a third option

Class ParserA{
    // two maps declaration
    static {
         // maps initialization
    }

    public int ParseA(byte[] data){
        //parse the byte array
     }

    public xxxx getAttr(){

    }
}

The static block is only executed when the class loader loads the class, that only happens once regardless the number of threads. That allows you to initialize the static attributes of each parser.

链接地址: http://www.djcxy.com/p/91410.html

上一篇: Gradle挂起执行任务:[:android:generateDebugSources

下一篇: 我应该使用静态初始化程序还是超类