dimanche 14 juillet 2019

What is the pattern used in this class?

I wonder what is the patten in this code.

I was analyzing a library called web3j.

Here is the code for this source:

public interface FilterTopic<T>
{
    @JsonValue
    T getValue();
}

public static class SingleTopic implements FilterTopic<String>
{
    private String topic;

    public SingleTopic()
    {
        this.topic = null;
    }
    public SingleTopic(String topic)
    {
        this.topic = topic;
    }

    @Override
    public String getValue() {
        // TODO Auto-generated method stub
        return topic;
    }

}

public static class ListTopic implements FilterTopic<List<SingleTopic>>
{
    private List<SingleTopic> topics;

    public ListTopic(String… optionalTopics)
    {
        topics = new ArrayList<>();
        for(String topic : optionalTopics)
        {
            if(topic != null) topics.add(new SingleTopic(topic));
            else topics.add(new SingleTopic());
        }
    }

    @Override
    public List<SingleTopic> getValue() {
        // TODO Auto-generated method stub
        return topics;
    }   
}

You can see the FilterTopic interface. And it has several return values using the static class. What pattern is this like?

If you want to see the full code, look here https://github.com/KoangHoYeom/Ethereum-JSONRPC-With-Java-Ex/blob/master/src/main/java/org/BlockChainService/domain/dto/Filter.java

Thank you for reading!

Aucun commentaire:

Enregistrer un commentaire