Look at the renderings first

1. What is an interface

Interface: A structured approach to separating methods from implementation A completely decoupled approach is also the way to implement multiple inheritance in the Java language

2. Why abstract interfaces

Fragments of the communication

3. Why use Fragment

Fragment is created on android2.3

  • 1. Fast (this is to solve the slow Activity)
  • 2. Easy management (fragmentManager reuse is high)
  • 3. Low coupling degree

    4. Communication between Activities and Fragments

    1> Handler communication mode

    Advantages:

  • Solve problems and realize communication disadvantages:
  • 1. High coupling (holding objects)
  • 2. Failed to get a return value from the Activity
  • 3. Memory leaks

2> Advantages of broadcast communication:

  • 1. Problem solver
  • 2. Low coupling degree disadvantages:
  • 1. The performance is poor
  • 2. Not having a clear goal
  • 3. Code redundancy 3> EventBus benefits:
  • 1. Simple
  • 2. Convenience refers to which shortcomings:
  • 1. Poor performance, a large number of reflection, stuck
  • 2. The code is difficult to maintain (you don’t need to know the logic of the code and need to read the underlying source code)
  • 4> Interface advantages:
  • 1. Simple
  • 2. High efficiency
  • 3. Disadvantages of decoupling:
  • It’s not good if you have a lot of interfaces in your project (and it can be cumbersome to name them), so you need universal interfaces

    5. Interface abstraction

  • Interface components: 1. Interface name 2. Function (return value of function name ==> Variable parameter ==> variable) 3. Empty function bodies There are four types of functions:

  • There is no return value for an argument
  • There are parameters and there are return values
  • No return value has an argument
  • No parameter No return value

    6. Code

    1. Define the Function

package com.zhang.yu.struct; /** * Created by zhang_shuai on 2017/10/18. * Del: Created by zhang_shuai on 2017/10/18 mFunctionName; public Function(String name){ this.mFunctionName = name; }}Copy the code

2. No parameter has no return value

package com.zhang.yu.struct; /** * Created by zhang_shuai on 2017/10/18. * Del: No parameter no return value */ public abstract class FunctionNoParamNoResult extends Function { public FunctionNoParamNoResult(String name) { super(name); } public abstract voidfunction(a); // No parameter no return value}Copy the code

2. There are returned values and parameters

package com.zhang.yu.struct; /** * Created by zhang_shuai on 2017/10/18. * Del: public abstract class FunctionAndParamNoResult<Param> extends Function { public FunctionAndParamNoResult(String name) { super(name); } public abstract Stringfunction(Param data);
}Copy the code

3. No parameter has a return value

package com.zhang.yu.struct; /** * Created by zhang_shuai on 2017/10/18. * Del: Public abstract class FunctionNoParamAndResult<Result> extends Function { public FunctionNoParamAndResult(String name) { super(name); } public abstract Resultfunction(a); }Copy the code

4. No parameter, no return value

package com.zhang.yu.struct; /** * Created by zhang_shuai on 2017/10/18. * Del: No parameter no return value */ public abstract class FunctionNoParamNoResult extends Function { public FunctionNoParamNoResult(String name) { super(name); } public abstract voidfunction(a); // No parameter no return value}Copy the code

5.FunctionManager is used to manage four cases

package com.zhang.yu.struct; import android.text.TextUtils; import java.util.HashMap; /** * Created by zhang_shuai on 2017/10/18. * Del: Management */ public class FunctionManager {private static final FunctionManager ourInstance = new FunctionManager(); public static FunctionManagergetInstance() {
        return ourInstance;
    }

    private FunctionManager() { mFunctionNoParamNoResultHashMap = new HashMap<>(); mFunctionNoParamAndResultHashMap = new HashMap<>(); mFunctionAndParamAndResultHashMap = new HashMap<>(); mFunctionAndParamNoResultHashMap = new HashMap<>(); } / / no parameters and return values private HashMap < String, FunctionNoParamNoResult > mFunctionNoParamNoResultHashMap; / / no parameter returns a value private HashMap < String, FunctionNoParamAndResult > mFunctionNoParamAndResultHashMap; / / a parameter has a return value to private HashMap < String, FunctionAndParamAndResult > mFunctionAndParamAndResultHashMap; / / has no return value parameter private HashMap < String, FunctionAndParamNoResult > mFunctionAndParamNoResultHashMap; // Add interface public FunctionManager addFunction(FunctionNoParamNoResultfunction){
        mFunctionNoParamNoResultHashMap.put(function.mFunctionName,function);
        returnthis; Public void invokeFunction(String)functionName){
        if(TextUtils.isEmpty(functionName)){
            return;
        }
        if(mFunctionNoParamNoResultHashMap ! = null){ FunctionNoParamNoResult f = mFunctionNoParamNoResultHashMap.get(functionName);
            if(f ! = null){ f.function(); }if(f == null){
                try {
                    throw new FunctionException("NO Function : "+functionName); } catch (FunctionException e) { e.printStackTrace(); }}}} // Add interface public FunctionManager addFunction(FunctionNoParamAndResult)function){
        mFunctionNoParamAndResultHashMap.put(function.mFunctionName,function);
        returnthis; } public <Result> Result invokeFunction(String)functionName,Class<Result>  c){
        if(TextUtils.isEmpty(functionName)){
            return null;
        }
        if(mFunctionNoParamAndResultHashMap ! = null){ FunctionNoParamAndResult f = mFunctionNoParamAndResultHashMap.get(functionName);
            if(f ! = null){if(c! =null){return c.cast(f.function());
                }else{
                    return(Result) f.function(); }}if(f == null){
                try {
                    throw new FunctionException("NO Function : "+functionName); } catch (FunctionException e) { e.printStackTrace(); }}}returnnull; } // Add interface public FunctionManager addFunction(FunctionAndParamNoResultfunction){
        mFunctionAndParamNoResultHashMap.put(function.mFunctionName,function);
        returnthis; } public <Param> Param invokeFunction(StringfunctionName,String  str){
        if(TextUtils.isEmpty(functionName)){
            return null;
        }
        if(mFunctionAndParamNoResultHashMap ! = null){ FunctionAndParamNoResult f = mFunctionAndParamNoResultHashMap.get(functionName);
            if(f ! = null){if(str! =null) {return(Param) f.function(str); }}if(f == null){
                try {
                    throw new FunctionException("NO Function : "+functionName); } catch (FunctionException e) { e.printStackTrace(); }}}returnnull; }}Copy the code

