Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of composition usage in Java 7 & 8 insead of JavaScript. Follow up to: https://www.youtube.com/watch?v=wfMtDGfHWpA
javac CompositionJava7LombokExample.java -cp ~/.m2/repository/org/projectlombok/lombok/1.16.6/lombok-1.16.6.jar
java -cp ./ Main
# Bark, Bark!
# Driving to the destination...
# What a day! What a lovely day!
# What a day! What a lovely day!
# Bark, Bark!
# Bark, Bark!
/usr/lib/jvm/java-8-oracle/bin/javac CompositionJava8Example.java
/usr/lib/jvm/java-8-oracle/bin/java -cp ./ Main
# Bark, Bark!
# Driving to the destination...
# What a day! What a lovely day!
# What a day! What a lovely day!
# Bark, Bark!
# Bark, Bark!
import lombok.Delegate;
interface Barker {
void bark();
class DefaultImpl implements Barker {
@Override
public void bark() {
System.out.println("Bark, Bark!");
}
}
}
interface Pooper {
void poop();
class DefaultImpl implements Pooper {
@Override
public void poop() {
System.out.println("<dump droped>");
}
}
}
interface Meower {
void meow();
class DefaultImpl implements Meower {
@Override
public void meow() {
System.out.println("Meow!");
}
}
}
interface Cleaner {
void clean();
class DefaultImpl implements Cleaner {
@Override
public void clean() {
System.out.println("Cleaning for effect...");
}
}
}
interface Driver {
void drive();
class DefaultImpl implements Driver {
@Override
public void drive() {
System.out.println("Driving to the destination...");
}
}
}
interface Killer {
void kill();
class DefaultImpl implements Killer {
@Override
public void kill() {
System.out.println("What a day! What a lovely day!");
}
}
}
class Dog implements Barker, Pooper {
@Delegate
private Barker barker = new Barker.DefaultImpl();
@Delegate
private Pooper pooper = new Pooper.DefaultImpl();
}
class Cat implements Meower, Pooper {
@Delegate
private Pooper pooper = new Pooper.DefaultImpl();
@Delegate
private Meower meower = new Meower.DefaultImpl();
}
class CleaningRobot implements Driver, Cleaner {
@Delegate
private Driver driver = new Driver.DefaultImpl();
@Delegate
private Cleaner cleaner = new Cleaner.DefaultImpl();
}
class MurderRobot implements Driver, Killer {
@Delegate
private Driver driver = new Driver.DefaultImpl();
@Delegate
private Killer killer = new Killer.DefaultImpl();
}
class MurderRobotDog implements Driver, Killer, Barker {
@Delegate
private Driver driver = new Driver.DefaultImpl();
@Delegate
private Barker barker = new Barker.DefaultImpl();
@Delegate
private Killer killer = new Killer.DefaultImpl();
}
class Main {
public static void main(String[] args) {
MurderRobotDog mrd = new MurderRobotDog();
mrd.bark();
mrd.drive();
mrd.kill();
Killer killer = mrd;
killer.kill();
Barker barker = mrd;
barker.bark();
barker = new Dog();
barker.bark();
}
}
interface Barker {
default void bark() {
System.out.println("Bark, Bark!");
}
}
interface Pooper {
default void poop() {
System.out.println("<dump droped>");
}
}
interface Meower {
default void meow() {
System.out.println("Meow!");
}
}
interface Driver {
default void drive() {
System.out.println("Driving to the destination...");
}
}
interface Cleaner {
default void clean() {
System.out.println("Cleaning for effect...");
}
}
interface Killer {
default void kill() {
System.out.println("What a day! What a lovely day!");
}
}
class Dog implements Barker, Pooper {
}
class Cat implements Meower, Pooper {
}
class CleaningRobot implements Driver, Cleaner {
}
class MurderRobot implements Driver, Killer {
}
class MurderRobotDog implements Driver, Killer, Barker {
}
class Main {
public static void main(String[] args) {
MurderRobotDog mrd = new MurderRobotDog();
mrd.bark();
mrd.drive();
mrd.kill();
Killer killer = mrd;
killer.kill();
Barker barker = mrd;
barker.bark();
barker = new Dog();
barker.bark();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.