Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Last active Oct 8, 2020
Embed
What would you like to do?
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