I've started reading "Spring Recipes" by Gary Mak. It covers up to 2.5, and we're up to 3. But I've read up to chapter 5 and I still think it's worth reading, because it conceptually explains the concept of spring, as well as providing a great overview of its functionality and lots of working code.
The very first pattern the book discusses is the "Container" pattern. Essentially, instead of instantiating a concrete class into an interface, which introduces a dependency on the concrete class and kinds of defeats the purpose of the interfaces, you instead create a Container which instantiates the concrete class and makes it available via a "get" method which specifies an id.
In the example shown, a "report generator" class is separated into two classes - a report generator class and a report service class. The report service class gets the report generator class from the container's "getComponent" method, specifying "reportGenerator" as the id. Then, when it needs to generate an annual report, monthly report etc., it just makes a call on the report generator object.
The upshot of all this is that the the Report Service doesn't have a dependency on the specific Report Generator (unlike before, when they were combined).
Since the container class also instantiates the Report Service and stores it in the same object map as the report generator, it can be accessed in a main method. The main method instantiates the container, gets the report service from the container, and then calls the appropriate generate method on it.
You could argue that you've made the system more complex by introducing the container, and just kicked the dependency can down the road to the container class. On the other hand, you've separated the dependency-type code (which type of report) from the reporting service code. Perhaps the real gain is that it represents a way to separate distinct functionality into separate classes, in a way that keeps a class that uses an interface from also referring to the concrete class. That's got to be a win.
The main method creates an instance of the container receives a service report from the container, and then calls the appropriate method of creating it.
ReplyDeletemobile billing
Stephen,
ReplyDeleteThanks for your comment :) I would say that the main method creates an instance of the container, obtains the reporting service from it, and then calls the appropriate method on the service to create the report. Is that what you had in mind?
This comment has been removed by the author.
ReplyDeleteHi... thanks for this interesting post. But would you mind discussing the container instance in details 'coz this seems little technical for me. But I am very curious about this topic.
ReplyDeletesms payment software
I appreciated your professional way of writing this post thanks; you have made such a nice blog. I found in your website perfect for my needs it contains wonderful and helpful posts.
ReplyDelete