I am beating the Information Expert GRASP Pattern to death, but Thomas Eyde had a great comment about how one of the code smells I left in the Create a Shopping Cart example actually violated the Information Expert principle. He’s right, and it is one of the code smells I mentioned in the original shopping cart post.
The code in question is as follows:
In the AddItem method above, the ArrayListCart class “makes a copy” of item before placing it in the _items ArrayList. As I mentioned in the Creat a Shopping Cart post, this is not ideal (bogus actually) as ArrayListCart is not the Information Expert on what it takes to make a copy of Item. Item is. Therefore, as Thomas so well put it, Item should have a Copy() method to make a copy of itself.
Now we have a much cleaner solution that follows the Information Expert GRASP Pattern. Although you don’t have to follow these principles as if they are written in stone, you can see that they may help show problems in your code. Check out Applying UML and Patterns for more information on GRASP Patterns.