最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - Javascript : Interface and DTO - Stack Overflow

programmeradmin4浏览0评论

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.

caller will recieve a dto for instantiation, will feed it with user inputs, and then call the correct implementation of Iworker (method execute).

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

Please tell me if the idea is good or not and any implementation in javascript is welcome

Thanks a lot

Edit : Thanks for help, will take Bergi solution, but i need one more thing So my implementation will be like this :

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.

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.

caller will recieve a dto for instantiation, will feed it with user inputs, and then call the correct implementation of Iworker (method execute).

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

Please tell me if the idea is good or not and any implementation in javascript is welcome

Thanks a lot

Edit : Thanks for help, will take Bergi solution, but i need one more thing So my implementation will be like this :

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.

Share Improve this question edited Feb 9, 2013 at 10:25 riadh gomri asked Feb 8, 2013 at 21:21 riadh gomririadh gomri 8691 gold badge15 silver badges22 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

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.

and 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.

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

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.

any implementation in javascript is welcome

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);
            });
    }
};

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.

JS.Class allows you to create interfaces.

Regarding Interface:

For javascript there is JSDoc @interface.

If you prefer Typescript, go for Interfaces.

If you just want some code hints from the IDE of what is defined and what is not defined, I used JSDoc @typedef recently:

/**
 * @typedef {Object} MyType
 * @property {boolean} foo
 * @property {function} bar
 * @property {(running|paused)} baz
 */
发布评论

评论列表(0)

  1. 暂无评论