2010年9月25日星期六

Using dependency injection can help make it

Service Locator vs Dependency Injection
The fundamental choice is between Service Locator and Dependency Injection. The first point is that both implementations provide the fundamental decoupling that's missing in the naive example - in both cases application code is independent of the concrete implementation of the service interface. The important difference between the two patterns is about how that implementation is provided to the application class. With service locator the application class asks for it explicitly by a message to the locator. With injection there is no explicit request, the service appears in the application class - hence the inversion of control.

Inversion of control is a common feature of frameworks, but it's something that comes at a price. It tends to be hard to understand and leads to problems when you are trying to debug. So on the whole I prefer to avoid it unless I need it. This isn't to say it's a bad thing, just that I think it needs to justify itself over the more straightforward alternative.

The key difference is that with a Service Locator every user of a service has a dependency to the locator. The locator can hide dependencies to other implementations, but you do need to see the locator. So the decision between locator and injector depends on whether that dependency is a problem.

Using dependency injection can help make it easier to see what the component dependencies are. With dependency injector you can just look at the injection mechanism, such as the constructor, and see the dependencies. With the service locator you have to search the source code for calls to the locator. Modern IDEs with a find references feature make this easier, but it's still not as easy as looking at the constructor or setting methods.

There is no question that Wal-Mart's relentless drive to squeeze out costs has benefited consumers. The giant retailer is at least partly responsible for the low rate of U.S. inflation, and a McKinsey & Co. study concluded that about 12% of the economy's productivity gains in the second half of the 1990s could be traced to Wal-Mart alone.

There is also no question that doing business with Wal-Mart can give a supplier a fast, heady jolt of sales and market share. But that fix can come with long-term consequences for the health of a brand and a business. Vlasic, for example, wasn't looking to build its brand on a gallon of whole pickles. Pickle companies make money on "the cut," slicing cucumbers into spears and hamburger chips. "Cucumbers in the jar, you don't make a whole lot of money there," says Steve Young, a former vice president of grocery marketing for pickles at Vlasic, who has since left the company.

At some point in the late 1990s, a Wal-Mart buyer saw Vlasic's gallon jar and started talking to Pat Hunn about it. Hunn, who has also since left Vlasic, was then head of Vlasic's Wal-Mart sales team, based in Dallas. The gallon intrigued the buyer. In sales tests, priced somewhere over $3, "the gallon sold like crazy," says Hunn, "surprising us all." The Wal-Mart buyer had a brainstorm: What would happen to the gallon if they offered it nationwide and got it below $3? Hunn was skeptical, but his job was to look for ways to sell pickles at Wal-Mart. Why not?


A lot of this depends on the nature of the user of the service. If you are building an application with various classes that use a service, then a dependency from the application classes to the locator isn't a big deal. In my example of giving a Movie Lister to my friends, then using a service locator works quite well. All they need to do is to configure the locator to hook in the right service implementations, either through some configuration code or through a configuration file. In this kind of scenario I don't see the injector's inversion as providing anything compelling.

没有评论:

发表评论