It seems much too complicated and puzzling to believe that Hamlet is sometimes sane, sometimes pretending to be mad, and sometimes actually mad. In Act 1, Scene 5, he tells Horatio and Marcellus that he may “put an antic disposition on.” No doubt he has decided to pretend to be mad but is only suggesting that he “might” do it. He has at least two good reasons for pretending to be insane. One is that Claudius is keeping a close watch on him, trying to guess what he is thinking, suspecting that he is plotting against him. As Claudius says to Polonius in one of Shakespeare’s most striking metaphors:
There’s something in his soul,
O’er which his melancholy sits on brood,
And I do doubt the hatch and the disclose
Will be some danger… (Act 3, Scene 1)
Hamlet knows that it will be impossible to behave in the same way he did before he was traumatized by the meeting with his father’s ghost, by the revelation that his uncle was a murderer and usurper, and by the possibility that his own mother was an accomplice. Claudius is too clever, too watchful, too suspicious to be easily deceived. And he has others spying on Hamlet as well. By acting insane, Hamlet hopes to be able to hide his true thoughts and feelings from the prying of Claudius, Polonius, Gertrude, Ophelia, and others, who will come to include Rosencrantz and Guildenstern.
A second reason for pretending to be insane is that Hamlet is obviously terribly concerned about Claudius learning anything at all about a “ghost” haunting Elsinore, especially a ghost who resembles the former king and who may have been in contact with his son. What Hamlet has in mind is that if there is any hint of his seeing and talking to his father, Claudius will believe this to be nothing but an insane delusion. If Hamlet has really been in contact with his father’s ghost, then he would surely know everything about the murder Claudius committed, and Claudius might decide to kill Hamlet or have him killed. On the other hand, if Hamlet has only imagined conversing with his father’s ghost, then he could not have learned anything he didn’t already know.
Hamlet may have had a third reason for pretending to be mad. He knows he is going to have to kill Claudius, and the simplest course of action might be to pretend to be doing it in a fit of madness—as he pretended when he killed Polonius and when he was apprehended and brought before the King. The problem here is that it would be nearly impossible for Hamlet to succeed Claudius if everybody thought Hamlet was insane.
Throughout the remainder of the play Hamlet is either mad or mad in fashion, but there is no sufficient reason to believe he is ever temporarily mad.
John Dover Wilson in his book "What Happens In Hamlet" from 1935 wrote that at six points in the play Hamlet actually goes mad. But, I think Wilson goes too far out on a limb at the expense of being definitive, i.e., in answering the questions from the play as suggested by the title of his book. The play does not actually supply an answer to the madness question, though Wilson tries to supply one and we as spectator's have grown accustomed to expect one. As a fully fleshed character as we see Hamlet to be, the question of his sanity should be resolved by plays end. But it isn't.
I would suggest that the play thematically and dramatically does not want to supply one.Thematically the play is about ambiguity and duplicity and one of those expressions or sub-themes is image and reality. If the line between real and feigned madness can be hidden or fudged by artifice how can we diagnose Hamlet?
Dramatically, each of the characters sees in Hamlet what they what to see. Polonius sees madness sprung from frustrated love. Gertrude sees it as her remarriage and his father's death. Claudius along with Rosencrantz and Guildenstern sees ambition lies behind the antics. For the actor playing Hamlet the madness question provides more dramatic range. It is a way for the actor to "act out" and bring to the fore this very question.