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) {
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.
source share