1

I am running a zoo application.

My zoo includes an abstract class of 'animal', and several deriving classes - 'zebra', 'elephant', 'orangutan', 'baboon' and so on. Of each class I have several instances.

My question is: I want to check out if two animal instances can mate.

The business logic is divided to two parts:

  1. I want to check if each mating partner is fit for mating, e.g. not too young or too old or sick etc.
  2. I want to check if the two partners match - e.g. a zebra cannot mate an elephant, but an orangutan can mate with a baboon.

I assume the first requirement would be implemented by an abstract function which would reside under the animal baseclass. But what about the second requirement?

How would you design the classes in the most general matter that adding new types of animals would not require much of an overhead?

  • 2
    Why would you need a design pattern? Why wouldn't a function that takes both objects and checks for compatibility work? – Michael Shaw Mar 4 '14 at 20:01
  • 1
    Well, I assume that might work too, but how would the function look like? Would it need multiple overloads for the different animals? – Berlo Mar 4 '14 at 20:19
  • It would depend on what it does. What decides whether animals can mate? I would have assumed it would just check for the same species, but you said that orangutans and baboons can mate. If there's a logic behind it, you could implement that logic, if it's random, you could do a table lookup. – Michael Shaw Mar 4 '14 at 22:02
  • Well, there is logic to it. Each animal (specie) decides for itself. – Berlo Mar 5 '14 at 18:28
  • There are a number of ways to accomplish this but the wrong way is to implement this in the Animal or its derived classes. If you implement it in those classes then every time you add a new species you will potentially need to update every single animal-derived class. IOW, that means the design is not extensible. Michael's function idea can work, at least then only one place needs to change. Another option would be to dynamically determine acceptable partners at object creation time then u can get away with it being in the animal class. Adding new species only requires config file/db changes. – Dunk Mar 5 '14 at 19:58
1

According to Uncle Bob in Clean Code this is a typical example of when to write more procedural code and less object oriented. You want the flexibility to add "data structures", so you need to put the logic elsewhere.

Data/Object Anti-Symmetry

  • Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures.
  • OO code makes it easy to add new classes without changing existing functions.
  • Procedural code makes it hard to add new data structures because all the functions must change.
  • OO code makes it hard to add new functions because all the classes must change.

That's why Michael instinctively advised to have a function determine if 2 animals can mate.

1

Depends on the actual requirements... What do you mean with 'not require much of an overhead'?

Do you want to be able to add animals dynamically, at runtime (in a plugin)? Add an abstract method to Animal:

bool acceptsMate(Animal);

When checking for possible mates, if one them accepts the other as mate, they can go at it. Added a new animal in a plugin library means implementing this method, and the other existing animals need not know of this new species to mate with them.

If you do not have this requirement, I would use a lookup table, where pairs of compatible animals are listed.

If this is a school question, I guess they are hinting at the visitor pattern, which imho is a bad fit for this problem. You would need to duplicate code for each match, because there are 2 ways to query if they can mate.

AnimalA->visitCanMateWith(AnimalB)

or

AnimalB->visitCanMateWith(AnimalA)
  • I don't need an option to add animals at run-time, but rather have a simple way to add new animal species later on during the development of the application. This is not a school question; I was just hoping that there would be some design pattern that would fit my need. – Berlo Mar 5 '14 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.