jeudi 21 mai 2015

"Broken OOP" or how to use NavigationDrawer callbacks

I am creating app with using custom implementation of NavigationDrawer- MaterialDrawer. This is great library that uses dynamic adding drawer into existent view hierarchy.
As far as we need Navigation Drawer to be present on all activities in our application, we can have one BasicActivity abstract class that initialize drawer, sets it up and do some other basic stuff which is similar for all activities.

Here is my some part of logic from my BaseActivity.

  @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l, IDrawerItem iDrawerItem) {
        Intent intent;
        int itemId = iDrawerItem.getIdentifier();
        if(itemId==getActivityTag()) {
            return;
        }
        switch(itemId){
            case DrawerConstants.PrimaryItemConstants.ALL_CATEGORIES_PAGE_INDEX:
                intent = new Intent(this,CategoryListActivity.class);
                startActivity(intent);
                break;
            case DrawerConstants.PrimaryItemConstants.LOGIN_ITEM_INDEX:
                intent = new Intent(this,LoginScreen.class);
                startActivity(intent);
                break;
        }
    }
    public abstract int getActivityTag();

Everything seems to work good, but one problem is that we have to know about all our child inherited classes and switch between them in the statement.
So parent is aware of its children.
I think it is bad practice in terms of OOP.
Maybe there is any workarounds to solve this problem in elegant way following design patterns.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire