Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Last active October 8, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpluta/53bac3cb5d28e5eb8549a28aa3cb439f to your computer and use it in GitHub Desktop.
Save pawelpluta/53bac3cb5d28e5eb8549a28aa3cb439f to your computer and use it in GitHub Desktop.
Article: The OOP has been explained wrongly to you - solution
class WashingController {
private final WashingService washingService;
WashingController(WashingService washingService) {
this.washingService = washingService;
}
void washWool() {
Collection<LaundryElement> laundry = getAllLaundry().stream()
.filter(WoolLaundryElement.class::isInstance).collect(toList());
WashingMachine washingMachine = new WashingMachine();
Integer spinSpeed = getRequestedSpinSpeed();
Integer temperature = getRequestedTemperature();
washingService.washWool(washingMachine, laundry, spinSpeed, temperature);
}
void washCotton() {
Collection<LaundryElement> laundry = getAllLaundry().stream()
.filter(CottonLaundryElement.class::isInstance).collect(toList());
WashingMachine washingMachine = new WashingMachine();
Integer spinSpeed = getRequestedSpinSpeed();
Integer temperature = getRequestedTemperature();
washingService.washCotton(washingMachine, laundry, spinSpeed, temperature);
}
void washSilk() {
Collection<LaundryElement> laundry = getAllLaundry().stream()
.filter(SilkLaundryElement.class::isInstance).collect(toList());
WashingMachine washingMachine = new WashingMachine();
Integer spinSpeed = getRequestedSpinSpeed();
Integer temperature = getRequestedTemperature();
washingService.washSilk(washingMachine, laundry, spinSpeed, temperature);
}
private Collection<LaundryElement> getAllLaundry() {
Collection<LaundryElement> allLaundry = new ArrayList<>();
// fill the allLaundry collection with available laundry by any way,
// e.g. obtaining it from repositories, providers, services
return allLaundry;
}
private Integer getRequestedSpinSpeed() {
return new Random().nextInt(1200);
}
private Integer getRequestedTemperature() {
return new Random().nextInt(95);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment