Try Out New Website HTML Table Generator

Maternity.js

File: Maternity.js

'use strict';

/**
 * Factory (classe statique de construction d'instances de Robots et Humains)
 * @class Maternity
 * @static
 */
var Maternity = {
    /**
     * Fabrication d'un bipède (Robot ou Humain suivant le type fourni à la méthode)
     * @method build
     * @param {String} type Type de bipède à construire (robot, humain)
     * @param {String} name Nom du bipède
     * @param {Number} age Age (uniquement pour les humains)
     */
    build: function (type, name, age) {
        if (type.toLowerCase() === 'robot') return new Robot(name);
        if (type.toLowerCase() === 'human') return new Human(name, age);
        throw new TypeError('Le type ' + type + ' n\'est pas supporté par la méthode "build" de la maternité');
    }
};



// ---
// Exemples d'utilisation

var toto = Maternity.build('human', 'Toto', 12);
toto.speak();
toto.walk('east');
toto.drink();

var spartacus = Maternity.build('robot', 'Spartacus');
spartacus.speak();
spartacus.walk('north');
spartacus.walk('north');
// spartacus.drink(); // Impossible un Robot ne boit pas!

console.log('-------------');
console.log('Is Toto a Human? => ' + (toto instanceof Human));
console.log('Is Toto a Robot? => ' + (toto instanceof Robot));

console.log('Is Spartacus a Human? => ' + (spartacus instanceof Human));
console.log('Is Spartacus a Robot? => ' + (spartacus instanceof Robot));
console.log('-------------');


// ---
// Polymorphisme

var bipeds = [toto, spartacus];

bipeds.forEach(function (v, i, a) {
    v.speak();

    // 1ère possibilité
    try {
        v.drink();
    }
    catch (error) {
    }

    // 2ème possibilité
    if (typeof v.drink !== 'undefined') v.drink();
});

console.log('--- Fin fichier Maternity.js ------------------');

    (full-width)

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.