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
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
  1. DZone
  2. Data Engineering
  3. Databases
  4. Java Holiday Calendar 2016 (Day 8): Use Traits in Java

Java Holiday Calendar 2016 (Day 8): Use Traits in Java

Using traits can vastly help simplify your code and allow you to reuse components with ease. This example examines using traits with interfaces.

Per-Åke Minborg user avatar by
Per-Åke Minborg
·
Dec. 08, 16 · Tutorial
Like (11)
Save
Tweet
Share
8.27K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

Today's tip is about defining your classes and interfaces using Traits. This allows you to simplify your code and reuse components efficiently without creating unnecessary coupling or inheritance between your classes.

Suppose we have two interfaces, Table and Column, that both have names, then we would introduce a name trait — HasName — that both interfaces extends. A Table and Column can also have an alias, so because of that, we would introduce the trait HasAlias:

    interface Table extends HasName, HasAlias, HasOtherTableProperties {}
    interface Column extends HasName, HasAlias, HasOtherColumnProperties {}

    interface HasName {
        void setName(String name);
        String getName();
    }

    interface HasAlias {
        void setAlias(String alias);
        String getAlias();
    }

    public <T extends HasName & HasAlias> void printNameAndAlias(T item) {
        System.out.format("Name=%2, Alias=%s%n", item.getName(), item.getAlias());
    }


Notice in the last method, printNameAndAlias(), how we define a dynamic compound type T that must be both HasName and HasAlias but does not care about other traits or defined methods. The nice thing now is that once we define a Schema interface like this:

    interface Table extends HasName, HasAlias, HasOtherSchemaProperties {}


Then we can re-use the existing trait methods (e.g. printNameAndAlias()) with no extra effort or coupling. So this would work out-of-the-box:

    Schema schema = ...;
    printNameAndAlias(schema);


The trait scheme is used heavily in the open-source project Speedment that is a stream based ORM tool and runtime. Check out the actual Table and Column code here.

Do This


    interface Table extends HasName, HasAlias, HasOtherTableProperties {}
    interface Column extends HasName, HasAlias, HasOtherColumnProperties {}

    interface HasName {
        void setName(String name);
        String getName();
    }

    interface HasAlias {
        void setAlias(String name);
        String getAlias();
    }

    public <T extends HasName & HasAlias> void printNameAndAlias(T item) {
        System.out.format("Name=%2, Alias=%s%n", item.getName(), item.getAlias());
    }


Don't Do This

   interface Table {
        void setName(String name);
        String getName();
        void setAlias(String name);
        String getAlias();
        ... other table specific methods
    }
    interface Column {
        void setName(String name);
        String getName();
        void setAlias(String name);
        String getAlias();
        ... other column specific methods
    }

    public void printNameAndAlias(Table item) {
        System.out.format("Name=%2, Alias=%s%n", item.getName(), item.getAlias());
    }

    public void printNameAndAlias(Column item) {
        System.out.format("Name=%2, Alias=%s%n", item.getName(), item.getAlias());
    }

    public void printNameAndAlias(Schema item) {
        System.out.format("Name=%2, Alias=%s%n", item.getName(), item.getAlias());
    }


Read more about traits on DZone here.

Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.

Trait (computer programming) Java (programming language) Database Calendar (Apple)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Automated Performance Testing With ArgoCD and Iter8
  • Secrets Management
  • Using AI and Machine Learning To Create Software
  • Top Authentication Trends to Watch Out for in 2023

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: