1. Understanding of control reversal and dependency injection:
Control reversal:Generally, when an object class is used, the caller needs to create an instance of the object class. The inversion of control means that the caller no longer instantiates the object class, but declares the use of class objects and encapsulated objects. The specific instantiation is implemented by the container.
Dependency injection:Dependency injection is a key step in the Design Pattern of control reversal. When an object is instantiated to a container, the container generates a specific object class object and injects the object into the caller class, the caller can declare, define, and use the object class.
Purpose:To reduce the Coupling Degree of computer programs.
2. Story roles and responsibilities
Class Name |
Remarks (Responsibilities) |
Seed, pine, Bombax |
Seed interface, pine tree, kapok tree |
Forest |
Forest, caller, Controller |
Scientist |
Scientist and injector |
Person |
Human, client |
3. Background
The entity class is a seed, pine tree, Cotton Tree, and other tree classes. At first, the seed does not know what tree will grow. But this is the seed of a tree. When it grows up, it will have the function of the tree.
A forest is a controller that is controlled by seeds and trees. In the forest, the forest class declares the seed identity and encapsulates the seed function. However, it is still in the seed classification stage because it does not know the seed of any tree.
A scientist is a class that can change the name of a seed. Scientists control the fate of the seed in the forest and decide what kind of tree the seed grows. After deciding which tree to plant, scientists inject liquid into the forest and tell the forest.
Humans are the users of seeds and the recipients of executor and seed functions.
4. A story about dependency Injection
Once upon a time there was a seed of a tree, but it had no idea what it was, but at least it had the function of shading.
Seed interface:
1 package story; 2 3/** 4 * seed interface. Only the seed of a tree is known, and the tree is shaded. 5*6 * @ author qiaofengbo 7*8 */9 public interface seed {10 11 static final string name = "Tree"; 12 13 void function (); 14}
It is thinking that it may be a pine tree that will grow into a towering tree in the future. It may also be a kapok tree that can be shaded and the flowers can be used for medicine ......
Pine trees:
1 package story; 2 3 Public class pine implements seed {4/** 5 * pine tree class, with shading function 6 */7 public void function () {8 // todo auto-generated method stub 9 system. out. println ("I Am a pine tree"); 10} 11 12}
Kapo tree:
1 package story; 2 3 Public class Bombax implements seed {4/** 5 * decision tree class, with shading function 6 */7 public void function () {8 // todo auto-generated method stub 9 10 system. out. println ("I Am a kapok tree"); 11} 12 13}
As a tree, the forest is their home site. Just as the battlefield is the home of soldiers. So it came to the forest. The forest has the ability to manage all seeds and plant various trees in different partitions.
Forest type:
1 package story; 2 3/** 4 * Forest, where seeds are in the forest. Which tree seeds are less important, 5*6 * @ author qiaofengbo 7*8 */9 public class forest {10 11 private Seed; 12 13 public seed getseed () {14 return seed; 15} 16 17 Public void setseed (Seed) {18 this. seed = seed; 19} 20 21/** 22 * planting pine seed 23 */24 public void planting_pine () {25 seed. function (); 26} 27 28/** 29 * planting kapok tree seeds 30 */31 public void planting_bombax () {32 seed. function (); 33} 34}
Although various trees have been planted in different areas in the forest, we need to allocate seeds because we do not know the types of seeds.
In order to plant trees in the specified area without making mistakes, scientists injected a liquid into the seeds that determined the seed species, thus completely determining the seed species. Then, the injected seeds are handed over to the forest.
Scientists:
1 package story; 2 3/** 4 * scientists, capable of deciding what tree the seeds grow, inject things to the seed to change the seed 5*6 * @ author qiaofengbo 7*8 */9 public class scientist {10 11 public static forest injection_pine () {12 seed pine = new pine (); 13 forest = new forest (); 14 // inject 15 forest. setseed (PINE); 16 17 // forest. planting_pine (); 18 return forest; 19} 20 21 public static forest injection_bombax () {22 seed Bombax = new Bombax (); 23 forest = new forest (); 24 forest. setseed (Bombax); 25 // forest. planting_bombax (); 26 return forest; 27} 28}
Humans plant seeds from the forest's prescribed area. The trees planted are the desired trees.
Humans:
1 package story; 2 3/*** human, determine what scientists inject to seeds 5*6 * @ author qiaofengbo 7*8 */9 public class person {10 11 public static void main (string ARGs []) {12 planting (); 13} 14 15 public static void planting () {16 forest = scientist. injection_bombax (); 17 // start planting 18 forest. planting_pine (); 19} 20}
A story about dependency Injection