Fragment 通信原则
两个fragment不应该直接通信(two fragments should never communicate directly)
常见的通信
1. eventBus rxBus
优点:
方便,快捷,简单
缺陷:
2.handler
缺点:
3.广播
缺陷
定义万能通信
我们要达到的效果
我们可以看到function都有一个对应的名字
function 对应的功能
image.png
根据上图我们定义
首先我们的function需要一个名字
public abstract class Function {
public String mFunctionName;
public Function(String functionName) {
this.mFunctionName = functionName;
}
}
函数名字定义好了,我们来定义对应的功能
1. 无参,无返回值
public abstract class FunctionNoParamNoResult extends Function{
public FunctionNoParamNoResult(String functionName) {
super(functionName);
}
public abstract void function();
}
2. 有参,无返回值
public abstract class FunctionWithParamOnly extends Function {
public FunctionWithParamOnly(String functionName) {
super(functionName);
}
public abstract void function(Param param);
}
3. 有返回值,无参
public abstract class FunctionWithResultOnly extends Function {
public FunctionWithResultOnly(String functionName) {
super(functionName);
}
public abstract Result function();
}
4. 有参,有返回值
public abstract class FunctionWithParamAndResult extends Function {
public FunctionWithParamAndResult(String functionName) {
super(functionName);
}
public abstract Result function(Param param);
}
定义了这么多,我们需要一个定义一个管理类
FunctionManager
public class FunctionManager {
private static FunctionManager ourInstance;
public static FunctionManager getInstance() {
if (ourInstance == null) {
return ourInstance = new FunctionManager();
}
return ourInstance;
}
private FunctionManager() {
mFunctionNoParamNoResultHashMap = new HashMap<>();
mFunctionWithParamOnlyHashMap = new HashMap<>();
mFunctionWithResultOnlyHashMap = new HashMap<>();
mFunctionWithParamAndResultHashMap = new HashMap<>();
}
private HashMap mFunctionNoParamNoResultHashMap;
private HashMap mFunctionWithParamOnlyHashMap;
private HashMap mFunctionWithResultOnlyHashMap;
private HashMap mFunctionWithParamAndResultHashMap;
public FunctionManager addFunction(FunctionNoParamNoResult function) {
mFunctionNoParamNoResultHashMap.put(function.mFunctionName, function);
return this;
}
public void invokeFunction(String functionNmae) {
if (TextUtils.isEmpty(functionNmae)) {
return;
}
if (mFunctionNoParamNoResultHashMap != null) {
FunctionNoParamNoResult f = mFunctionNoParamNoResultHashMap.get(functionNmae);
if (f != null) {
f.function();
}
if (f == null) {
try {
throw new FunctionException(" has no this function:" + functionNmae);
} catch (FunctionException e) {
e.printStackTrace();
}
}
}
}
public FunctionManager addFunction(FunctionWithParamOnly function) {
mFunctionWithParamOnlyHashMap.put(function.mFunctionName, function);
return this;
}
public void invokeFunction(String functionNmae,Param param) {
if (TextUtils.isEmpty(functionNmae)) {
return;
}
if (mFunctionWithParamOnlyHashMap != null) {
FunctionWithParamOnly f = mFunctionWithParamOnlyHashMap.get(functionNmae);
if (f != null) {
f.function(param);
} else {
try {
throw new FunctionException(" has no this function:" + functionNmae);
} catch (FunctionException e) {
e.printStackTrace();
}
}
}
}
public FunctionManager addFunction(FunctionWithResultOnly function) {
mFunctionWithResultOnlyHashMap.put(function.mFunctionName, function);
return this;
}
public Result invokeFunction(String functionNmae, Class c) {
if (TextUtils.isEmpty(functionNmae)) {
return null;
}
if (mFunctionWithParamOnlyHashMap != null) {
FunctionWithResultOnly f = mFunctionWithResultOnlyHashMap.get(functionNmae);
if (f != null) {
if (c != null) {
return c.cast(f.function());
} else {
return (Result) f.function();
}
} else {
try {
throw new FunctionException(" has no this function:" + functionNmae);
} catch (FunctionException e) {
e.printStackTrace();
}
}
}
return null;
}
public FunctionManager addFunction(FunctionWithParamAndResult function) {
mFunctionWithParamAndResultHashMap.put(function.mFunctionName, function);
return this;
}
public Result invokeFunction(String functionNmae,Param param, Class c) {
if (TextUtils.isEmpty(functionNmae)) {
return null;
}
if (mFunctionWithParamOnlyHashMap != null) {
FunctionWithParamAndResult f = mFunctionWithParamAndResultHashMap.get(functionNmae);
if (f != null) {
if (c != null) {
return c.cast(f.function(param));
} else {
return (Result) f.function(param);
}
} else {
try {
throw new FunctionException(" has no this function:" + functionNmae);
} catch (FunctionException e) {
e.printStackTrace();
}
}
}
return null;
}
}
FunctionException
public class FunctionException extends Exception {
public FunctionException(String exceptionStr) {
super(exceptionStr);
}
}
调用
A.class
public static final String functionName = A.class.getName();
public void initFunction() {
//注册一个 无参function
FunctionManager.getInstance().addFunction(new FunctionNoParamNoResult(functionName) {
@Override
public void function() {
}
});
//注册一个有参function 并且定义参数类型为String(任意类型)
FunctionManager.getInstance().addFunction(new FunctionWithParamOnly(functionName) {
@Override
public void function(String str) {
//虽然我收到了你的参数,但是我是不会给你结果的
}
});
//注册一个无参,有返回值的function,返回值String(任意类型)
FunctionManager.getInstance().addFunction(new FunctionWithResultOnly(functionName) {
@Override
public String function() {
return "我会返回给你一个结果,亲!";
}
});
//注册一个有参String(任意类型),有返回值String(任意类型)
FunctionManager.getInstance().addFunction(new FunctionWithParamAndResult(functionName) {
@Override
public String function(String str) {
return "你给我一个参数我给你一个结果";
}
});
}
B.class
发送消息到A.class
public void post() {
//发送消息给A.class 无参的function
FunctionManager.getInstance().invokeFunction(A.functionName);
//发送消息给A.class 有参的function
FunctionManager.getInstance().invokeFunction(A.functionName, "我是一个String类型参数");
//发送一个消息给A.class 有返回结果的
String result = FunctionManager.getInstance().invokeFunction(A.functionName, String.class);
//发送一个消息 给A.class 有参数,有返回结果
String resultAndParam = FunctionManager.getInstance().invokeFunction(A.functionName, "我是有参数,有返回结果的哦", String.class);
}