1

I have the following two methods:

public void informAllFragmentsWhenDatabaseStatusChanged () {
    for (int i=0; i<mainActivityPageAdapter.getCount(); i++) {
        if (mainActivityPageAdapter.getFragment(i) instanceof InterfaceMainDatabaseStatusChanged)
            ((InterfaceMainDatabaseStatusChanged) mainActivityPageAdapter.getFragment(i)).onMainDatabaseStatusChanged();
    };

}

public void informAllFragmentsWhenListStatusChanged () {
    for (int i=0; i<mainActivityPageAdapter.getCount(); i++) {
        if (mainActivityPageAdapter.getFragment(i) instanceof InterfaceListStatusChanged)
            ((InterfaceListStatusChanged) mainActivityPageAdapter.getFragment(i)).onListStatusChanged();
    };

}

Please note that each is executed in different moments.

As you noticed main part of both methods is the same. What is different the interface name used by instanceof and method which is executed on object.

The question is how to combine those two methods into one in elegant way.

I was thinking about something like that:

public void informAllFragments (InterfaceType interface);

Any suggestions?

user2707175
  • 1,071
  • 2
  • 16
  • 42

2 Answers2

1
private void informAllFragments (){
PagerAdapter mainActivityPageAdapter = null; // assign a valid PageAdapter here

   for (int i=0; i<mainActivityPageAdapter.getCount(); i++) {
        if (InterfaceListStatusChanged.class.isAssignableFrom(mainActivityPageAdapter.getFragment(i).getClass())){
            ((InterfaceListStatusChanged) mainActivityPageAdapter.getFragment(i)).onMainDatabaseStatusChanged();
        }else if (InterfaceMainDatabaseStatusChanged.class.isAssignableFrom(mainActivityPageAdapter.getFragment(i).getClass())){
            ((InterfaceMainDatabaseStatusChanged) mainActivityPageAdapter.getFragment(i)).onListStatusChanged();
        }
    };

}

You could also use the same if/else structure with 'instanceof'. Your code cannot get much shorter, as you call different methods.

user2436850
  • 158
  • 6
  • It's the direction I was thinking about. But when trying to implement it I had compiler problems. Thus I would be grateful for full proposal. – user2707175 Nov 06 '13 at 16:30
  • OK, I updated the Code above.You could also use [reflection](http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string), but I wouldn't recommend this. – user2436850 Nov 06 '13 at 18:03
  • Not so easy. As first: one class can have both interfaces (but here it's quite easy to modify that), secondly inform about event A and event B can be executed in different moments. Sure I could create some function like informAllFragments (int event) where event would be some constant identifier and then to have switch in for loop but it's neither very elegant (although perhaps a bit better than having two methods). I would rather prefer to give interface name as method parameter. – user2707175 Nov 07 '13 at 15:18
1

You can create simple interface with method somethink like:

interface IFragment{
void getFragment(MainActivityPageAdapter mainActivityPageAdapter);
}

Then you can create two classes and implement this interface. For example:

public class ListStatusChanged implemenet IFragment{
void getFragment(MainActivityPageAdapter mainActivityPageAdapter){
if (mainActivityPageAdapter.getFragment(i) instanceof  InterfaceMainDatabaseStatusChanged)
        ((InterfaceMainDatabaseStatusChanged) mainActivityPageAdapter.getFragment(i)).onMainDatabaseStatusChanged();
}
}

The same for second class. So your main method will looks like

public void informAllFragments(IFragment iFragment){
   for (int i=0; i<mainActivityPageAdapter.getCount(); i++) {
      iFragment.getFragment(mainActivityPageAdapter);
   }
}

So when you want to call first your method you will write

informAllFragments(new ListStatusChanged());

UPDATE you can put flag into the method informAllFragments(boolean flag) or enum

so you will have next

    public void informAllFragments (boolean flag) {
    for (int i=0; i<mainActivityPageAdapter.getCount(); i++) {
        if (flag && mainActivityPageAdapter.getFragment(i) instanceof InterfaceMainDatabaseStatusChanged)
            ((InterfaceMainDatabaseStatusChanged) mainActivityPageAdapter.getFragment(i)).onMainDatabaseStatusChanged();
else if (!flag && mainActivityPageAdapter.getFragment(i) instanceof InterfaceListStatusChanged)
            ((InterfaceListStatusChanged) mainActivityPageAdapter.getFragment(i)).onListStatusChanged();
    };

}

koropatva
  • 11
  • 2
  • It's not the direction I was thinking about. As instead of two methods I would have two classes ListStatusChanged implemenet IFragment and DatabaseStatusChanged implements IFragment. So it's more or less the same, two elements. But how to combine this into one? – user2707175 Nov 06 '13 at 16:28
  • check me updates :) this is idea you can use enums and put swith case. If you have more then two methods or use my logic with boolean flag for two statuses – koropatva Nov 07 '13 at 15:15
  • That's some kind of solution but not too elegant. Please note one of my comments above "Sure I could create some function like informAllFragments (int event) where event would be some constant identifier and then to have switch in for loop but it's neither very elegant (although perhaps a bit better than having two methods). I would rather prefer to give interface name as method parameter." In general it seems that there is no any easy way for solving this problem in java. – user2707175 Nov 08 '13 at 23:28