ortense
4/20/2016 - 9:05 PM

factory function and composition in es6

factory function and composition in es6

/* basic factorys */

const makeAnimal = state => ({
    comer: () => `${state.name} está comendo`,
    respirar: () => `${state.name} respira`
})

const makeMachine = state => ({
    recarregar: () => `${state.name} está recarregando`,
    ligar: () => `${state.name} está ligado`,
    desligar: () => `${state.name} está desligado`,
})

const makeMeower = state => ({
    miar: () => `${state.name}: Miau!`
})

const makeKiller = state => ({
    matar: target => `${state.name} mata ${target}`
})

const makeCleaner = state => ({
    limpar: target => `${state.name} limpa ${target}`
})

/* compound factorys */

const makeCat = state =>
    Object.assign({},
        makeMeower(state),
        makeAnimal(state))

const makeKillerRobot = state =>
    Object.assign({},
        makeKiller(state),
        makeMachine(state))

const makeKillerCatRobot = state =>
    Object.assign({},
        makeMeower(state),
        makeKiller(state),
        makeMachine(state))
        
const makeKillerClearCatRobot = state =>
    Object.assign({},
        makeMeower(state),
        makeKiller(state),
        makeCleaner(state),
        makeMachine(state))

const minato = makeKillerClearCatRobot({name: minato})

/**
{
    miar: [Function],
    matar: [Function],
    limpar: [Function],
    recarregar: [Function],
    ligar: [Function],
    desligar: [Function]
}
**/