Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Last active October 8, 2020 19:39
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/a1a4bd5bacf1b503c1443b21b58bb043 to your computer and use it in GitHub Desktop.
Save pawelpluta/a1a4bd5bacf1b503c1443b21b58bb043 to your computer and use it in GitHub Desktop.
Article: The OOP has been explained wrongly to you - solution
class Basket {
private Laundry laundry;
private Basket(Laundry laundry) {
this.laundry = laundry;
}
static Basket empty() {
return new Basket(Laundry.of(emptyList()));
}
void put(LaundryElement laundryElement) {
laundry = laundry.with(laundryElement);
}
Laundry takeOutWool() {
List<LaundryElement> woolElements = laundryOf(WoolLaundryElement.class);
laundry = laundry.withOut(woolElements);
return Laundry.of(woolElements);
}
Laundry takeOutCotton() {
List<LaundryElement> cottonElements = laundryOf(CottonLaundryElement.class);
laundry = laundry.withOut(cottonElements);
return Laundry.of(cottonElements);
}
Laundry takeOutSilk() {
List<LaundryElement> silkElements = laundryOf(SilkLaundryElement.class);
laundry = laundry.withOut(silkElements);
return Laundry.of(silkElements);
}
private List<LaundryElement> laundryOf(Class<? extends LaundryElement> elementType) {
return laundry.asList().stream().filter(elementType::isInstance).collect(toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment