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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:23:27+00:00 2026-05-28T16:23:27+00:00

I had to extend my existing code with a Class B. The existing code

  • 0

I had to extend my existing code with a Class B. The existing code uses a singleton in Library. Now Class B (which itself will be available as a singleton as is Class A) needs its own library instance…

I’m wondering what’s the best way to extend the existing code (Class A, Library) such that I have to change the library as less as possible.

public class A 
{
    var lib = Library.Instance;

    public DoSomething()
    {
        lib.DoStuff();
    }    
}

public class B
{
    var lib = Library.Instance;  //wrong! needs its own instance

    public DoSomething()
    {
        lib.DoOtherStuff();
    }    
}

public class Library
{
    public static Library Instance
    {
        get
        {
            return _librarySingleton;
        }
    }

    //library internally uses singleton too!!
}

public static class MyProgram
{
    var a = new A();    //will be an singleton
    var b = new B();    //will be an singleton

    a.DoSomething();
    b.DoSomething();
}

There will never be another class. So two instances will be just fine.

  • 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-28T16:23:28+00:00Added an answer on May 28, 2026 at 4:23 pm

    Unfortunately the Singleton pattern really can’t help you here as this pattern is specifically designed to return one and only one instance for the lifetime of the application.

    Singletons are useful for certain cases like Logging, however they are generally avoided as they are notoriously difficult to mock out, test and extend.

    If possible, I would recommend refactoring the above code to use the Inversion of Control Pattern and constructor injection of the dependency. This is achieved by creating an interface, say ILibrary and having two implementations.

    These implementations can be created once and stored to emulate Singleton-like behaviour in a third helper class. A really good way of doing this is in an enterprise application is to utilise a Dependency Injection Container, which maintains the lifetime of instances (Singleton or Transient) and allows easy injection into constructors.

    A code example using IoC/DI as a pattern would look like this:

    public class A 
    {
        private readonly ILibrary _library;
    
        public A(ILibrary library)
        {
            _library = library;
        }
    
        public DoSomething()
        {
            _library.DoStuff();
        }    
    }
    
    public class B
    {
        private readonly ILibrary _library;
    
        public B(ILibrary library)
        {
            _library = library;
        }
    
        public DoSomething()
        {
            _library.DoStuff();
        }      
    }
    
    public interface ILibrary
    {
        void DoStuff();
    }
    
    public class LibraryTypeOne : ILibrary
    {
        void DoStuff()
        {
             Console.WriteLine("I am library type one");
        }
    }
    
    public class LibraryTypeTwo : ILibrary
    {
        void DoStuff()
        {
             Console.WriteLine("I am library type two");
        }
    }
    
    public static class MyProgram
    {
        var a = new A(new LibraryTypeOne());    // Note, you need to store
        var b = new B(new LibraryTypeTwo());    // these instances somewhere to 
                                                // share throughout the app
    
        a.DoSomething();
        b.DoSomething();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most programing languages have 'include library into your code' function but where is function
We've had to extend our website to communicate user credentials to a suppliers website
Edit: This didn't work because I had: class Animal { ... } class Horse
I had someone help extend the jQuery Cycle plugin and works great (thanks Levi
I am writing a module to extend the Core/Catalog/Product/View/Media.php class so I can expose
I’ve had this problem many times: I have a piece of source code, but
I have an existing WebGL renderer (too much code to be useful to share)
I had initially built a DatePicker. A Calendar Dialog will open next to a
I am trying to extend BitmapDrawable public class CenterClampedBitmapDrawable extends BitmapDrawable I want to
I had some problems figuring out a good title, but hopefully the code examples

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.