Avoiding code duplicacy java

Hi,

There are two classes A and B both extends a Class C

class A extends C
{
}

class B extends C
{
}

Now there are some common function which i want to use in class A as well as B and so i move them to class C to avoid code duplicity

There is a third class D which extends E and this class E is in a separate Jar
Now i need the common methods which i had put in C to be used in D too.

One way is to put it directly in D. but that would lead to code duplicity.
As E is in a separate jar the common code cannot be directly moved to E as the common code uses some classes in the
current project.

What way i can resolve this? Is there any way to avoid code duplicacy here and not to put the common functions in C directly under D ?

Thanks
Rohit BajajAsked:
Who is Participating?
 
dpearsonCommented:
There are a couple of approaches for this:

a) Add another base class to the hierarchy
    LeavableGroup extends BaseListFragment

    HomeListFragment extends LeavableGroup
    GroupListFragment extends HomeListFragment
    SearchFragment extends LeavableGroup

Then implement leaveGroup() in the LeavableGroup() class and it'll be available to the others.

b) Move the functionality for leaving a group to a separate class outside of the hierarchy
   public class Leave Group {
       public void leaveGroup(BaseListFragment fragment) {
              ....
        }
    }

and have each GroupListFragment and SearchFragment create a LeaveGroup class (as a data field) and call leaveGroup() on it.

For this to work, the LeaveGroup class needs to be able to see enough of the BaseListFragment to actually do the work of leaving the group - e.g. if there's a list and you need to take it off the list, it needs access to that list.

Hope that helps,

Doug
0
 
Rohit BajajAuthor Commented:
Also on similar lines i encountered one more issue :

I have the following two classes :

GroupListFragment extends HomeListFragment
SearchFragment extends BaseListFragment
HomeListFragment extends BaseListFragment


GroupListFragment and SearchFragment both have a common function leaveGroup()

The BaseListFragment is in a separate jar. How do i avoid code duplicacy of the function leaveGroup() in both GroupListFragment and SearchFragment
0
 
zzynxSoftware engineerCommented:
As E is in a separate jar the common code cannot be directly moved to E as the common code uses some classes in the current project.

If you have common functionality code, you should put it in a common jar/project/library.
That way it can be reused by each and every project.

GroupListFragment and SearchFragment both have a common function leaveGroup()
Then move that leaveGroup() method to the class both GroupListFragment and SearchFragment have in common: BaseListFragment.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.