How to wire deeply dependent polymorphic objects in Guice?

40 views Asked by At

How to translate the following pieces into Guice(v6.0) ?

  • Person class depends on Meet class and Bye interface.
  • Meet class depends on Hello interface.
  • Hello interface has polymorphic implementations by FrenchHello class and SpanishHello class.
  • Bye interface has polymorphic implementations by FrenchBye class and SpanishBye class.
  • Main class creates one Person object, who says hello and bye in French.
  • Main class creates the other Person object, who says hello and bye in Spanish.
interface Hello {
    void say();
}

class FrenchHello implements Hello {
    void say() { System.out.println("Bonjour!"); }
}

class SpanishHello implements Hello {
    void say() { System.out.println("Hola!"); }
}

interface Bye {
    void say();
}

class FrenchBye implements Bye {
    void say() { System.out.println("Au revoir!"); }
}

class SpanishBye implements Hello {
    void say() { System.out.println("Adios!"); }
}

class Meet {
    Hello hello;
    Meet(Hello hello) { this.hello = hello; }
    void say() { hello.say(); }
}

class Person {
    Meet meet;
    Bye bye;
    Person(Meet meet, Bye bye ) { 
        this.meet = meet; 
        this.bye = bye;
    }
    void say() {
        meet.say();
        bye.say();
    }
}
        
public class Main {
    public static void main(String[] args) {
        new Person(new Meet(new FrenchHello()), new FrenchBye())).say();
        new Person(new Meet(new SpanishHello()), new SpanishBye())).say();
    }
}
0

There are 0 answers