Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Last active October 12, 2020 05:45
Show Gist options
  • Save pawelpluta/4ef31b12722e1d220a49cbe049a9efbe to your computer and use it in GitHub Desktop.
Save pawelpluta/4ef31b12722e1d220a49cbe049a9efbe to your computer and use it in GitHub Desktop.
Article: The OOP has been explained wrongly to you - solution
abstract class LaundryElement {
private static final Boolean IS_CLEAN = TRUE;
private final String color;
private final BigDecimal weight;
private Boolean dirty;
private LaundryElement(String color, BigDecimal weight, Boolean dirty) {
this.color = color;
this.weight = weight;
this.dirty = dirty;
}
static LaundryElement wool(String color, BigDecimal weight) {
return new WoolLaundryElement(color, weight, IS_CLEAN);
}
static LaundryElement cotton(String color, BigDecimal weight) {
return new CottonLaundryElement(color, weight, IS_CLEAN);
}
static LaundryElement silk(String color, BigDecimal weight) {
return new SilkLaundryElement(color, weight, IS_CLEAN);
}
String getColor() {
return color;
}
BigDecimal getWeight() {
return weight;
}
void clean() {
this.dirty = FALSE;
}
void dirty() {
this.dirty = TRUE;
}
static class WoolLaundryElement extends LaundryElement {
private WoolLaundryElement(String color, BigDecimal weight, Boolean dirty) {
super(color, weight, dirty);
}
}
static class CottonLaundryElement extends LaundryElement {
private CottonLaundryElement(String color, BigDecimal weight, Boolean dirty) {
super(color, weight, dirty);
}
}
static class SilkLaundryElement extends LaundryElement {
private SilkLaundryElement(String color, BigDecimal weight, Boolean dirty) {
super(color, weight, dirty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment