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 6086759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:47:32+00:00 2026-05-23T11:47:32+00:00

I’m working on a kind of parameter values parser library. I’d like to have

  • 0

I’m working on a kind of parameter values parser library. I’d like to have an Parser defined as follows:

public class Parser {

    private ValuesConfiguration configuration;
    private ValuesProvider valuesProvider;
    private ValuesMapper valuesMapper;

    public Parser(ValuesConfiguration configuration) {
        this.configuration = configuration;
    }

    public Result parse(String parameterName) {
        List<Values> values = valuesProvider.getValues(parameterName);
        // do other stuff on values
        // ...
        return valuesMapper.transformValues(values, configuration);
    }
}

I’d like this library clients to be unaware of ValuesProvider and ValuesMapper default implementations and use it like

Result result = new Parser(myConfig).parse("sampleParam");

Although there must be possibility to set their own implementations when needed. I wonder how and where should I init those default implementations and still let clients to set their own if they want. I don’t want to stick to

new DefaultValuesProvider()

etc. in constructor, because default implementation would e.g. access filesystem, so that it would be hard to test (mock them out). I know I can use setters (like in DI) but what about defaults?

EDIT:
After your all answer, I guess it is best here to have setters to allow clients to provider their own implementations of ValuesProvider and ValuesMapper. But how to create default implementations? I’d like to decouple instantiation from logic, so I don’t want to use new DefaultValueProvider() here. Is the factory pattern applicable here? If so how and where should I use it?

  • 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-05-23T11:47:33+00:00Added an answer on May 23, 2026 at 11:47 am

    How about:

    public void setValuesProvider(ValuesProvider valuesProvider) {
        this.valuesProvider = valuesProvider;
    }
    
    public Result parse(String parameterName) {
        if (valuesProvider == null) {
            valuesProvider = new DefaultValuesProvider();
        }
    
        List<Values> values = valuesProvider.getValues(parameterName);
        // do other stuff on values
        // ...
        return valuesMapper.transformValues(values, configuration);
    }
    

    As Mark points out, constructor injection might be the better way to go. That would look as follows:

    public Parser(ValuesConfiguration configuration) {
        this(configuation, new DefaultValuesProvider());
    }
    
    public Parser(ValuesConfiguration configuration, ValuesProvider valuesProvider) {
        this.configuration = configuration;
        this.valuesProvider = valuesProvider;
    }
    
    
    public Result parse(String parameterName) {
        List<Values> values = valuesProvider.getValues(parameterName);
        // do other stuff on values
        // ...
        return valuesMapper.transformValues(values, configuration);
    }
    

    I also agree with his summary of advantages:

    This would enable you to make the dependency final/readonly. Property Injection provides quite weak guarantees about invariants – e.g. you could keep changing the dependency on the same instance, and that’s probably not what you want.

    However, there are also disadvantages:

    1. The number of constructors is exponential in the number of optional parameters, causing a lot of delegation and duplicated JavaDoc for the parameters. Therefore, I’d only use constructor injection if there are few optional dependencies. You have 2, which about my pain threshold for this kind of thing.
    2. Constructor injection ist not very subclassing friendly, as the subclass constructors will have to redeclare the super class dependencies. In your case, a subclass that wishes to expose all configuration options will need 4 constructors … and if it adds an optional parameter it better shouldn’t to this with constructor injection, as this would require another 4 constructors.

    My recommendation therefore is to use setter injection, and if it is necessary to prevent the reassignment of dependences, do:

    public void setValuesProvider(ValuesProvider valuesProvider) {
        if (this.valuesProvider != null) {
            throw new IllegalStateException("Dependency already set");
        }
        this.valuesProvider = valuesProvider;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.