concept Emailer in category dependency injection

appears as: Emailer, Emailer, n Emailer, The Emailer
Dependency Injection: Design Patterns Using Spring and Guice

This is an excerpt from Manning's book Dependency Injection: Design Patterns Using Spring and Guice.

In our example, the Emailer depends on a SpellChecker, a TextEditor, and an AddressBook. This relationship is called a dependency. In other words, Emailer is a client of its dependencies.

Let’s apply the Factory pattern to our Emailer. The Emailer’s code remains the same (as shown in listing 1.2).

Notice that the Factory pattern is very explicit about what kind of Emailer it is going to produce; newFrenchEmailer() creates one with a French spellchecker. Any code that uses French email services is now fairly straightforward:

Emailer service = new EmailerFactory().newFrenchEmailer();

The most important thing to notice here is that the client code has no reference to spellchecking, address books, or any of the other internals of Emailer. By adding a level of abstraction (the Factory pattern), we have separated the code using the Emailer from the code that creates the Emailer. This leaves client code clean and concise.

The value of this becomes more apparent as we deal with richer and more complex object graphs. Listing 1.4 shows a Factory that constructs our Emailer with many more dependencies (also see figure 1.4).

Listing 1.4. A Factory that constructs a Japanese emailer
public class EmailerFactory { public Emailer newJapaneseEmailer() { Emailer service = new Emailer(); service.setSpellChecker(new JapaneseSpellChecker()); service.setAddressBook(new EmailBook()); service.setTextEditor(new SimpleJapaneseTextEditor()); return service; } }
Figure 1.7. EmailerFactory constructs and assembles a Japanese Emailer with various dependencies.

Code that uses such an Emailer is as simple and readable as we could wish: