In Shakespeare's Hamlet, the young Prince of Denmark has a real purpose for pretending to be mad. He is attempting to prove that what his father's ghost has told him is true: his uncle Claudius murdered his father the King. He begins acting erratically in order to see if he can catch Claudius in a lie or prove his guilt in some way. What happens is that he really does go a little over the edge and dips into madness himself - a madness caused by his grief and anger and his zeal to prove that he is right in his suspicions.
Hamlet is actually partly responsible for Ophelia's madness. He has expressed fond feelings for her, and suddenly he is acting crazy and rejecting her completely, which confuses and hurts her. It is when he accidentally kills her father Polonius, however, that Ophelia does truly go mad. Not only is her father dead, but he is dead by Hamlet's hand. She cannot accept the reality of what is happening to her, so she descends into madness.
They are both reacting to the death of a father, but Hamlet's madness is contained and controlled, while Ophelia's consumes her. Of course, you could say that eventually Hamlet is consumed, as well, since his actions lead to his death and the deaths of several others around him, including his mother. Only Horatio is left alive to tell the tale.