vendredi 4 décembre 2015

What pattern am I using in this example of Node.js code?

'use strict';

var type = {};

type.image = require('./search.image.js');
type.video = require('./search.video.js');
type.social = require('./search.social.js')

function search (query, formatter, type) {

    var objectToUse = type['type'];

    objectToUse.setFormatter = formatter;
    objectToUse.setQuery = query;

    objectToUse.exec(function(response) {
        return response;
    });
}

I'm trying to dynamically call a homemade search engine for different types of media. It requires different parsers and to have the data returned in a common format. I'm curious what type of pattern I'm using. If each of the search engines use common code I'll use a mixin. However, I'm curious what is the best pattern to evoke each? And, which pattern this most resembles?

Edit: This is an attempt to make the code a little more concise.

'use strict';

function search (query, formatter, type) {

    var requireStr = './search.' + type + '.js';
    var objectToUse = require(requireStr);

    objectToUse.setFormatter = formatter;
    objectToUse.setQuery = query;

    objectToUse.exec(function(response) {
        return response;
    });
}

Aucun commentaire:

Enregistrer un commentaire