mercredi 23 mars 2016

Java/Android Library - Design pattern suggestion

I have simple lib with custom layouts:

public class MyDelegate {
    setColor(int color);
    setCustomParam(int param)
}

----------------------------------------------------

public class MyLL extends LinearLayout {
    MyDelegate delegate;
    ...
    @Override
    setBackgroundColor(int color){
        delegate.setColor()
    }

    setCustomParam(int param){
        delegate.setCustomParam(param);
    }
}



public class MyRL extends RelativeLayout {
    MyDelegate delegate;
    ...
    @Override
    setBackgroundColor(int color){
        delegate.setColor()
    }

    setCustomParam(int param){
        delegate.setCustomParam(param);
    }
}



public class MyFL extends FrameLayout {
    MyDelegate delegate;
    ...
    @Override
    setBackgroundColor(int color){
        delegate.setColor()
    }

    setCustomParam(int param){
        delegate.setCustomParam(param);
    }
}

For non-Android users I'm just gonna say that LinearLayout, RelativeLayout and FrameLayout has the same parent which is called ViewGroup. My custom classes are using the same delegate and I need to override some of the base methods (eg. setBackgroundColor()) or add new ones (eg. setCustomParam()).

I wonder if it's possible to redesign it so that I could avoid writing those setBackgroundColor, setCustomParam in each of my custom layout and put them in one place.

Aucun commentaire:

Enregistrer un commentaire