Thomas Darimont
Erfahrenes Mitglied
Hallo,
dieser Beitrag erklärt das Verhaltensmuster: Strategy
Ausgabe:
Gruß Tom
dieser Beitrag erklärt das Verhaltensmuster: Strategy
Java:
package com.dacos.training.java.patterns.behavioral;
public class StrategyPatternExample {
public static void main(String[] args) {
CoffeMachine cm = new CoffeMachine();
cm.makeCoffe(CoffeType.ESPRESSO);
cm.makeCoffe(CoffeType.LATTE);
}
static class CoffeMachine {
public void makeCoffe(CoffeType type) {
selectStrategy(type).cook();
}
private CoffeCookingStrategy selectStrategy(CoffeType type) {
switch (type) {
case LATTE:
return new LatteCoffeCookingStrategy();
case ESPRESSO:
return new EspressoCoffeCookingStrategy();
}
return null;
}
}
static interface CoffeCookingStrategy {
void cook();
}
static class LatteCoffeCookingStrategy implements CoffeCookingStrategy {
@Override
public void cook() {
System.out.println("Cooking latte");
}
}
static class EspressoCoffeCookingStrategy implements CoffeCookingStrategy {
@Override
public void cook() {
System.out.println("Cooking Espresso");
}
}
enum CoffeType {
LATTE, ESPRESSO
}
}
Ausgabe:
Code:
Cooking Espresso
Cooking latte
Gruß Tom