How to program for constraints/rules

Posted by Gaurav on Programmers See other posts from Programmers or by Gaurav
Published on 2011-01-01T20:47:10Z Indexed on 2011/01/01 20:58 UTC
Read the original article Hit count: 436

First the background, during interviews in the past, many times I have been asked to design some or other variation of card game as programming puzzle, and I have tried to design it in OO way, but I have never been satisfied with my solutions. However it was not until recently that I realized that I had been approaching the problem from the wrong direction. Specifically I was trying to solve the problem by modeling individual card as an object.

Problem with this is individual cards don't have any non-trivial intrinsic behavior and therefore are not suitable (or primary) candidate as objects. What is interesting and important about cards are rules and constraints, such as there could be only four suits, or only thirteen cards in each suit. Of course, then there are any number of rules for games.

So my questions are

  1. Are there any idioms/constructs/patterns to program for rules & constraints.
  2. How many in 1 can be applied in conjunction with OO paradigm.

© Programmers or respective owner

Related posts about design

Related posts about design-patterns