Patterns - Service Locator. Oracle Oracle Oracle Technology Network > Java JavaOne JavaOne Sessions Available JavaOne sessions are now available for replay.
Posted 10/29/14 // Tags: java, JavaOne, Java 7 // Headlines Archive Software Downloads View All Downloads Top Downloads New Downloads What's New Java in the Cloud: Rapidly develop and deploy Java business applications in the cloud. Essential Links Developer Spotlight Building a 3-tiered Application with Java EE JMC Tutorial Create RESTful Services and deploy to Oracle Java Cloud Service with Netbeans Where are my Dump Files? Blogs Technologies Hardware and Software, Engineered to Work Together. Mocks Aren't Stubs. The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing.
Most language environments now have frameworks that make it easy to create mock objects. What's often not realized, however, is that mock objects are but one form of special case test object, one that enables a different style of testing. In this article I'll explain how mock objects work, how they encourage testing based on behavior verification, and how the community around them uses them to develop a different style of testing. I first came across the term "mock object" a few years ago in the XP community. Since then I've run into mock objects more and more. But as often as not I see mock objects described poorly. This difference is actually two separate differences. (In the earlier version of this essay I had realized there was a difference, but combined the two differences together. Regular Tests I'll begin by illustrating the two styles with a simple example.
Inversion of Control Containers and the Dependency Injection pattern. In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application.
Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control". In this article I dig into how this pattern works, under the more specific name of "Dependency Injection", and contrast it with the Service Locator alternative.
The choice between them is less important than the principle of separating configuration from use. One of the entertaining things about the enterprise Java world is the huge amount of activity in building alternatives to the mainstream J2EE technologies, much of it happening in open source. A lot of this is a reaction to the heavyweight complexity in the mainstream J2EE world, but much of it is also exploring alternatives and coming up with creative ideas. Components and Services A Naive Example Inversion of Control. Master-Slave Pattern. The Master-Slave pattern is often used for multi-threaded applications in which many instances of the same problem must be solved.
(Travelling Salesman Problem, for example.) The master creates and launches slaves to solve these instances in "parallel". When all of the slaves have finished, the master harvests the results. Master-Slave pattern is also used for user interfaces and servers. In both cases the master listens for commands coming either from the user or from clients. In this example we have refined the Master-Slave pattern with the Factory pattern.