Minborg

Minborg
Minborg

Thursday, December 8, 2016

Day 8, Java Holiday Calendar 2016, Use Traits

8. Use Traits in Java


Today's tips 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 district 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 in the original DZone article at https://dzone.com/articles/using-traits-in-java-8

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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.