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.
Idiomatic means following the conventions of the language. You want to find the easiest and most common ways of accomplishing a task rather than porting your knowledge from a different language.
Check for words or phrases that cannot be taken literally. "You have a chip on your shoulder" is one example. The literal meaning of this phrase is to hold a grudge. You will know you have found an idiom when the actual phrase does not make sense.
So, in software engineering “idiomatic” means “conforming to the natural mode of expression in a given context”, where that context can be a programming language, like Kotlin or JS, or even a framework like React or RxJava.
Kotlin, a new JVM language created by Jetbrains, is quickly becoming the go-to programming language used for Android development at Hootsuite. Its many features and Java interoperability make writing code much faster, cleaner, and less verbose.
Yes, and not only idiomatic Java, but OO, in general. It is called template method
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With