Dev question: Communication between components, am I doing it right?

Hi,

We’re intensely evaluating Vaadin for a big project, and after a couple of weeks I must say I’m impressed. Once one finally learns the layouts and, well, the basics, Vaadin makes for a great framework. Now we’re going for real.

My question is maybe for the dev team, as it is regarding the programming approach. I’ve checked the demos and all of them (if I’m not mistaken) are single-class, with global variables. As our project is quite big, we will need to split the work in different classes. Basically, it works, but I wonder if it’s the best way of doing it.

Okay, to simplify. Think of itunes. I have a HorizontalSplitPanel. On the left, a list of elements (for this example, a song list). On the right, the working area (for this example, a picture of the album and a play button).

The action we want to perform is to change the right area when an element from the left area is selected. For that, the split panel has two components, SongList and WorkingArea, which extend Panel.

This is what we’re doing: SongList has a reference to the object WorkingArea, and calls a method named changeWorkArea(song name). Is this the best way to do it?

Main class:


HorizontalSplitPanel mainPanel = new HorizontalSplitPanel();
WorkArea wa = new WorkArea()
SongList sl = new SongList(wa); // SongList will need to call methods from WorkArea

mainPanel.addComponent(sl);
mainPanel.addComponent(wa);

So, pretty simple so far.

Class SongList (extends Panel):


final Button s1 = new Button("Song 1");
final Button s2 = new Button("Song 2");

s1.setStyleName(BaseTheme.BUTTON_LINK);
// Tell the working area to regenerate itself with the new song
s1.addListener(new Button.ClickListener() {
	@Override
	public void buttonClick(ClickEvent event) {
		workArea.changeWorkArea(s1.getCaption()); // workArea has been initialized previously
	}
});

addComponent(s1);

... and the same code for song 2

Finally, WorkArea (also extends Panel) changes itself when called from the SongList


...initializers

public void changeWorkArea (String songName) {
	removeAllComponents();
	addComponent(new Song(songName)); // Not really important for this question
}

As you can see, my main concern is that maybe it’s not a great idea to initialize SongList with a reference to the WorkArea, but I’m not sure how you guys would do it.

Well, so this is what we’re doing, and I’d love to hear any criticisms. It works, so it must not be that bad, but as we’re building a big project, we’d like to do it well from the beginning.

Thank you very much!

Carlos

It depends™, but here’s one way I can think of: Create your own SongChangeEvent class and SongChangeListener interface, and add an addListener(SongChangeListener) method to your SongList class. Then have a separate controller/presenter class that implements your interface and listens for SongChangeEvents on your SongList, and then modifies the WorkArea accordingly.

Marlon’s suggestion is great. However if you want to do it a litte simpler (and maybe a bit dirtier?) you could just initialize the clicklistener in the main class, and pass that as a variable to the SongList. Saves creating the listener and event classes as it just reuses the existing ones.

And your assumption on splitting views into multiple classes is correct. Most nontrivial projects has a bunch of classes. Projects I’ve been working on usually has 10-100 Vaadin-related classes.

The idea of implementing a Listener is great, I hadn’t thought of that. It’s the first time we use a Java framework to develop web software and, well, it takes some time to change the paradigm from PHP+Javascript.

Thanks a lot to both of you!