0

we are developing an application. The application will be deployed in a proprietary event processing engine. We are not supposed to use any api such as spring core for DI. There are not proprietary DI frameworks yet. So the idea is to write one and a simple one.

Can any one please provide some inputs.

My idea is to write a factory class which has static methods in it. The static methods will return instances of the classes we want. For now we only want a single instance. I am assuming the below kind of code

public final class MyFactory {

    private static ClassA classA = new ClassA();
    private static ClassB classB = new ClassB();

    private MyFactory() {
        throw new CustomException("Cannot create instance");
    }

    public static ClassA getClassAInstance() {
        return classA;
    }

    public static ClassB getClassBInstance() {
        return classB;
    }
}

Later I will use it like this

public class SomeRandomClass {

    private ClassA classA = MyFactory.getClassAInstance();
}

Other thing I see is I need not test ClassA and ClassB. Testing SomeRandomClass will cover ClassA and ClassB. Because static content is always loaded first. So while testing SomeRandomClass I always have ClassA instance in it. So writing a junit on some method in SomeRandomClass will invoke methods in ClassA. Is this good?

Is it the right way I am doing? Can I improve it even?

8
  • 8
    Why are you supposed to reinvent a very complicated wheel instead of using working, tested, and understood frameworks? Commented Dec 3, 2013 at 14:45
  • Guice is not complex, you could try it. Commented Dec 3, 2013 at 14:48
  • Try to look at the code behind Guice - yes it is complex. But its usage is simple, just like CDI/Weld. That's why it is better to use what already exists. The thing is that in what is presented in this thread so far, I see no real reason to actually apply dependency injection just yet. Dependency injection is not a drop-in replacement for object instantiation. Commented Dec 3, 2013 at 14:51
  • 1
    A "simple" DI would be as simple as doing a forName on a string value somewhere. What specific requirements do you really have? Commented Dec 3, 2013 at 15:47
  • 1
    What you are doing with the MyFactory is called the Service Locator pattern; it is considered to be an anti-pattern. You should use constructor injection instead. Commented Dec 3, 2013 at 16:32

1 Answer 1

2

For starters, the factory API shouldn't reference the concrete class implementations directly like that. It kind of defeats the purpose. You won't be able to change the concrete classes without recompiling and you won't be able to do things like stubbing out interfaces for testing and development.

Then, assuming you want singletons (which is not how your example is written), you'll need to make sure your factory methods are thread safe in how they produce the singletons.

You should at a minimum have your factory return true singleton instances of interfaces. Then, you could implement some kind of configuration system and use the Java reflection API to determine which concrete classes should be created at runtime. This will also enable you to do things like stub out the interfaces for testing or development.

This isn't really DI. There's a lot more to it and it has benefits in readability/writability/configurability/maintainability that go far beyond what a factory can provide. I'm not sure why using Spring would be a problem in proprietary software. AFAIK Spring's license doesn't force code to be open source or free...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.