To detach yourself and observe your ego, just become aware of whats going on at that moment. Say your nervous about standing in line at a grocery store, just tell yourself "My ego is making me feel nervous right now". By being aware that you are suffering, is the ultimate way to overcoming your fears.

You have to realize something sir: it is not you, it is your brain that wishes to fap. It desires the hit of dopamine so badly that it has overtaken you. Put the weak brain back into its place with your awareness . :)