File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Inheritance tree Refactoring  : Android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Inheritance tree Refactoring  : Android" Watch "Inheritance tree Refactoring  : Android" New topic

Inheritance tree Refactoring : Android

Sahil Reddy
Ranch Hand

Joined: Jan 24, 2011
Posts: 145
Context : Android
Problem : There are many "Brands" like mufti , levis , blackberry and so on. Each Brand contains Various categories like Jeans, t-shirts and so on.
As a user i want to select the Brand and then the Category and then want to see the various items in a particular category . These items will be shown in a VIEWPAGER wherein the user can swipe and see various items.

Present Solution : I am using ViewPager and it requires an instance of PagerAdapter which sends the Page in accordance to the position.

I extended PagerAdapter with mine "MyAdapter". MyAdapter class contains various if-else statements which decides the page to send back to "ViewPager" in accordance to the item and category chosen by a user.

So i tried cleaning up my code and ends up as follows

I created class hierarchy as follows

MuftiAdapter extends BaseAdapter
LevisAdapter extends BaseAdapter

and so on so forth....

MuftiAdapter knows about what to return in its various possible categories. Levis know its own....
I created one AdapterFactory which provides me the right instance.

AdapterFactory now decides which instance to return depending upon the BRAND and CATEGORY.

Someone asked me what iff there are 100 BRANDS , will you make 100 classes for the same which extends BaseAdapter ?

I am not sure whether my above design is right or wrong ??? Is there any better design in this case when number of brands are very high.???

Thanks and Regards

Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

Whoever that was who asked the question about creating 100 adapter classes if you had 100 brands is spot on. The design you have lacks abstraction and is not a very good one. Its seems to me that you only need one Adapter class that can be parameterized with Brand and Category.

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: Inheritance tree Refactoring : Android
It's not a secret anymore!