Procedural vs Object Oriented vs Functional Programming


  • 1

  • 0
    P

    The following code represents aspects of each programming style.

    It is object-oriented because it encapsulates code into various objects. For instance, the Heart object encapsulates arteries, etc.

    It is functional because its methods have no state and merely pass objects among themselves like a heart pumps blood among its departments.

    Lastly, it is procedural because it takes the gives the computer instructions for what to do, e.g. for loop -> make new.

    class Heart {
        constructor(props) {
    	this.Aortic_Valve = false;
    	this.Mitral_Valve = false;
    	this.Pulmonary_Valve = false;
    	this.Tricuspid_Valve = false;
    	this.Superior_Venae_Cavea(props);
    	this.Inferior_Venea_Cavea(props);
        }
    
        Aorta(blood) {
    	distribute_to_REST(blood);
        }
    
        Inferior_Venea_Cavea(blood) {
    	this.Right_Atrium(blood);
        }
    
        Left_Atrium(lungs) {
    	var blood = lungs.getBlood();
    	this.Mitral_Valve = true;
    	this.Left_Ventricle(blood);
        }
    
        Left_Ventricle(blood) {
    	this.Mitral_Valve = false;
    	this.Aortic_Valve = true;
    	this.Aorta(blood);
        }
    
        Pulmonary_Artery(blood) {
    	this.Pulmonary_Valve = false;
    	this.Left_Atrium(new Abstract_Lungs(blood));
        }
    
        Superior_Venae_Cavea(blood) {
    	this.Right_Atrium(blood);
        }
    
        Right_Atrium(blood) {
    	this.Tricuspid_Valve = true;
    	this.Right_Ventricle(blood);
        }
    
        Right_Ventricle(blood) {
    	this.Tricuspid_Valve = false;
    	this.Pulmonary_Valve = true;
    	this.Pulmonary_Artery(blood);
        }
    }
    
    function distribute_to_REST(blood) {
        console.log(blood);
    }
    
    class Abstract_Lungs {
        constructor(props) {
    	    this.blood = props.map(cell => cell + '+');
        }
        getBlood() { return this.blood; }
    }
    
    let blood = [['rbc', 'wbc', 'p'], ['rbc', 'wbc', 'p'], ['rbc', 'wbc', 'p']];
    
    for (var i = 0; i < blood.length; i++) {
        new Heart(blood[i])
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.