5

让我们考虑一个这样的对象链:

Earth->Continent->Country->City->name

Earth.class让我们也考虑一下public static void main(String[] args)

当应用程序使用命令行选项执行时,例如,在不引入中间参数的情况下Barcelona将其传递给对象的最佳方法是什么?City

对象是在程序执行期间的不同阶段创建的。

我们应该将name变量设为静态还是使用 IoC,例如 Spring 或 Google Guice?还有其他选择吗?

欢迎任何想法。

4

3 回答 3

2
  • 我最喜欢 IOC 来完成这项任务。让 IOC 在构建 City 时传递依赖项。
  • 替代方案:使用静态服务注册表,可以查询其值。城市可以从服务登记处获得它的名字。
  • 备选方案:在您的层次结构上实现复合模式,包括一个函数,例如 find,它可以返回城市。然后你只需要查询和设置earth.find(BarcelonaID).setName(args[0]);

PicoContainer 中的 IoC 解决方案的示例如下:

PicoContainer container = new DefaultPicoContainer();
container.addComponent(Earth.class);
container.addComponent(Continent.class);
container.addComponent(Country.class);
container.addComponent(City.class, new ConstantParameter(cityName));

City barcelona = container.getComponent(City.class);
于 2012-05-05T16:40:16.977 回答
2

您可以自上而下构建数据结构——大陆构建城市,或自下而上main构建城市并将其传递给国家,或使用某种组合。DI 倾向于后者。

public static void main(String... argv) {
  // Bottom up.
  City city = new City(/* args relevant to city */);
  Country country = new Country(city, /* args relevant to country */);
  Continent continent = new Continent(country, /* args relevant to continent */);
  Planet planet = new Planet(continent, /* args relevant to planet */);
}

class City {
  City(/* few parameters */) { /* little work */ }
}
class Country {
  Country(/* few parameters */) { /* little work */ }
}
...
class Planet {
  Planet(/* few parameters */) { /* little work */ }
}

这可能比自上而下更干净:

public static void main(String... argv) {
  // Top down.
  Planet earth = new Planet(
    /* all the parameters needed by Earth and its dependencies. */);
}
class Planet {
  Planet(/* many parameters */) { /* lots of work */ }
}
...

DI 人士认为,自下而上的构造导致代码更易于维护和测试,但您不需要 DI 框架来使用它。

于 2012-05-05T16:41:23.900 回答
1

In my mind, this is the best use case you could imagine for a Visitor pattern. Basically, there should be one class Parameters that should hold all the parameters. Every object that needs one set of parameters can be visited with that Parameters class. The object can then pass the parameters to its children that know which parameters to use and how. In your case, this can be done this way:

public interface IParameterized{
   public void processParameters(Parameters param);
}

public class Earth implements IParameterized{
   public Earth(){
      // Create all countries here and store them in a list or hashmap 
   }
   public void processParameters(Parameters param){
      // find the country you want and pass the parameters to it
      country.processParameters(param);
   }
} 

public class Country implements IParameterized{
   public Country(){
      // Create all cities that belong to this country
   }
   public void processParameters(Parameters param){
      // find the city you want and pass the parameters to it
      city.processParameters(param);
   }
} 

public class City implements IParameterized{
   public City(){
      // Create city...
   }
   public void processParameters(Parameters param){
      // Do something with the parameter
   }
}

EDIT To wire up the dots, this can be used in the following way:

public static void main(String... argv) {
     Parameters params = new Parameters();
     // Populate params from the command line parameters
     Earth earth = new Earth();

     // Earth takes the responsibilty of processing the parameters
     // It will delegate the processing to the underlying objects in a chain
     earth.processParameters(params);
}

As a side note, you could also have a look at the Chain Of Responsibility design pattern

于 2012-05-05T16:50:22.577 回答