A mapping of the total energy of electrons in magnetic materials and alloys on Heisenberg and Ising-like models is utilized to describe bulk and surface magnetic ordering. The theory is applied to dilute metallic and semiconducting magnetic materials, phase diagrams of layers of alloys, and theoretical explanation of ferromagnetism of thin films on a non-magnetic substrate.