Composition in Java Simplified With New JEP Draft
Want to learn more about a new feature in the JEP draft that can improve and simplify Java code? Check out this post on using the Concise Method Bodies (CMB).
Join the DZone community and get the full member experience.
Join For FreeFavor Composition Over Inheritance
The mantra "Favor Composition over Inheritance" has, with good reason, been repeated many times in 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.
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, we can see the new CMB as shown below:
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 makes a lot of sense. It is especially useful when delegating methods with one or several parameters. Hope you enjoyed!
EDIT: By using CBM also for the throwing methods (as exemplified in the last row below) the class could be reduced even further:
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) = UnmodifiableList::thrower;
Published at DZone with permission of Per-Åke Minborg, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments