Created
December 10, 2020 06:28
-
-
Save Riduidel/14c110bd77b75d49ad6038ebea3eca2f to your computer and use it in GitHub Desktop.
Visitor -3 - Main avec visiteur retournant void
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Feuille implements Visitable { | |
public String montrerFeuille() { return "🍃"; } | |
@Override public void accept(Visitor v) { v.visit(this); } | |
} | |
class Fleur implements Visitable { | |
public String afficherFleur() { return "🌺"; } | |
@Override public void accept(Visitor v) { v.visit(this); } | |
} | |
interface Visitable { | |
public void accept(Visitor v); | |
} | |
interface Visitor { | |
void visit(Feuille feuille); | |
void visit(Fleur fleur); | |
} | |
public class Main { | |
public static void main(String[] args) { | |
List<Visitable> visitables = Arrays.asList(new Feuille(), new Fleur()); | |
for (Visitable v : visitables) { | |
v.accept(new Visitor() { | |
@Override public void visit(Feuille feuille) { | |
System.out.println(feuille.montrerFeuille()); | |
} | |
@Override public void visit(Fleur fleur) { | |
System.out.println(fleur.afficherFleur()); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment