2010年9月25日星期六

If you have multiple ways to construct a valid object

If you have multiple ways to construct a valid object, it can be hard to show this through constructors, since constructors can only vary on the number and type of parameters. This is when Factory Methods come into play, these can use a combination of private constructors and setters to implement their work. The problem with classic Factory Methods for components assembly is that they are usually seen as static methods, and you can't have those on interfaces. You can make a factory class, but then that just becomes another service instance. A factory service is often a good tactic, but you still have to instantiate the factory using one of the techniques here.

Constructors also suffer if you have simple parameters such as strings. With setter injection you can give each setter a name to indicate what the string is supposed to do. With constructors you are just relying on the position, which is harder to follow.

If you have multiple constructors and inheritance, then things can get particularly awkward. In order to initialize everything you have to provide constructors to forward to each superclass constructor, while also adding you own arguments. This can lead to an even bigger explosion of constructors.

Despite the disadvantages my preference is to start with constructor injection, but be ready to switch to setter injection as soon as the problems I've outlined above start to become a problem.

This issue has led to a lot of debate between the various teams who provide dependency injectors as part of their frameworks. However it seems that most people who build these frameworks have realized that it's important to support both mechanisms, even if there's a preference for one of them.

No one wants to end up in what is known among Wal-Mart vendors as the "penalty box"--punished, or even excluded from the store shelves, for saying something that makes Wal-Mart unhappy. (The penalty box is normally reserved for vendors who don't meet performance benchmarks, not for those who talk to the press.)

"You won't hear anything negative from most people," says Paul Kelly, founder of Silvermine Consulting Group, a company that helps businesses work more effectively with retailers. "It would be committing suicide. If Wal-Mart takes something the wrong way, it's like Saddam Hussein. You just don't want to piss them off."

As a result, this story was reported in an unusual way: by speaking with dozens of people who have spent years selling to Wal-Mart, or consulting to companies that sell to Wal-Mart, but who no longer work for companies that do business with Wal-Mart. Unless otherwise noted, the companies involved in the events they described refused even to confirm or deny the basics of the events.

To a person, all those interviewed credit Wal-Mart with a fundamental integrity in its dealings that's unusual in the world of consumer goods, retailing, and groceries. Wal-Mart does not cheat suppliers, it keeps its word, it pays its bills briskly. "They are tough people but very honest; they treat you honestly," says Peter Campanella, who ran the business that sold Corning kitchenware products, both at Corning and then at World Kitchen. "It was a joke to do business with most of their competitors. A fiasco."

But Wal-Mart also clearly does not hesitate to use its power, magnifying the Darwinian forces already at work in modern global capitalism.

没有评论:

发表评论