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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:15:48+00:00 2026-05-26T06:15:48+00:00

In our project, we have a class KnowledgeBaseManager, which gets used by other classes

  • 0

In our project, we have a class KnowledgeBaseManager, which gets used by other classes as follows:

KnowledgeBaseManager manager = KnowledgeBaseManager.get();
manager.foo();

KnowledgeBaseManager holds a static variable standardKnowledgeBaseManager which gets initialized when used for the first time:

class KnowledgeBaseManager {
  private static KnowledgeBaseManager standardKnowledgeBaseManager = null;
  public static KnowledgeBaseManager get() {
    if (standardKnowledgeBaseManager == null) {
      standardKnowledgeBaseManager = new KnowledgeBaseManager();
      // initialize  standardKnowledgeBaseManager with appropriate knowledge base
    }
    return standardKnowledgeBase;
 }

Furthermore, we have a parametrized constructor

public static KnowledgeBaseManager get(OntModel model) {...}

which we use until now just for unit test, where we need a KnowledgeBaseManager with a test knowledge base in the background.

Now, we have the following challenge: For development, we want that the application uses a KB-manager with another knowledge base in the background (because of speed). To be more specific, we build a web application with Wicket. So we want to declare somewhere at the start of the app, which knowledge base and KnowledgeBaseManager is used in the application (depending on wheter we are in development or deployment). The code for using the KB-manager (like

 KnowledgeBaseManager manager = KnowledgeBaseManager.get(); 

now) should not be changed for that.

The question is: what’s the best architecture for that?

I was thinking on using a dependency injection framework like PicoContainer or Guice, but don’t have any experience with it and I am not sure if that would be overhead for that particular problem. Any suggestions for best practices our case?

  • 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-26T06:15:49+00:00Added an answer on May 26, 2026 at 6:15 am

    You are implementing the singleton pattern- and if your code is multithreaded, you are implementing it wrong (see http://java.sun.com/developer/technicalArticles/Programming/singletons/ )

    Moreover, as you have discovered, singletons (i.e. global variables) are bad for testing. Dependency injection is one of the answers. Ignore all frameworks first. It means you write your KnowledgeBaseManager in the traditional way- a class with constructor and methods, no static singletonish-factorish methods. Code using KnowledgeBaseManager does not instantiate or lookup the KnowledgeBaseManager- it rather receives it via constructor or setter method (I prefer the former):

    public class ClassUsingKnowledgeBaseManager {
    
      protected final KnowledgeBaseManager knowledgeBaseManager;
    
      public ClassUsingKnowledgeBaseManager(KnowledgeBaseManager knowledgeBaseManager) {
        this.knowledgeBaseManager = knowledgeBaseManager;
      }
    
      // ...
    
    }
    

    Note that given that the class does not lookup/instantiate the manager, it does not care whether you are using a singleton or what, and that you can easily instantiate a different manager in your test without touching your code in any way.

    After you structure your code in this manner, you might find that you end up with big ugly classes that just instantiate all objects you need, that you need some extra code to tie objects to different scopes (i.e. session or request-scoped objects) and to use config files to control the instantiation… if you find that you are writing lots of repetitive or boilerplate code, you can check out the DI-frameworks, which will probably save you from that.

    But for a lot of programs, you might be able to write DI-style code without using a DI-framework.

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

Sidebar

Related Questions

Which static class initializes first if we have one more static classes in our
We have a smelly code in our project. A lot of values which used
We have a class LogManager in our Java project which looks like this: public
We have a servlet project, which contains (among many other classes) an interface that
In our project we have UI and logic (which may be represented as a
Our project is held in a SourceSafe database. We have an automated build, which
We have been using Spring Remoting in our project for sometime. It is used
In a project at work we have a certain value type class in our
In our web project, we have a custom Database Connection class that creates the
So, I have a grad class on artificial intelligence and our final project was

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.