Java教程

安卓Java接口方法数过多

本文主要是介绍安卓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();

})

);

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

这篇关于安卓Java接口方法数过多的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!