How to not get attached?

How do you not get attached to someone? I easily get attached and I only get hurt in the end.