-
-
Save pawelpluta/a1a4bd5bacf1b503c1443b21b58bb043 to your computer and use it in GitHub Desktop.
Article: The OOP has been explained wrongly to you - solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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