How to find type without using instanceof?

后端 未结 7 1780
予麋鹿
予麋鹿 2020-12-19 19:58

I have a List of interface type Criteria within my class Query.

List criteria = new ArrayList

        
7条回答
  •  别那么骄傲
    2020-12-19 20:35

    An interface is halfway to the strategy pattern! To vary the logic based on type, push it behind the interface if possible, such that Criteria has a doLogic(). You can pass that method whatever parameters you might need to alter in the calling code, or return new information - that is very implmentation specific and hard to advice on from the code in question.

    If all goes well, your calling code ends up

    for (Criteria c : criteria) {
        c.doLogic();
    }
    

提交回复
热议问题