Best way to pass a Java link along a chain of objects

Consider a chain of such objects:

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

Let Earth.class also have public static void main(String[] args)

When an application runs with a command line switch, for example. Barcelona , what would be the best way to pass it to the City object without introducing intermediate parameters?

Objects are created at different stages during program execution.

Should we make a static name variable or use IoC like Spring or Google Guice? Are there any other options?

Any ideas are welcome.

+4
source share
3 answers
  • I like IOC for this task. Let the IOC transmit addiction when it builds a city.
  • Alternative: Use a static registry of services that you can query for a value. A city can get its name from the registry of services.
  • Alternative. Deploy a composite template in your hierarchy, including a function, such as find, that can return a city. Then you just need to query and set earth.find(BarcelonaID).setName(args[0]);

An example of what the IoC solution in PicoContainer will look like:

 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); 
+2
source

You can create data structures from top to bottom - the continent constructs a city or from bottom to top - main builds a city and transfers it to the Country or uses some combination. DI supports the latter.

 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 */ } } 

which can be much cleaner than from top to bottom:

 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 people argue that building from the bottom up leads to a much more convenient and verifiable code, but it does not require a DI structure to use it.

+2
source

In my opinion, this is the best use case you could imagine for Visitor . Basically, there should be one Parameters class, which should contain all the parameters. Each object that needs one set of parameters can be visited using this Parameters class. An object can then pass parameters to its children, which know which parameters to use and how. In your case, this can be done as follows:

 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 connect points, this can be used as follows:

 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 an additional note, you can also take a look at the Chain Of Responsibility design template.

+1
source

Source: https://habr.com/ru/post/1410883/


All Articles