helloworld:design_pattern:visiteur:java
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
helloworld:design_pattern:visiteur:java [2020/02/18 18:40] – mhtml -> html root | helloworld:design_pattern:visiteur:java [2020/05/11 01:19] (Version actuelle) – Suppression de "oldid" dans les liens Wikipedia root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | [[https:// | + | [[https:// |
=====Le visiteur version light===== | =====Le visiteur version light===== | ||
+ | |||
+ | Le type de retour de la méthode '' | ||
+ | |||
+ | Il peut être plus propre d' | ||
<file java VisitorDemo.java> | <file java VisitorDemo.java> | ||
+ | package cours1; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | import java.util.stream.Collectors; | ||
+ | |||
// Interface contenant les méthodes pour traiter toutes les implémentations de l' | // Interface contenant les méthodes pour traiter toutes les implémentations de l' | ||
interface CarElementVisitor { | interface CarElementVisitor { | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
+ | | ||
+ | |||
+ | | ||
} | } | ||
Ligne 15: | Ligne 28: | ||
// pour que la bonne méthode soit appelée en fonction de la signature. | // pour que la bonne méthode soit appelée en fonction de la signature. | ||
interface CarElement { | interface CarElement { | ||
- | | + | |
} | } | ||
Ligne 30: | Ligne 43: | ||
} | } | ||
- | public | + | public |
- | visitor.visit(this); | + | |
} | } | ||
} | } | ||
class Engine implements CarElement { | class Engine implements CarElement { | ||
- | public | + | public |
- | visitor.visit(this); | + | |
} | } | ||
} | } | ||
class Body implements CarElement { | class Body implements CarElement { | ||
- | public | + | public |
- | visitor.visit(this); | + | |
} | } | ||
} | } | ||
Ligne 51: | Ligne 64: | ||
public Car() { | public Car() { | ||
- | this.elements = new CarElement[] { new Wheel(" | + | this.elements = new CarElement[] { new Wheel(" |
- | | + | new Wheel(" |
- | new Wheel(" | + | |
} | } | ||
- | public | + | public |
- | | + | |
- | elem.accept(visitor); | + | |
- | } | + | |
- | | + | |
} | } | ||
} | } | ||
Ligne 66: | Ligne 77: | ||
// Une implémentation du traitement des données | // Une implémentation du traitement des données | ||
class CarElementPrintVisitor implements CarElementVisitor { | class CarElementPrintVisitor implements CarElementVisitor { | ||
- | public | + | public |
- | | + | |
} | } | ||
- | public | + | public |
- | | + | |
} | } | ||
- | public | + | public |
- | | + | |
} | } | ||
- | + | ||
- | public | + | public |
- | | + | |
} | } | ||
} | } | ||
class CarElementDoVisitor implements CarElementVisitor { | class CarElementDoVisitor implements CarElementVisitor { | ||
- | public | + | public |
- | | + | |
} | } | ||
- | public | + | public |
- | | + | |
} | } | ||
- | public | + | public |
- | | + | |
} | } | ||
- | public | + | public String visit(Car car) { |
- | System.out.println(" | + | return (" |
- | } | + | |
- | } | + | |
- | + | ||
- | // Le programme principal | + | |
- | public class VisitorDemo { | + | |
- | public static void main(String[] args) { | + | |
- | CarElement car = new Car(); | + | |
- | car.accept(new CarElementPrintVisitor()); | + | |
- | car.accept(new CarElementDoVisitor()); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | =====Le visiteur avec des paramètres et un code de retour===== | + | |
- | Ajouter un paramètre et un code de retour ne nécessite que de modifier l' | + | |
- | + | ||
- | <file java VisitorDemo.java> | + | |
- | // Interface contenant les méthodes pour traiter toutes les implémentations de l' | + | |
- | interface CarElementVisitor { | + | |
- | Object visit(Wheel wheel, Object data); | + | |
- | Object visit(Engine engine, Object data); | + | |
- | Object visit(Body body, Object data); | + | |
- | Object | + | |
- | } | + | |
- | + | ||
- | // Interface qui contiendra les données qui seront traitées par la méthode visit. | + | |
- | // Il faut absolument passer par une interface et que la fonction soit implémentée dans le POJO | + | |
- | // pour que la bonne méthode soit appelée en fonction de la signature. | + | |
- | interface CarElement { | + | |
- | Object accept(CarElementVisitor visitor, Object data); | + | |
- | } | + | |
- | + | ||
- | // Une implémentation des données stockées | + | |
- | class Wheel implements CarElement { | + | |
- | private String name; | + | |
- | + | ||
- | public Wheel(String name) { | + | |
- | this.name = name; | + | |
- | } | + | |
- | + | ||
- | public String getName() { | + | |
- | return | + | |
- | } | + | |
- | + | ||
- | public Object accept(CarElementVisitor visitor, Object data) { | + | |
- | return visitor.visit(this, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | class Engine implements CarElement { | + | |
- | public Object accept(CarElementVisitor visitor, Object data) { | + | |
- | return visitor.visit(this, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | class Body implements CarElement { | + | |
- | public Object accept(CarElementVisitor visitor, Object data) { | + | |
- | return visitor.visit(this, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | class Car implements CarElement { | + | |
- | CarElement[] elements; | + | |
- | + | ||
- | public Car() { | + | |
- | this.elements = new CarElement[] { new Wheel(" | + | |
- | new Wheel(" | + | |
- | new Wheel(" | + | |
- | } | + | |
- | + | ||
- | public Object accept(CarElementVisitor visitor, Object data) { | + | |
- | for(CarElement elem : elements) { | + | |
- | // Traiter correctement le code retour si nécessaire ici. | + | |
- | elem.accept(visitor, | + | |
- | } | + | |
- | return visitor.visit(this, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | // Une implémentation du traitement des données | + | |
- | class CarElementPrintVisitor implements CarElementVisitor { | + | |
- | public Object visit(Wheel wheel, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | public Object visit(Engine engine, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | public Object visit(Body body, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | public Object visit(Car car, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | class CarElementDoVisitor implements CarElementVisitor { | + | |
- | public Object visit(Wheel wheel, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | public Object visit(Engine engine, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | public Object visit(Body body, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | public Object visit(Car car, Object data) { | + | |
- | System.out.println(" | + | |
- | return null; | + | |
} | } | ||
} | } | ||
Ligne 226: | Ligne 116: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
CarElement car = new Car(); | CarElement car = new Car(); | ||
- | | + | |
- | | + | |
- | car.accept(new CarElementDoVisitor(), null); | + | |
} | } | ||
} | } | ||
</ | </ |
helloworld/design_pattern/visiteur/java.1582047634.txt.gz · Dernière modification : de root