Javascript:接口和DTO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14781574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript : Interface and DTO
提问by riadh gomri
i am looking to simulate interface and DTO in javascript for this problem. an object dto, an object caller and different implementation for the same Interface IWorker.
我正在寻找在 javascript 中模拟接口和 DTO 来解决这个问题。一个对象 dto,一个对象调用者和同一个接口 IWorker 的不同实现。
caller will recieve a dto for instantiation, will feed it with user inputs, and then call the correct implementation of Iworker (method execute).
调用者将收到一个用于实例化的 dto,将用户输入提供给它,然后调用 Iworker 的正确实现(方法执行)。
I want that in my code i have juste one instance of dto, and one instance of caller, so i will have juste to call caller.CallWorker() everytime the user make another choice
我希望在我的代码中我有一个 dto 实例和一个 caller 实例,所以每次用户做出另一个选择时我都必须调用 caller.CallWorker()
Please tell me if the idea is good or not and any implementation in javascript is welcome
请告诉我这个想法是否好,欢迎任何在 javascript 中的实现
Thanks a lot
非常感谢
Edit : Thanks for help, will take Bergi solution, but i need one more thingSo my implementation will be like this :
编辑:感谢您的帮助,将采用 Bergi 解决方案,但我还需要一件事所以我的实现将是这样的:
var caller = {
callWorker: function(obj) {
if(obj.id == 1) Worker1.execute();
if(obj.id == 2) Worker2.execute();
if(obj.id == 2) Worker3.execute();
}
};
but this means that i have to add all worker defintions(one js script per implementation) in the html page. I want to just add dynamically the scripts, in fact active worker depends on a lot of business logic, so i will include them dynamically to have only active workers in the page. How do you recommend me to do ? Do all conditions in caller.callworker ? or there is more elegant approach.
但这意味着我必须在 html 页面中添加所有工作人员定义(每个实现一个 js 脚本)。我只想动态添加脚本,实际上活动工作人员取决于很多业务逻辑,因此我将动态包含它们以使页面中只有活动工作人员。你建议我怎么做?在 caller.callworker 中执行所有条件吗?或者有更优雅的方法。
采纳答案by Bergi
i am looking to simulate interface
我正在寻找模拟界面
There are no "interfaces" in the dynamical typed language JavaScript, as well as there are no classes. The nearest simulation is a function that checks whether a given object has a certain set of methods; yet it cannot make any type or functionality tests.
动态类型语言 JavaScript 中没有“接口”,也没有类。最近的模拟是检查给定对象是否具有特定方法集的函数;但它无法进行任何类型或功能测试。
and DTO
和 DTO
The nearest thing to a DTO is a JSON-serialisable object. That means it has no cycles, does not contain Date
objects or even host objects.
最接近 DTO 的是一个 JSON 可序列化对象。这意味着它没有循环,不包含Date
对象甚至宿主对象。
I want that in my code i have juste one instance of dto, and one instance of caller, so i will have juste to call caller.CallWorker() everytime the user make another choice
我希望在我的代码中我有一个 dto 实例和一个 caller 实例,所以每次用户做出另一个选择时我都必须调用 caller.CallWorker()
That's another design pattern: Singletons. They can be easily coded in JS, since objects can be created on the fly and don't need a class and a constructor that needs to prevent multiple instantiation. Just use object literals for them.
这是另一种设计模式:单例。它们可以很容易地在 JS 中编码,因为对象可以动态创建并且不需要需要防止多次实例化的类和构造函数。只需为它们使用对象文字。
any implementation in javascript is welcome
欢迎使用 javascript 中的任何实现
It's just a simple example, but you can extend it where you need:
这只是一个简单的例子,但你可以在需要的地方扩展它:
function makeDTO(id) {
// creates a basic data structure, with empty or default values or from parameters
return {
id: id,
userInput: [],
validate: false,
…
};
}
var caller = {
callWorker: function(obj) {
// do something
}
};
var dto = makeDTO(14);
caller.callWorker(dto);
var asString = JSON.stringify(dto);
or there is more elegant approach.
或者有更优雅的方法。
Make workers an array or object. For example
使工人成为数组或对象。例如
var workers = {
1: {/* std worker #1 */},
…
};
// then the caller will look like this:
var caller = {
callWorker: function(obj) {
var workerName = obj.id; // just the id? Or something more complex
if (workerName in workers)
workers[workerName].execute(obj);
else
dynamicallyLoadWorker(workerName, function callbackWhenLoaded(worker) {
workers[workerName] = worker;
worker.execute(obj);
});
}
};
回答by marteljn
Since JavaScript is dynamically typed, there is no need for the interface
construct. You can call your method for each of your implementations without inheriting from an interface. In terms of DTO, the same answer really applies. You can use JSON to construct an object however you would like and transfer it over the wire without the need of any of the statically typed constructs you mentioned.
由于 JavaScript 是动态类型的,因此不需要interface
构造。您可以为每个实现调用您的方法,而无需从接口继承。就 DTO 而言,同样的答案确实适用。您可以使用 JSON 构建一个对象,但您可以通过网络传输它,而无需您提到的任何静态类型构造。
回答by Mr. Polywhirl
JS.Classallows you to create interfaces.