This is called modelling, or observational learning. The principle is that you observe a form of behaviour in someone else and eventually manage to imitate it. It is based on social learning theories and has been used in the treatment of various disorders. You can learn more here.