background preloader

Patterns

Facebook Twitter

Patterns - Index

Patterns - Service Locator. Oracle Technology Network > Java 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. Start for free. Essential Links Developer Spotlight Java EE—the Most Lightweight Enterprise Framework? Blogs Technologies Contact Us About Oracle Cloud Events Top Actions News Key Topics Oracle Integrated Cloud Applications & Platform Services. 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 Extreme Programming (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. Regular Tests I'll begin by illustrating the two styles with a simple example. These two behaviors imply a couple of tests, these look like pretty conventional JUnit tests. 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 class MovieLister... 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. When a command is received, a slave is launched to execute the command while the master resumes listening for more commands (such as the "suspend the last command" command.) In this example we have refined the Master-Slave pattern with the Factory pattern.