Interfaces

I'd be far more willing to use more ubiquitous interfaces in Java if I could do something akin to the following:

public interface SomeInterface {
    public void foo();
 
    public void bar();
}
public class SomeInterfaceImpl implements SomeInterface {
    public void foo() {
        System.out.println("Hello, world from SomeInterfaceImpl!");
    }
 
    public void bar() {
        System.out.println("Hello, world from SomeInterfaceImpl!");
    }
}
public class SomeClass implements SomeInterface {
 
    // Delegate all calls to foo() here.
    private SomeInterface someInterfaceInstance implements SomeInterface;
 
    public SomeClass(SomeInterface someInterfaceInstance) {
        this.someInterfaceInstance = someInterfaceInstance;
    }
 
    @Override
    public void bar() {
        System.out.println("Hello, world from SomeClass!");
    }
 
    public static void main(String... args) {
        SomeInterface someInterfaceImpl = new SomeInterfaceImpl();
        SomeClass someClassInstance = new SomeClass(someInterfaceImpl);
 
        someClassInstance.foo();
        someClassInstance.bar();
    }
}
> java SomeClass
> Hello, world from SomeInterfaceImpl!
> Hello, world from SomeClass!

This keeps you from having to type out this method on SomeClass:
@Override
public void foo() {
    this.someInterfaceInstance.foo();
}

Which is hella annoying. And yeah, you could get your IDE to do that for you, but then it adds clutter to your source code when you or somebody new comes to look at it later. Besides, the less typing/clicking there is, the more productive I can be! Plus, this promotes composition over inheritance, a lofty goal in software engineering. Win win, don't you think?

Comments

Add a New Comment
myface.jpg
Find me on:
LiveJournal Facebook MySpace Twitter reddit Google Amazon.comBoardGameGeek
{"module":"feed\/FeedModule","params":{"src":"http:\/\/twitter.com\/statuses\/user_timeline\/18552685.rss","limit":"1","module_body":"[http:\/\/twitter.com\/psyllogism %%description%%]"}}

psyche (sī'kē) n. 1. That which is responsible for one's thoughts and feelings. 2. The seat of the faculty of reason.

syllogism (sĭl'ō-jĭz'əm) n. 1. Reasoning from the general to the specific. 2. A subtle or specious piece of reasoning.

psyllogism (sī'lō-jĭz'əm) person: A twenty-something college graduate, part time grad student, software developer, and geek. I am married and trying to manage school, life, friends, family, a job, and an (un)healthy gaming hobby.

(view template)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License