Skip to content

Instantly share code, notes, and snippets.

@Riduidel
Created December 10, 2020 06:28
Show Gist options
  • Save Riduidel/14c110bd77b75d49ad6038ebea3eca2f to your computer and use it in GitHub Desktop.
Save Riduidel/14c110bd77b75d49ad6038ebea3eca2f to your computer and use it in GitHub Desktop.
Visitor -3 - Main avec visiteur retournant void
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