DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Separation of Concerns in Vaadin

Separation of Concerns in Vaadin

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
Feb. 23, 12 · Interview
Like (0)
Save
Tweet
Share
4.90K Views

Join the DZone community and get the full member experience.

Join For Free

Last week, we quickly created a Delete generated column in a table.

Although this is enough to attain our objective, two nested anonymous classes puts a strain on maintenance costs. Moreover, behavior code (the deletion) is interwoven with GUI code (column).

Correcting these mistakes can be achieved through the creation of two top-level classes, one for the deletion behavior, and the other for the column. We have to find a way to pass the data container and the item id to the behavior class: the most obvious way to do so it change the latter's structure to store both like so:

public class DeleteButtonColumnGenerator implements ColumnGenerator {
 
    @Override
    public Object generateCell(Table source, Object itemId, Object columnId) {
 
        Button button = new Button("Delete");
 
        button.addListener(new DeleteClickListener(itemId, source.getContainerDataSource()));
 
        return button;
    }
}

This design is much more decoupled and maintenance friendly than the following, but we can do better. The click listener's structure is at present tightly coupled to the parameters it needs to access. It would be nice to remove this coupling. Fortunately, Vaadin components can store data on their own. Let's rely on this feature to clean our design.

First, create a placeholder for the container and the item id:

public class ContainerItemId {
 
    private final Object itemId;
     
    private final Container container;
 
    public ContainerItemId(Container container, Object itemId) {
     
        this.itemId = itemId;
        this.container = container;
    }
 
    public Object getItemId() {
     
        return itemId;
    }
 
    public Container getContainer() {
     
        return container;
    }
}

Then, we remove all references to both container and item id from the click listener and use the Vaadin way:

public class DeleteClickListener implements ClickListener {
 
    @Override
    public void buttonClick(ClickEvent event) {
 
        Button button = event.getButton();
         
        ContainerItemId cii = (ContainerItemId) button.getData();
         
        if (cii != null) {
             
            cii.getContainer().removeItem(cii.getItemId());
        }
    }
}

Notice how we get the reference to the button in the event, it's enough!

Finally, we have to pass the data to the button in the column generator:

public class DeleteButtonColumnGenerator implements ColumnGenerator {
 
    @Override
    public Object generateCell(Table source, Object itemId, Object columnId) {
 
        Button button = new Button("Delete");
 
        ContainerItemId cii = new ContainerItemId(source.getContainerDataSource(), itemId);
         
        button.setData(cii);
         
        button.addListener(new DeleteClickListener());
 
        return button;
    }
}

This new design is much more modularized: for application that go beyond prototypes, this approach should be favored over the previous one.

From http://morevaadin.com/content/separation-concerns

Vaadin

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • HTTP vs Messaging for Microservices Communications
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: