'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;
});
}
我试图为不同类型的媒体动态调用自制搜索引擎。它需要不同的解析器并以通用格式返回数据。我很好奇我正在使用什么类型的模式。如果每个搜索引擎都使用公共代码,我将使用mixin。但是,我很好奇什么是唤起每个模式的最佳模式?而且,这种模式最相似?
答案 0 :(得分:1)
这似乎是战略模式。来自维基百科:
在计算机编程中,策略模式(也称为 策略模式)是一种软件设计模式,可以实现 算法在运行时选择的行为。
策略模式 定义一系列算法,封装每个算法,并制作 算法可以在该族中互换。战略让 算法与使用它的客户独立不同。[1]战略是 有影响力的书籍设计模式中包含的模式之一 由Gamma等人。推广使用模式的概念 描述软件设计。
答案 1 :(得分:0)
据我能从这段代码中了解到,@ James Gaunt似乎对策略是正确的。 这可能是实现它的更好方法,您不需要不需要的代码:
'use strict';
function search (query, formatter, type) {
var objectToUse = require('./search.' + type + '.js');
objectToUse.setFormatter = formatter;
objectToUse.setQuery = query;
objectToUse.exec(function(response) {
return response;
});
}