2206
Tech & Research / Re: Object-oriented design with Simon
« on: May 10, 2017, 11:09:07 PM »apple-knowing baskets frankly sound like a hack
need to track that [peeling] separately too?
carry my fruits in a tray instead, does the tray now have to copy all the special-casing that the basket is doing?
I assume the work grows too quickly here, yes.
I don't dare to propose a Basket<mainClass, subclass1, subclass2, ...> with compile-time code generation, even when that's probably possible. Maybe this is truly the land for dynamic typing without any static checks at all? But even those raise errors on unsupported methods...
Quote
How does Smalltalk handle [...] still return some sort of default or special value (think null or undefined or similar) for that case?
Most of this is answered by "I erred and Smalltalk doesn't allow it by default." The Smalltalk nil object responds to all messages that the class would normally respond to, and returns the nil object. This looks OK because everything is an object in Smalltalk.
Quote
For languages where it must be done explicitly, it just becomes a matter of if/where/how you want to "hide" the check.
Wise interpretation. The three proposed suggestions happen give the three possible answers: The check can go in the element class, in the client code, or in the container class. I didn't see this!
Quote
I do agree that the situation you are describing [ie. abstracting out the pattern of "if (supported) dothis() else /* do nothing */"] is not uncommon, and it would probably be useful for commonly used languages to support that case better.
Nnnnn, thanks.
Quote from: Colorful Arty
create a boolean attribute for Fruit named isCoreable and then give the subclasses of fruit that are coreable the core() function. Then, you can check each fruit, check if its attribute isCoreable is true, then call the method core().
Yep, this looks like a variant of the fat interface. The Fruit class gets some Apple-specific knowledge.
If you define isCoreable in Fruit, and core() only in the subclasses, then you still have to cast in the client code.
Quote
subclasses of Fruit that are coreable implement an interface that lets them be cored. I'm not sure how many languages use interfaces, but Java does, and Python can use abstract classes instead to solve the problem.
Quote
In some cases you can also simplify the manual checking by, for example, requiring fruits to implement an ICoreable interface if it supports coring. So you just have to check for ICoreable and not more explicitly for specific fruit types.
This is excellent when we don't like to put everything right in base class. The example becomes Banana : Fruit; Orange : Fruit; Apple : Fruit, Coreable.
Defining the Coreable interface is probably wise even if we do it solely for explicit dynamic casting later. Coring seems to be a central concern in our domain, otherwise we wouldn't worry as much about this particular design problem. We can well abstract that into an interface and dynamic-cast to that.
Bonus: Found a webpage with the exact section from Riel's book! I bought the hardcover second-hand last year, it was splendid bedtime reading. At least if you believe that OO is often good.
-- Simon