Minborg

Minborg
Minborg

Thursday, September 20, 2018

Composition in Java will be Simplified with New JEP Draft

Favor Composition over Inheritance

The mantra "Favor Composition over Inheritance" has, with good reasons, been repeated many times in the literature. However, there is little or no language support in Java to simplify the composition of objects. However, with a new JEP draft named "Concise Method Bodies", the situation might improve slightly.

Brian Goetz is responsible for the JEP draft which likely will be handled under project "Amber". The complete draft can be found here.

Concise Method Bodies

The JEP, when implemented, allows for something called Concise Method Bodies (CMB) whereby, loosely speaking, a method body can be a lambda or a method reference. Here is one example:

Old Style:

int length(String s) {
  return s.length();
}


New CMB:

int length(String s) -> s.length();     //  -> is "single expression form"

or alternately simply:

int length(String s) = String::length;  //  = is "method reference form"

This will reduce boilerplate coding while improving code readability.

Composition

Consider the existing Java class Collections.UnmodifiableList which delegates an inner List class and prevents it from being modified (code shortened and reordered for readability):

  static class UnmodifiableList<E> extends UnmodifiableCollection<E>
                                  implements List<E> {

        final List<? extends E> list;

        UnmodifiableList(List<? extends E> list) {
            super(list);
            this.list = list;
        }

        public boolean equals(Object o) {return o == this || list.equals(o);}
        public int hashCode()           {return list.hashCode();}

        public E get(int index) {return list.get(index);}
        public int indexOf(Object o)            {return list.indexOf(o);}
        public int lastIndexOf(Object o)        {return list.lastIndexOf(o);}
        public E set(int index, E element) {
            throw new UnsupportedOperationException();
        }

With CMB, it can be implemented like this:

 static class UnmodifiableList<E> extends UnmodifiableCollection<E>
                                  implements List<E> {

        final List<? extends E> list;

        UnmodifiableList(List<? extends E> list) {
            super(list);
            this.list = list;
        }

        public boolean equals(Object o) = list::equals;
        public int hashCode()           = list::hashCode;
        public E get(int index)         = list::get;
        public int indexOf(Object o)    = list::indexOf;
        public int lastIndexOf(Object o)= list::lastIndexOf;
        public E set(int index, E element) {
            throw new UnsupportedOperationException();
        }


I think this feature would make sense. It is especially useful when delegating methods with one or several parameters.

No comments:

Post a Comment

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