Czytając wredny artykuł na temat wad OOP na rzecz innego paradygmatu natknąłem się na przykład, z którym nie mogę znaleźć zbyt wiele winy.
Chcę być otwarty na argumenty autora i chociaż teoretycznie rozumiem ich argumenty, szczególnie w jednym przykładzie trudno mi sobie wyobrazić, jak można by to lepiej zaimplementować, powiedzmy, w języku FP.
// Consider the case where “SimpleProductManager” is a child of
// “ProductManager”:
public class SimpleProductManager implements ProductManager {
private List products;
public List getProducts() {
return products;
}
public void increasePrice(int percentage) {
if (products != null) {
for (Product product : products) {
double newPrice = product.getPrice().doubleValue() *
(100 + percentage)/100;
product.setPrice(newPrice);
}
}
}
public void setProducts(List products) {
this.products = products;
}
}
// There are 3 behaviors here:
getProducts()
increasePrice()
setProducts()
// Is there any rational reason why these 3 behaviors should be linked to
// the fact that in my data hierarchy I want “SimpleProductManager” to be
// a child of “ProductManager”? I can not think of any. I do not want the
// behavior of my code linked together with my definition of my data-type
// hierarchy, and yet in OOP I have no choice: all methods must go inside
// of a class, and the class declaration is also where I declare my
// data-type hierarchy:
public class SimpleProductManager implements ProductManager
// This is a disaster.
Zauważ, że nie szukam obalenia argumentów autora przeciwko „Czy istnieje jakiś racjonalny powód, dla którego te 3 zachowania powinny być powiązane z hierarchią danych?”.
W szczególności pytam, w jaki sposób ten przykład zostałby modelowany / zaprogramowany w języku FP (rzeczywisty kod, nie teoretycznie)?