Platinum Partner
java,scala

Applying Scala to solving real world problems: Making delegation super easy

In Java it is very common to write boring code like below to perform logging:
public class Foo {
private static Logger logger = LoggerFactory.getLogger(Foo.class);

public void m1() {
logger.debug("...");
...
logger.debug("...");
}
public void m2() {
logger.debug("...");
...
logger.debug("...");
}
}
As you keep typing the delegation call logger.debug(…) repeatedly, you’ll wonder if there is an easy way to do that. For example, you can create your own debug() method, then you can just call debug() instead of logger.debug():
public class Foo {
private static Logger logger = LoggerFactory.getLogger(Foo.class);

public void debug(String msg) {
logger.debug(msg);
}
public void m1() {
debug("...");
...
debug("...");
}
public void m2() {
debug("...");
...
debug("...");
}
}
The problem is that,you’ll need to create other similar methods like info(), warn() and error(). In addition, when you work on another class Bar, you will need to create those  methods in Bar again! To solve these problems, you may extract this code into a common base class to be reused:
public class DelegatingLogger {
protected static Logger logger;

public void debug(String msg) {
logger.debug(msg);
}
public void info(String msg) {
logger.info(msg);
}
...
}

public class Foo extends DelegatingLogger {
static {
logger = LoggerFactory.getLogger(Foo.class);
}
public void m1() {
debug("...");
...
debug("...");
}
}
However, because Java only support single inheritance, if Foo needs to inherit a real base class, then this approach won’t work. To solve this problem in Scala, we can make the DelegatingLogger class a “trait” which is an auxiliary base class (any class can be a trait as long as its constructor takes no argument):
trait DelegatingLogger {
val logger: Logger = LoggerFactory.getLogger(getClass());

def debug(msg: String) {
logger.debug(msg)
}
def info(msg: String) {
logger.info(msg)
}
}

class Foo extends SomeParent with DelegatingLogger {
def m1 {
debug("...")
...
debug("...")
}
}

What the trait mechanism does here is essentially allowing us to easily turn the Foo class from a logger user into a logger itself, that is, turning a “use” relationship into an “is” relationship.

If you’re careful, you may notice that now the logger is an instance variable, not a static variable. If you’re concerned about it, you can let a singleton object (which is also named Foo here but could be anything else) inherit DelegatingLogger, then use a static import to make the static methods available in the Foo class:

object Foo extends DelegatingLogger {

}

import Foo._

class Foo extends SomeParent {
def m1 {
debug("...")
}
}

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}