A suitable way to avoid the static method (possibly in the Util class) that is used to update the user interface

I'm new to Android, I read and see if the static method in the Util class is used to update the user interface for unit test. How do I avoid this in an appropriate way to maintain code and unit test?

Example:

class ActivityA {
    private View view;
    private MyListener myListener;
    public void methodB() {
        Util.callLogicB(this, view, myListener);
    }
}

class ActivityB {
    private View view;
    private MyListener myListener;
    public void methodC() {
        Util.callLogicB(this, view, myListener);
    }
}

class Util {
    public static void callLogicB(Context context, View view, MyListener listener) {
    // do something with view
    }
}
+4
source share
3 answers

I think the best way to implement this is to completely disconnect your views (your activity) from your logical class. This can be done using the interface .

, , . mainActivity textView . LogicWithTextUpdate textView mainActivity .

public class MainActivity extends Activity implements MyViewListener {
    //Your logic class, that is capable of updating a textview
    LogicWithTextUpdate logicWithTextUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //This is just to create a layout (with a textview inside)
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //This creates the class using constructor (and set listener)
        logicWithTextUpdate = new LogicWithTextUpdate(this);

        //Update or execute your logic. This will update textview using interface MyViewListener (onUpdateTextListener)
        //This can be executed using a button (Onclick...)
        logicWithTextUpdate.doSomeLogic(2);
    }

    @Override
    public void onUpdateTextListener(String text) {
        //Only here code runs inside your activity class
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(text);
    }
}

. MainActivity textview, "listener" onUpdateTextListener. :

public interface MyViewListener {
    //to be implemented inside your mainActivity
    public void onUpdateTextListener(String text);
}

, :

public class LogicWithTextUpdate {
    MyViewListener myViewListener;

    //Constructor here is used to set myViewListener (but you can use a setter like setMyViewListener)
    public LogicWithTextUpdate(MyViewListener myViewListener) {
        this.myViewListener = myViewListener;
    }

    public void doSomeLogic(int a) {
        //Some logic
        a = a * 2;
        a = a + 1;

        //Update text using listener. This will update mainActivity, because it is implementing MyViewListener
        myViewListener.onUpdateTextListener(String.valueOf(a));
    }
}

doSomeLogic , . myViewListener.onUpdateTextListener, onUpdateTextListener .

. - LogicWithTextUpdate Actitivy. .

+3

AFAIK, Utility , , , , . , Utility , :

  • , URL URL- HTTP-
  • , JSON HTTP-
  • , JSON ArrayList
  • , , , ArrayList Activity , .
+2

, . . , ​​ , :

final class Util {
    static IUtil util = new DefaultUtil();
    private Util() {}

    public static void callLogicB(Context context, View view, MyListener listener) {
        util.callLogicB(context, view, listener);
    }

}


interface IUtil {
    public void callLogicB(Context context, View view, MyListener listener);
}

final class DefaultUtil implemets IUtil {
    @Override
    public void callLogicB(Context context, View view, MyListener listener) {
    // do something with view
    }
}

final class TestUtil implemets IUtil {
    @Override
    public void callLogicB(Context context, View view, MyListener listener) {
    //your test logic
    }
}

@Before
void setUp() {
    Util.util = new TestUtil();
}
+1
source

Source: https://habr.com/ru/post/1679307/


All Articles