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

  • Home
  • SEARCH
  • 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 8189787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:24:00+00:00 2026-06-07T03:24:00+00:00

I am building an application that should take a parameter specifying an implementation of

  • 0

I am building an application that should take a parameter specifying an implementation of a given data structure to use. Say, the super-type of the parameter is Super, and and there are many implementations as SubX, that I provide. There may be also SubY, not known at compile time, provided by the user of my application.

Within the application, the parameter will be passed all around, and will be used create instances of Sub_, invoke methods defined in Super, and used as a parameter in parameterized types.

If it were only for the parameterized type, I could make my classes parameterized too, and that would work. However, I need to create instances of such objects, and so I am wondering what would be the best way of configuring my application.

Should I pass the types plus factories as parameters? I find the factory pattern ugly and would like to avoid this approach. Besides, there would be too many parameters passed around (my application actually requires several of such parameters).

Should I create an object and stick this configuration in there? Could the object object be resolved at runtime, allowing the configuration not be known at compile time?

EDIT: Ideally, I would like having something like the following code. Could you provide a minimum working example based on this?

class Test[Type <: Super] { 
   def main {
       val testVal: Type = new Type()
       testVal.methodDefinedInSuper()
    }
 }
  • 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-07T03:24:02+00:00Added an answer on June 7, 2026 at 3:24 am

    Use factories. If there are too many of them and you don’t want to create a factory of factories to unify them, then use a dependency injection framework to do so. Dependency injection frameworks even support XML configuration, which seems to be in line with what you want to do.

    If you don’t know any dependency injection frameworks, first check whether whatever frameworks you are using don’t already provide it, and, failing that, check out Google Guice.

    Example with a factory:

    trait Factory[T] {
      def newInstance(): T
    }
    
    class Test[Type <: Super](typeFactory: Factory[Type]) { 
      def main {
         val testVal: Type = typeFactory.newInstance()
         testVal.methodDefinedInSuper()
      }
    }
    

    Example with Google GUICE:

    class Test[Type <: Super] { 
      @Inject val testVal: Type = _
      def main {
         testVal.methodDefinedInSuper()
      }
    }
    

    Note that this style of injection on Google Guice is the least recommended of them all. I’ve used it because it’s also the most concise, to give a general view.

    Note, however, that there’s no reason to use Type <: Super in either case. That bound isn’t gaining you anything, so you could rewrite them like this:

    class Test(typeFactory: Factory[Super]) { 
      def main {
         val testVal: Super = typeFactory.newInstance()
         testVal.methodDefinedInSuper()
      }
    }
    
    class Test { 
      @Inject val testVal: Super = _
      def main {
         testVal.methodDefinedInSuper()
      }
    }
    

    Anyway, in the first case you pass the factory as a constructor parameter, and in the second case you use one of Guices bindings to tell Guice what it should use to initialize testVal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: We're building an application that allows our customers to supply data in a
I'm building an application that uses an elaborate API to fetch data from Youtube,
I'm building an application that is seriously slower than it should be (a process
I'm building mvc3 application that should serve about 1000-2000 concurrent users, I implemented Castle
I am building an application that should show an arrow to a certain location.
I am currently building a java-servlet-based web application that should offer its service to
I'm building an application that will take an image of a single person's whole
I'm building an application that needs to query a lot of data that is
I'm building an application that is used by several different customers. Each customer has
I'm building a application that sends a test message to another email, the program

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.