Outils pour utilisateurs

Outils du site


helloworld:design_pattern:visiteur:java

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
helloworld:design_pattern:visiteur:java [2020/02/18 18:40] – mhtml -> html roothelloworld:design_pattern:visiteur:java [2020/05/11 01:19] (Version actuelle) – Suppression de "oldid" dans les liens Wikipedia root
Ligne 1: Ligne 1:
-[[https://en.wikipedia.org/w/index.php?title=Visitor_pattern&oldid=939675762|Visitor pattern]] {{ :helloworld:design_pattern:visiteur:java:visitor_pattern_-_wikipedia_2020-02-18_18_38_39_.html |Archive du 07/02/2020 le 18/02/2020 }}+[[https://en.wikipedia.org/w/index.php?title=Visitor_pattern|Visitor pattern]] {{ :helloworld:design_pattern:visiteur:java:visitor_pattern_-_wikipedia_2020-02-18_18_38_39_.html |Archive du 07/02/2020 le 18/02/2020 }}
 =====Le visiteur version light===== =====Le visiteur version light=====
 +
 +Le type de retour de la méthode ''visit'' / ''accept'' dépend du besoin.
 +
 +Il peut être plus propre d'utiliser la valeur de retour pour séparer le traitement des données de leur exploitation.
  
 <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 CarElement. // Interface contenant les méthodes pour traiter toutes les implémentations de l'interface CarElement.
 interface CarElementVisitor { interface CarElementVisitor {
-  void visit(Wheel wheel); +  String visit(Wheel wheel); 
-  void visit(Engine engine); + 
-  void visit(Body body); +  String visit(Engine engine); 
-  void visit(Car car);+ 
 +  String visit(Body body); 
 + 
 +  String visit(Car car);
 } }
  
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 {
-  void accept(CarElementVisitor visitor);+  String accept(CarElementVisitor visitor);
 } }
  
Ligne 30: Ligne 43:
   }   }
  
-  public void accept(CarElementVisitor visitor) { +  public String accept(CarElementVisitor visitor) { 
-    visitor.visit(this);+    return visitor.visit(this);
   }   }
 } }
  
 class Engine implements CarElement { class Engine implements CarElement {
-  public void accept(CarElementVisitor visitor) { +  public String accept(CarElementVisitor visitor) { 
-    visitor.visit(this);+    return visitor.visit(this);
   }   }
 } }
  
 class Body implements CarElement { class Body implements CarElement {
-  public void accept(CarElementVisitor visitor) { +  public String accept(CarElementVisitor visitor) { 
-    visitor.visit(this);+    return visitor.visit(this);
   }   }
 } }
Ligne 51: Ligne 64:
  
   public Car() {   public Car() {
-    this.elements = new CarElement[] { new Wheel("front left"), +    this.elements = new CarElement[] { new Wheel("front left"), new Wheel("front right"), new Wheel("back left"), 
-      new Wheel("front right"), new Wheel("back left"), +        new Wheel("back right"), new Body(), new Engine() };
-      new Wheel("back right"), new Body(), new Engine() };+
   }   }
  
-  public void accept(CarElementVisitor visitor) {   +  public String accept(CarElementVisitor visitor) { 
-    for(CarElement elem : elements) { +    List<CarElement> numbers = Arrays.asList(elements); 
-      elem.accept(visitor); +    return numbers.stream().map(elem -> elem.accept(visitor)).collect(Collectors.joining("\n")) + "\n" 
-    } +        visitor.visit(this);
-    visitor.visit(this);+
   }   }
 } }
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 void visit(Wheel wheel) {     +  public String visit(Wheel wheel) { 
-    System.out.println("Visiting " + wheel.getName() + " wheel");+    return ("Visiting " + wheel.getName() + " wheel");
   }   }
  
-  public void visit(Engine engine) { +  public String visit(Engine engine) { 
-    System.out.println("Visiting engine");+    return ("Visiting engine");
   }   }
  
-  public void visit(Body body) { +  public String visit(Body body) { 
-    System.out.println("Visiting body");+    return ("Visiting body");
   }   }
