« Patrons de conception/Monteur » : différence entre les versions

Contenu supprimé Contenu ajouté
imported>Chicobot
Frór (discussion | contributions)
Ligne 27 :
===Java===
 
<source lang="java">
''<span style="color: green;">/** "Product" */</span>''
/* Produit */
<span style="color: #7F0055; font-weight: bold">class</span> Pizza {
class Pizza {
<span style="color: #7F0055; font-weight: bold">private</span> String <span style="color: blue;">dough</span> = <span style="color: blue;">""</span>;
private String pate = "";
<span style="color: #7F0055; font-weight: bold">private</span> String <span style="color: blue;">sauce</span> = <span style="color: blue;">""</span>;
private String sauce = "";
<span style="color: #7F0055; font-weight: bold">private</span> String <span style="color: blue;">topping</span> = <span style="color: blue;">""</span>;
private String garniture = "";
 
<span style="color: #7F0055; font-weight: bold">public void</span> setDough(String dough) { <span style="color: #7F0055; font-weight: bold">this</span>.<span style="color: blue">dough</span> = dough; }
<span style="color: #7F0055; font-weight: bold">public void</span> setSaucesetPate(String saucedough) { <span style="color: #7F0055; font-weight: bold">{ this</span>.<span style="color: blue">sauce</span>pate = saucepate; }
public void setSauce(String sauce) { this.sauce = sauce; }
<span style="color: #7F0055; font-weight: bold">public void</span> setTopping(String topping) { <span style="color: #7F0055; font-weight: bold">this</span>.<span style="color: blue">topping</span> = topping; }
public void setGarniture(String garniture) { this.garniture = garniture; }
}
}
 
 
''<span style="color: green">/** "Abstract Builder" */</span>''
/* Monteur */
<span style="color: #7F0055; font-weight: bold">abstract class</span> PizzaBuilder {
abstract class MonteurPizza {
<span style="color: #7F0055; font-weight: bold">protected</span> Pizza <span style="color: blue">pizza</span>;
protected Pizza pizza;
 
<span style="color: #7F0055; font-weight: bold">public</span> Pizza getPizza() { <span style="color: #7F0055; font-weight: bold">return</span> <span style="color: blue;">pizza</span>; }
public Pizza getPizza() { return pizza; }
<span style="color: #7F0055; font-weight: bold">public void</span> createNewPizzaProduct() { <span style="color: blue;">pizza</span> = <span style="color: #7F0055; font-weight: bold">new</span> Pizza(); }
public void creerNouvellePizza() { pizza = new Pizza(); }
 
<span style="color: #7F0055; font-weight: bold">public abstract void</span> buildDough();
<span style="color: #7F0055; font-weight: bold">public abstract void</span> buildSaucemonterPate();
<span style="color: #7F0055; font-weight: bold">public abstract void</span> buildToppingmonterSauce();
public abstract void monterGarniture();
}
}
 
''<span style="color: green;">/** "ConcreteBuilder" */</span>''
/* MonteurConcret */
<span style="color: #7F0055; font-weight: bold">class</span> HawaiianPizzaBuilder <span style="color: #7F0055; font-weight: bold">extends</span> PizzaBuilder {
class MonteurPizzaHawaii extends MonteurPizza {
<span style="color: #7F0055; font-weight: bold">public void</span> buildDough() { <span style="color: blue;">pizza</span>.setDough(<span style="color: blue;">"cross"</span>); }
public void monterPate() { pizza.setPate("croisée"); }
<span style="color: #7F0055; font-weight: bold">public void</span> buildSauce() { <span style="color: blue;">pizza</span>.setSauce(<span style="color: blue;">"mild"</span>); }
public void monterSauce() { pizza.setSauce("douce"); }
<span style="color: #7F0055; font-weight: bold">public void</span> buildTopping() { <span style="color: blue;">pizza</span>.setTopping(<span style="color: blue;">"ham+pineapple"</span>); }
public void monterGarniture() { pizza.setGarniture("jambon+ananas"); }
}
}
 
''<span style="color: green;">/** "ConcreteBuilder" */</span>''
/* MonteurConcret */
<span style="color: #7F0055; font-weight: bold">class</span> SpicyPizzaBuilder <span style="color: #7F0055; font-weight: bold">extends</span> PizzaBuilder {
class MonteurPizzaPiquante extends MonteurPizza {
<span style="color: #7F0055; font-weight: bold">public void</span> buildDough() { <span style="color: blue;">pizza</span>.setDough(<span style="color: blue;">"pan baked"</span>); }
public void monterPate() { pizza.setPate("feuilletée"); }
<span style="color: #7F0055; font-weight: bold">public void</span> buildSauce() { <span style="color: blue;">pizza</span>.setSauce(<span style="color: blue;">"hot"</span>); }
public void monterSauce() { pizza.setSauce("piquante"); }
<span style="color: #7F0055; font-weight: bold">public void</span> buildTopping() { <span style="color: blue;">pizza</span>.setTopping(<span style="color: blue;">"pepperoni+salami"</span>); }
public void monterGarniture() { pizza.setGarniture("pepperoni+salami"); }
}
}
 
 
''<span style="color: green;">/** "Director" */</span>''
/* Directeur */
<span style="color: #7F0055; font-weight: bold">class</span> Waiter {
class Serveur {
<span style="color: #7F0055; font-weight: bold">private</span> PizzaBuilder <span style="color: blue;">pizzaBuilder</span>;
private MonteurPizza monteurPizza;
 
<span style="color: #7F0055; font-weight: bold">public void</span> setPizzaBuilder(PizzaBuilder pb) { <span style="color: blue;">pizzaBuilder</span> = pb; }
public void setMonteurPizza(MonteurPizza mp) { monteurPizza = mp; }
<span style="color: #7F0055; font-weight: bold">public</span> Pizza getPizza() { <span style="color: #7F0055; font-weight: bold">return</span> <span style="color: blue;">pizzaBuilder</span>.getPizza(); }
public Pizza getPizza() { return monteurPizza.getPizza(); }
 
<span style="color: #7F0055; font-weight: bold">public void</span> constructPizza() {
public void construirePizza() {
<span style="color: blue;">pizzaBuilder</span>.createNewPizzaProduct();
monteurPizza.creerNouvellePizza();
<span style="color: blue;">pizzaBuilder</span>.buildDough();
monteurPizza.monterPate();
<span style="color: blue;">pizzaBuilder</span>.buildSauce();
monteurPizza.monterSauce();
<span style="color: blue;">pizzaBuilder</span>.buildTopping();
monteurPizza.monterGarniture();
}
}
}
 
 
''<span style="color: green;">/** A customer ordering a pizza. */</span>''
/* Un client commandant une pizza. */
<span style="color: #7F0055; font-weight: bold">class</span> BuilderExample {
class ExempleMonteur {
<span style="color: #7F0055; font-weight: bold">public static void</span> main(String[] args) {
public static void main(String[] args) {
Waiter waiter = <span style="color: #7F0055; font-weight: bold">new</span> Waiter();
Serveur serveur = new Server();
PizzaBuilder hawaiian_pizzabuilder = <span style="color: #7F0055; font-weight: bold">new</span> HawaiianPizzaBuilder();
MonteurPizza monteurPizzaHawaii = new MonteurPizzaHawaii();
PizzaBuilder spicy_pizzabuilder = <span style="color: #7F0055; font-weight: bold">new</span> SpicyPizzaBuilder();
MonteurPizza monteurPizzaPiquante = new MonteurPizzaPiquante();
 
waiter.setPizzaBuilder( hawaiian_pizzabuilder );
serveur.setMonteurPizza(monteurPizzaHawaienne);
waiter.constructPizza();
serveur.construirePizza();
 
Pizza pizza = waiter.getPizza();
Pizza pizza = serveur.getPizza();
}
}
}
</source>
 
== Voir aussi ==