Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a small game of life implementation with Spring, but trying to avoid referencing the Spring context within my code. I have two interfaces, Board and Cell. In each implementation of Board it will create a 2d array of Cells at some point. The problem is that since I'm trying not to reference Spring in my code, I can't use the Spring context to get an instance of the Cell.

I was thinking that it would be a good idea to create a CellFactory to create Cells, and then in the implementation give it the Cell class that I want to use.

So my question is, is this a bad design? If so what would be a better way of achieving this?

share|improve this question
    
You may try @autowired annotation – jatanp Mar 13 at 14:18
    
Why are you using Spring if you are trying not to reference Spring? You are fighting against the framework and surprised that you are having difficulty? – user40980 Mar 14 at 18:44
    
You can use javaee dépendency injection. It's less powerfull that Spring oness but if it's enough for you Spring is compatible with them. But having classes that depends on Spring is not a problem if you're writing an application (not a library). There is pretty much 99.9999999% chance that you won't move away from Spring. – Walfrat Mar 15 at 8:42

Infact Spring implementation is much simpler and modular. Since u already have interfaces why not instantiate them via spring? Instance would be singleton by default. You can inject cell interface in constructor at runtime using Spring (DI). Here is reference to Spring DI

http://www.mkyong.com/spring/spring-dependency-injection-di/

However if still you don't wish to use Spring then, you could implement cell with singleton pattern and get the instance when required (make sure to keep it thread safe)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.