A large number of spectroscopic studies have provided evidence of the presence of multiple populations in globular clusters by revealing patterns in the stellar chemical abundances. This paper is aimed at studying the origin of these abundance patterns. We explore a model in which second generation (SG) stars form out of a mix of pristine gas and ejecta of the first generation of asymptotic giant branch stars. We first study the constraints imposed by the spectroscopic data of SG stars in globular clusters on the chemical properties of the asymptotic and super asymptotic giant branch ejecta. With a simple one-zone chemical model, we then explore the formation of the SG population abundance patterns focussing our attention on the Na-O, Al-Mg anticorrelations and on the helium distribution function. We carry out a survey of models and explore the dependence of the final SG chemical properties on the key parameters affecting the gas dynamics and the SG formation process. Finally, we use our chemical evolution framework to build specific models for NGC 2808 and M4, two Galactic globular clusters which show different patterns in the Na-O and Mg-Al anticorrelation and have different helium distributions. We find that the amount of pristine gas involved in the formation of SG stars is a key parameter to fit the observed O-Na and Mg-Al patterns. The helium distribution function for these models is in general good agreement with the observed one. Our models, by shedding light on the role of different parameters and their interplay in determining the final SG chemical properties, illustrate the basic ingredients, constraints and problems encountered in this self-enrichment scenario which must be addressed by more sophisticated chemical and hydrodynamic simulations.