Another update + dependency injection writing exercise
|tuzgai posted at 2019-02-07T16:52:41Z|
Now that we've had some time back in the classroom I think the class is feeling a little better about things. It helps that the material this week is pretty light, so people have been able to focus on getting up to speed on their projects. I do have an anecdote to share:
Yesterday we were given a small group exercise where we used stream operations to determine whether a given hand of cards was a poker hand. We found the three of a kind pretty challenging, it didn't neatly fit into the methods we knew, even though we knew we could do it easily in a for loop. With my encouragement to not give up, we spent another 10-15 minutes reading up on the available stream operations and collectors and eventually found the groupingBy collector and worked out how to process the set it returned. I did this with a lot of hitting . in netbeans to look at options from the suggestions and googling javadocs.
At the end, one of the people in my group said "Huh, so research is really important?" and had an expression like he'd found a new superpower. It was really exciting to see that revelation and to help encourage him to not give up on problems but keep working away and researching solutions. I'm ahead in the class, so I'm glad that I can role model problem-solving techniques and help everyone push forward.
Anyway, here's my writing exercise on dependency injection:
Dependency injection is an encapsulation strategy that allows the various classes of a project to be initialized with their dependencies in the constructor with the parameters from an external source, rather than called directly from the class. This allows us to more easily manage dependencies and potentially swap them out as needed.
For example, when testing a component we can construct it with a stub dependency set up specifically for testing - this allows us to test our component independently of its dependencies. Another example is that we might want to develop our application with a simple file-based memory system and then switch to a database later. To achieve this, we will use the Spring framework's dependency injection capability. Essentially, we will declare all of our classes and the arguments to their constructors in an XML configuration file and call Spring to inject these dependencies into our app. This way, in our previous examples we can have a 'release' config, a 'testing' config and 'db' and 'file' configs rather than doing a bunch of work in code to manage these.Last edited 2019-04-15T14:14:24Z Permalink