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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:15:05+00:00 2026-05-24T02:15:05+00:00

I am implementing a program which uses a shared utility class with singleton behavior.

  • 0

I am implementing a program which uses a shared utility class with singleton behavior.

One instance of the utility class is created in the main thread and passed to all other objects instantiated:

SomeUtil util = new SomeUtil();

...

Foo foo = new Foo(util, arg1, arg2)
Bar bar = new Bar(util, arg3, arg4, arg5) 

Is there some more elegant way of achiving this (i.e. design pattern)?

  • 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-24T02:15:06+00:00Added an answer on May 24, 2026 at 2:15 am

    As others have mentioned, Singleton can be an alternative. Note though that your current design is easy to unit test (since you are injecting the SomeUtil dependency, which can thus easily be replaced by a mock object during unit tests), while Singleton makes unit testing awkward and difficult:

    • it makes your objects depend on global state, thus it is more difficult to setup your tests correctly, and easy to make mistakes (e.g. by forgetting to initialize the Singleton properly for a specific test),
    • it is more difficult to understand the code since you can’t easily identify whether or not a given piece of code is dependent on global state, except by actually reading the whole code.

    That being said, if it is a real utility class, i.e. it has no internal state, and it is not dependent on anything which would make unit testing difficult (like a DB, or file system), it can be OK to use it as a Singleton (although this begs the question why do you need to instantiate it at all – usually utility classes have only static methods, and a private constructor to prevent instantiation).

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

Sidebar

Related Questions

I'm implementing a single-producer single-consumer queue, by which one thread waits for the global
I'm writing a c# application which uses automation to control another program. Naturally that
I worte a little Web Proxy program that uses a TcpListener, which listen on
I'm implementing a program which gets text from various sites and checks which ones
I have a class (control), implementing ICustomTypeDescriptor, which is used both at design-time and
I am implementing a haskell program which compares each line of a file with
I'm implementing a classical map-reduce program in which I have a parent that spwans
I'm implementing a system to send Messages between different parts of a program I'm
I'm implementing a process elevation helper for Windows. It's a program that will run
When implementing Quicksort, one of the things you have to do is to choose

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.