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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:13:45+00:00 2026-05-17T17:13:45+00:00

I have a class that takes many arguments to create. It’s a sort of

  • 0

I have a class that takes many arguments to create. It’s a sort of an audio processor that needs a sample rate, sample resolution, number of channels etc. Most of the parameters have sane defaults. And most of them should be only settable in the initializer (constructor), because it makes no sense to change them afterwards. I do not want to create a gargantuan initializer with all the parameters because (1) it would be huge and essentially it would only copy the passed values, doing no real work, and (2) the user would have to specify values for all the parameters. What’s a good way to solve this?

I’ve tried writing getters and setters for the params. This means that I could not create the “real” audio processing unit in the constructor, since the parameter values are not known then. I had to introduce a new method (say prepareForWork) so that the users can do something like:

AudioProcessor *box = [[AudioProcessor alloc] init];
[box setSampleRate:…];
[box setNumberOfChannels:…];
[box prepareForWork];
[box doSomeProcessing];

This is nice because it does not require an unwieldy constructor. Also the defaults are set in the initializer which means that I can take a fresh AudioProcessor instance and it could still do some work. The flip side is that (1) there is an extra method that has to be called before the instance can do any real work and (2) the class should refuse to change any of the parameters after prepareForWork was called. Guarding both these invariants would take some boilerplate code that I don’t like.

I thought I could create a special “preset” class that would look like this:

@interface SoundConfig : NSObject {
    NSUInteger numberOfChannels;
    NSUInteger sampleResolution;
    float sampleRate;
}

And then require an instance of this class in the AudioProcessor initializer:

@interface AudioProcessor : NSObject {…}
- (id) initWithConfig: (SoundConfig*) config;

Default values would be set in the SoundConfig initializer, the AudioProcessor constructor would be simple and there would be no invariants to keep watching by hand.

Another approach I thought of was a kind of AudioProcessorBuilder class. You would create an instance of that, set the audio params through accessors and then finally it would build an AudioProcessor instance for you, setting all the properties through a non-public setters so that you could not change them later (and break the invariant).

Now that I write this I favour the SoundConfig approach. How do you solve this, is there a better 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-05-17T17:13:46+00:00Added an answer on May 17, 2026 at 5:13 pm

    See how NSURLConnection is implemented and used. It uses an NSURLRequest object to parameterise it, similarly to the way your SoundConfig object parameterises the AudioProcessor object.

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

Sidebar

Related Questions

I have a class with two constructors, one that takes no arguments and one
I am creating unit tests with DUnit. I have a class that takes quite
I have a generic class that takes a type T . Within this class
So I have a base class with a constructor that takes (T V) .
I have a Login Class which has a function: isCorrect() that takes username and
I have a method that takes a list of entities ( Class es) and
I have a static method that takes a parameter and returns a class. the
I have a web service class that takes an object as an argument. The
I have a class, that has (too) many methods. Most of the methods take
Here's the situation: I have an abstract class with a constructor that takes a

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.