Best way to avoid duplicate code when using Fragments

Viewed 2474

I have an app ready and running in the Google Play store, now i am implementing fragments.

so,i already have a class A which extends B with some methods, now i have class which C extends FragmentActivity, so now i need use the same methods as in class A but here since i am extending FragmentActivity i cannot make use of class B, so here there are duplicate methods which are same as class A but i need to reuse the methods.

This below example shows my situation:

Eg:

Current implementation

class A extends B{

one();
two();

}

After integrating fragments,

Eg:

class C extends FragmentActivity{

one();// same methods as in class A
two();// same methods as in class A

}

1) What is the best way to re-use the methods in this case and how?

2) I have approach in my mind like creating a class and make the methods as static and re-use the methods in both A and C class, but is my approach good and can i make the methods as static and is it a good approach ?

3) Another approach that i have thought of "Strategy Pattern".

Eg:
class A extends ApiClass {
   private ClassContainingDupMethod strategy;
}

class N extends AnotherApiClass {
   private ClassContainingDupMethod strategy;

   public methodCallingDupMethod(){
      strategy.dupMethod();
   }
}

class ClassContainingDupMethod{
   public dupMethod(){;}
}

Is the "Strategy Pattern". a good approach ? as i need to create object of common class in both the classes.

Any suggestions on this would be appreciated.

7 Answers
Related