-  + 
-  public void visit(Car car) {     +  public String visit(Car car) { 
-    System.out.println("Visiting car");+    return ("Visiting car");
   }   }
 } }
  
 class CarElementDoVisitor implements CarElementVisitor { class CarElementDoVisitor implements CarElementVisitor {
-  public void visit(Wheel wheel) { +  public String visit(Wheel wheel) { 
-    System.out.println("Kicking my " + wheel.getName() + " wheel");+    return ("Kicking my " + wheel.getName() + " wheel");
   }   }
  
-  public void visit(Engine engine) { +  public String visit(Engine engine) { 
-    System.out.println("Starting my engine");+    return ("Starting my engine");
   }   }
  
-  public void visit(Body body) { +  public String visit(Body body) { 
-    System.out.println("Moving my body");+    return ("Moving my body");
   }   }
  
-  public void visit(Car car) { +  public String visit(Car car) { 
-    System.out.println("Starting my car"); +    return ("Starting my car");
-  } +
-+
- +
-// Le programme principal +
-public class VisitorDemo { +
-  public static void main(String[] args) { +
-    CarElement car = new Car(); +
-    car.accept(new CarElementPrintVisitor()); +
-    car.accept(new CarElementDoVisitor()); +
-  } +
-+
-</file> +
- +
-=====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'interface ''CarElementVisitor'' et ''CarElement''. Le reste du code devra être modifié en conséquence pour permettre sa compilation. +
- +
-<file java VisitorDemo.java> +
-// Interface contenant les méthodes pour traiter toutes les implémentations de l'interface CarElement. +
-interface CarElementVisitor { +
-  Object visit(Wheel wheel, Object data); +
-  Object visit(Engine engine, Object data); +
-  Object visit(Body body, Object data); +
-  Object visit(Car car, Object data); +
-+
- +
-// 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 this.name; +
-  } +
-  +
-  public Object accept(CarElementVisitor visitor, Object data) { +
-    return visitor.visit(this, data); +
-  } +
-+
- +
-class Engine implements CarElement { +
-  public Object accept(CarElementVisitor visitor, Object data) { +
-    return visitor.visit(this, data); +
-  } +
-+
- +
-class Body implements CarElement { +
-  public Object accept(CarElementVisitor visitor, Object data) { +
-    return visitor.visit(this, data); +
-  } +
-+
- +
-class Car implements CarElement { +
-  CarElement[] elements; +
-  +
-  public Car() { +
-    this.elements = new CarElement[] { new Wheel("front left"), +
-      new Wheel("front right"), new Wheel("back left"), +
-      new Wheel("back right"), new Body(), new Engine() }; +
-  } +
-  +
-  public Object accept(CarElementVisitor visitor, Object data) {   +
-    for(CarElement elem : elements) { +
-      // Traiter correctement le code retour si nécessaire ici. +
-      elem.accept(visitor, data); +
-    } +
-    return visitor.visit(this, data); +
-  } +
-+
- +
-// Une implémentation du traitement des données +
-class CarElementPrintVisitor implements CarElementVisitor { +
-  public Object visit(Wheel wheel, Object data) {     +
-    System.out.println("Visiting " + wheel.getName() + " wheel"); +
-    return null; +
-  } +
-  +
-  public Object visit(Engine engine, Object data) { +
-    System.out.println("Visiting engine"); +
-    return null; +
-  } +
-  +
-  public Object visit(Body body, Object data) { +
-    System.out.println("Visiting body"); +
-    return null; +
-  } +
-  +
-  public Object visit(Car car, Object data) {     +
-    System.out.println("Visiting car"); +
-    return null; +
-  } +
-+
- +
-class CarElementDoVisitor implements CarElementVisitor { +
-  public Object visit(Wheel wheel, Object data) { +
-    System.out.println("Kicking my " + wheel.getName() + " wheel"); +
-    return null; +
-  } +
-  +
-  public Object visit(Engine engine, Object data) { +
-    System.out.println("Starting my engine"); +
-    return null; +
-  } +
-  +
-  public Object visit(Body body, Object data) { +
-    System.out.println("Moving my body"); +
-    return null; +
-  } +
-  +
-  public Object visit(Car car, Object data) { +
-    System.out.println("Starting my car")+
-    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();
-    // Traiter correctement le code retour si nécessaire. +    System.out.println(car.accept(new CarElementPrintVisitor())); 
-    car.accept(new CarElementPrintVisitor(), null); +    System.out.println(car.accept(new CarElementDoVisitor()));
-    car.accept(new CarElementDoVisitor(), null);+
   }   }
 } }
 </file> </file>
helloworld/design_pattern/visiteur/java.1582047634.txt.gz · Dernière modification : de root