6.FunctionException Custom exception

package com.zhang.yu.struct; /** * Created by zhang_shuai on 2017/10/18. * Del: */ public class FunctionException extends Exception { public FunctionException(String s){ super(s); }}Copy the code

7.BaseFragment Is used to bind interfaces

package com.zhang.yu; import android.content.Context; import android.support.v4.app.Fragment; /** * Created by zhang_shuai on 2017/10/18. * Del: */ public class BaseFragment extends Fragment { @Override public void onAttach(Context context) { super.onAttach(context); // Bind the interfaceif(context instanceof MainActivity){ ((MainActivity)context).setFunction(); }}}Copy the code

8.Fragment1

package com.zhang.yu; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.zhang.yu.struct.FunctionManager; /** * Created by zhang_shuai on 2017/10/18. * Del: */ public class Fragment1 extends BaseFragment {public static final String INTRFACE = Fragment1.class.getName()+"NPNR";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message, container, false);
        view.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FunctionManager.getInstance().invokeFunction(INTRFACE); // use interface}});returnview; }}Copy the code

9. MainActivity to use

// Create interface public voidsetFunction(){
        FunctionManager functionManager = FunctionManager.getInstance();

            functionManager.addFunction(new FunctionNoParamNoResult(Fragment1.INTRFACE) {
                @Override
                public void function() {
                    Toast.makeText(MainActivity.this,"Interface with no parameters and no return value",Toast.LENGTH_SHORT).show(); }});functionManager.addFunction( new FunctionNoParamAndResult<String>(Fragment2.INTRFACE){

               @Override
               public String function() {

                   return "I'm an interface with a return value and no parameter 2."; }});functionManager.addFunction(new FunctionAndParamNoResult(Fragment3.INTRFACE) {
            @Override
            public String function(Object data) {
                return "I'm 3 with an interface that returns a value and takes a parameter."+data; }}); }Copy the code

conclusion

1. Use this scheduling 2. Main design ideas 3

Project address :github.com/fengyutongx…