Animal pet = Shasarak.Pets[0];Now, there's a problem with this. This bit of code is testing the species of my pet, and then deciding what noise to display depending on the pet's species. That's bad OO. The information about what noise the species makes is something that the species class itself should control. Therefore, the code shouldn't say "if you're a cat, say 'miaouw'"; the code should say "make a noise", and then the animal itself responds differently, depending on which species it is.
string speech;
switch pet.Species
case "cat":
speech = "miaouw";
break;
case "dog":
speech = "woof";
break;
case "mouse":
speech = "squeak";
break;
default:
speech = "something unintelligible";
break;
WriteToOutput("Your pet says " + speech);
Animal pet = Shasarak.Pets[0];You then define Noise() as a virtual function on the Animal superclass:
WriteToOutput("Your pet says " + pet.Noise());
public abstract class Animal {Then, on each subclass of animal, you override that method, for example:
public virtual string Noise() {
return "something else";
}
}
public class Cat : Animal {Or:
public override string Noise() {
return "miaouw";
}
}
public class Dog : Animal {And so on.
public override string Noise() {
return "woof";
}
}
class AnimalFactory{
static Animal createAnimal(String type){
Animal animal = null
if(type == "cat"){
animal = new Cat()
}
if(type == "dog"){
animal = new Dog()
}
…
if(animal == "cow"){
animal == new Cow()
}
return animal
}
}
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
}
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
}
print ' <tr><td align="right">Farm Bureau:</td>
<td><input type="hidden" value="'.$county.'">';
switch ($county)
{
case '1_baker': echo 'Baker'; break;
case '2_benton': echo 'Benton'; break;
case '3_clackamas': echo 'Clackamas'; break;
case '4_clatsop': echo 'Clatsop'; break;
case '5_columbia': echo 'Columbia'; break;
case '6_coos_curry': echo 'Coos-Curry'; break;
case '7_crook_wheeler': echo 'Crook-Wheeler'; break;
case '9_deschutes': echo 'Deschutes'; break;
case '10_douglas': echo 'Douglas'; break;
case '11_gilliam': echo 'Gilliam'; break;
case '12_grant': echo 'Grant'; break;
case '13_harney': echo 'Harney'; break;
case '14_hood_river': echo 'Hood River'; break;
case '15_jackson': echo 'Jackson'; break;
case '16_jefferson': echo 'Jefferson'; break;
case '17_josephine': echo 'Josephine'; break;
case '18_klamath_lake': echo 'Klamath-Lake'; break;
case '20_lane': echo 'Lane'; break;
case '21_lincoln': echo 'Lincoln'; break;
case '22_linn': echo 'Linn'; break;
case '23_malheur': echo 'Malheur'; break;
case '24_marion': echo 'Marion'; break;
case '26_multnomah': echo 'Multnomah'; break;
case '27_polk': echo 'Polk'; break;
case '28_sherman': echo 'Sherman'; break;
case '29_tillamook': echo 'Tillamook'; break;
case '30_umatilla_morrow': echo 'Umatilla-Morrow'; break;
case '31_union': echo 'Union'; break;
case '32_wallowa': echo 'Wallowa'; break;
case '33_wasco': echo 'Wasco'; break;
case '34_washington': echo 'Washington'; break;
case '36_yamhill': echo 'Yamhill'; break;
default: echo 'Unknown'; break;
}
print ' County Farm Bureau';
print '
</td>';
County name = oregon.counties[$county];
WriteToOutput ("county.Name() + " County Farm Bureau");
public abstract class County {
public virtual string Name() {
return "unknown county";
}
}
// This is part of one of my login steps
…
ConfirmationCallback alreadyPlaying = new ConfirmationCallback("You are already playing. Throw the other copy out?",
ConfirmationCallback.INFORMATION, ConfirmationCallback.YES_NO_OPTION,
ConfirmationCallback.NO);
cb = createCallbackArray(alreadyPlaying); // creates an array of callbacks to execute
data.getCallbackHandler().handle(cb); // handle the callbacks by prompting the user for a response
if (alreadyPlaying.getSelectedIndex() == ConfirmationCallback.YES) {
dupe.getRegistry().dest(dupe, true);
} else {
throw new FailedLoginException("cannot log in, already logged in!");
}
…
Is it really better to write a 300 count if/else if/else check you think? I just find it more concise when dealing with those kinds of variables to use a switch and simply add cases. I use switch often in PHP and C.
Anyway, I'd never heard of that and was curious.