安卓Java接口方法数过多

最近使用SDK的接口很烦恼,接口方法数量达30之多,眼花缭乱找都找不到需要的方法,总不能每次都去查文档

开始思考如何对接口分类管理

目前处理方式自定义一个管理类,默认实现所有接口方法,自身再声明几个管理的接口对其方法进行分类,只要在同名方法内调用自己声明的接口方法即可

以下为别人写死的SDK和Callback

public class HumanSDK{

...

setCallback(HumanCallback humanCallback);

removeCallback(HumanCallback humCallback);

...

}

pulic interface HumanCallback{

void eatfood1();

void eatfood2();

void eatfood3();

void drink1();

void drink2();

void drink3();

}

接口管理类

public class Human implements HumanCallback{

public static Human human;

public EatfoodCallback eatfoodCallback;

public DrinkCallback drinkCallback;

public Human setEatfoodCallback(EatfoodCallback eatfoodCallback){

this.eatfoodCallback=eatfoodCallback;

return this;

}

public Human setDrinkCallback(DrinkCallback drinkCallback){

this.drinkCallback=drinkCallback;

return this;

}

private Human(){};

public static Human getInstance(){

if(human==null){

human=new Human();

}

this.drinkCallback=null;

this.eatfoodCallback=null;

return human;

}

public interface EatfoodCallback{

void eatfood1();

void eatfood2();

void eatfood3();

}

public interface DrinkCallback{

void drink1();

void drink2();

void drink3();

}

@Override

public void eatfood1(){

if(eatfoodCallback!=null){

eatfoodCallback.eatfood1();

}

}

@Override

public void eatfood2(){

if(eatfoodCallback!=null){

eatfoodCallback.eatfood2();

}

}

@Override

public void eatfood3(){

if(eatfoodCallback!=null){

eatfoodCallback.eatfood3();

}

}

@Override

public void drink1(){

if(drinkCallback!=null){

drinkCallback.drink1();

}

}

@Override

public void drink2(){

if(drinkCallback!=null){

drinkCallback.drink2();

}

}

@Override

public void drink3(){

if(drinkCallback!=null){

drinkCallback.drink3();

}

}

}

 

原接口调用

HumanSDK.setCallback(new HumanCallback(){

@Override

public void eatfood1();

@Override

public void eatfood2();

@Override

public void eatfood3();

@Override

public void drink1();

@Override

public void drink2();

@Override

public void drink3();

});

修改后

HumanSDK.setCallback(Human.getInstance().setEatfoodCallback(new Human.EatfoodCallback(){

@Override

public void eatfood1();

@Override

public void eatfood2();

@Override

public void eatfood3();

})

.setDrinkCallback(new Human.DrinkCallback(){

@Override

public void drink1();

@Override

public void drink2();

@Override

public void drink3();

})

);

主要通过自己的接口去实现他写死的接口来进行管理

上一篇:Human vs AI,人类和机器的学习究竟谁更胜一筹?


下一篇:抢占式协程调度新语言