views:

114

answers:

3

Bear with me... I don't think this is too subjective but maybe I'm wrong.

Recently I wanted to factor out some repetitive code which drew a custom Bitmap background on our BlackBerry app.

(This question is not really about BlackBerry though, so I'll provide some details here about the BB GUI so that non-BB Java people can weigh in...)

The class FullScreen is from the BB API - it has a method paint(Graphics) which the framework calls to draw the screen and any components added to it. It's possible to override this to do custom painting - like drawing a Bitmap background before any other painting happens (newer BB APIs provide a Background class but our app has to work on older phones).

I wanted to have a bunch of screens all with the same background, which each did some kind of custom painting... here's what I came up with:

abstract public class BGFullScreen extends FullScreen {
    Bitmap bg;

    public BGFullScreen(Manager mgr, long style) {
        super(mgr, style);
        bg = Bitmap.getBitmapResource("bg.jpg");
    }

    abstract protected void innerPaint(Graphics g);

    protected void paint(Graphics g) {
        g.drawBitmap(new XYRect(0, 0, bg.getWidth(), bg.getHeight()), bg, 0, 0);

        innerPaint(g);

        super.paint(g);
    }
}

Each screen will then subclass this abstract class and implement innerPaint(). This way when the BB framework calls the paint() method, each screen can have custom painting stuff happen AFTER the background is drawn (so any painting happens on top of the background) but BEFORE the components of the screen are drawn, when FullScreen.paint() is called.

(I came up with this because I've been studying Common Lisp at home, and it occurred to me that what I wanted to do was something like the interleaved method combination in CLOS)

Here's a sample implementation of the abstract class above:

public class MainAppScreen extends BGFullScreen {

    public MainAppScreen() {
        super(new VerticalFieldManager(), 0);
        // add some components to the screen:
        add(new ButtonField(...));
        add(...)
    }

    protected void innerPaint(Graphics g) {
        // stuff drawn will be on top of background and under buttons 
        g.draw(...)
    }
}

Basically I want a child class to implement a method gets called in-between its parent's implementation and its grandparent's implementation of the same method. I couldn't figure out any other way to do this in Java...

Is this idomatic Java? Is this actually really common and it's a dumb question? Is this actually horrible design? (BB experts, how can I do this any other way?)

Edited to add: This does work as described - the drawing happens in the order I want.

+4  A: 

Yes, and not only idiomatic Java, but OO, in general. It is called template method

OscarRyz
Thanks! Maybe it's a silly question... I'm definitely only a beginner.
spacemanaki
nahhh, from time to time we tend to invent already invented patterns. It is good to ask, and then find out it already existed. From now on, you can say *hey, we don't you just use the template method pattern for that* etc. ;)
OscarRyz
Well I haven't actually read much on patterns altho the GoF book is on my to-read list.
spacemanaki
+2  A: 

This is almost exactly how Swing works — see the JComponent.paintComponent method. The paint method calls paintBorder, paintComponent, and paintChildren, and you can override paintComponent to implement your own drawing without interfering with the standard Swing components.

jleedev
Interesting - I haven't used Swing in a long time and even then it was only a short class in undergrad. Thanks!
spacemanaki
A: 

Welcome to design pattern: What you discovered is called Template Method.

The Elite Gentleman