Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:32:51+00:00 2026-06-18T11:32:51+00:00

i am looking to simulate interface and DTO in javascript for this problem. an

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:32:52+00:00Added an answer on June 18, 2026 at 11:32 am

    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);
                });
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so i'm looking for a way to simulate an 404 error, i tried this
I have different webpages that simulate single A4 press pages. I'm looking for a
I'm looking to simulate a kiosk mode for Safari on Windows. OSX will not
I'm looking for a jquery plugin to simulate a vertical marquee. I need it
I am looking for a way to simulate mouse and keyboard input in C++.
I am trying to simulate linux command ls using linux api from c. Looking
Looking to do a bit of refactoring... Using NHibernate I have this query currently
Looking for a perl one-liner what will find all words with the next pattern:
Looking at the Ehcahce implementation of net.sf.cache.JS107, I am trying to achieve the following
Looking for best advice on how to do this: I have an insert like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.