class AnimalFactory{
static Animal createAnimal(String type){
Animal animal = null
switch(type){
case "cow":
animal = new Cow()
break
case "dog"
animal = new Dog()
break
…
case "cow"
animal = new Cow()
break
}
return animal
}
createInstanceOfClassNamed: aStringAnother possibility would be to actually pass in the class itself as a parameter to the method:
| class |
class := (Smalltalk at: aString asSymbol).
^class new
createInstanceOfClass: aClassYou'd have to think a bit about where the class parameter came from, of course; but your string parameter must have come from somewhere too. If the string was a property on an object, then perhaps instead of storing a string you could store a reference to the class instead, or perhaps return the class as the result of a method which is implemented polymorphically (although the latter is starting to lead you into parallel inheritance hierachies, which are also usually not a good idea).
^aClass new
asBooleanAs flumpy goes on to say, it might very well be reasonable to have a method or potentially even a whole class dedicated to obtaining a yes or no answer. In my old Smalltalk application, for example, there's a MessageBox class. That has a class method called #confirm: (which takes a string as argument). That brings up a messagebox with the appropriate text, the title "please confirm", the question-mark icon, and buttons marked Yes and No; it returns a boolean. So you can do:
(#('y', 'yes', 'yeah', 'okay', 'yup', 'absolutely', 'go ahead', 'uh huh', 'yo') includes: self asLowercase) ifTrue: [^true].
(#('n', 'no', 'nope', 'i wouldn''t', 'definitely not', 'uh uh', 'no way') includes: self asLowercase) ifTrue: [^false].
^nil
(MessageBox confirm: 'Are you sure?') ifFalse: [^#cancelled]
The problem is that your statement is incorrect, and hence clearly not a trusim.
By way of illustration: if someone were designing a harness to prevent people from falling off rock-faces, would you require the designer to be well-versed in the finer points of General Relativity?
You need to be aware of the effects of gravity; you don't need to know about its internal implementation. You need to know that if you let go of a rope with nothing to support you, you will fall; you don't need to know that the reason why that happens is because space-time is curved around massive objects. The implications of gravity are that under certain circumstances it is unwise to let go of ropes; you need to know what those situations are; but the reason why gravity causes you to fall under those circumstances doesn't matter in the slightest. If God were to carry out some massive changes in the internal implementation of gravity such that it suddenly started working in a Newtonian rather than Einsteinian fashion, that wouldn't make the slightest difference to safety-harness design.
Needing to know about the consequences of implementation doesn'tin any way imply a need to know about implementation; indeed, if it did, OO in its current form could not exist, because for an object to understand another object's public interface behaviour would require it to be aware of the private details of how that behaviour is implemented: public behaviour is, after all, a consequence of private implementation.