First, the creation of related objects of classes can indeed be done by new. However, when the project is too large and the object has too many dependencies, we need to write a lot of new code when building the object, and there may be a method of directly calling the object after forgetting to instantiate some dependent objects, then there will be a null pointer exception. If this process is left to container management, we don't have to worry about object instantiation, and there will be no null pointer exceptions when using dependent objects at any time, and the code can be simplified. Second, the attributes used in the method call process may be instance attributes, if they are all defined as static, what about the data used in the method? If you define all methods as static, then the properties of the class referenced in the method must also be defined as static, which is equivalent to global attributes, so there will definitely be data synchronization problems. In fact, it makes sense to think about the programming principles given in the design pattern, all aimed at building highly readable, easy-to-maintain and scalable code. Best wishes!
It is convenient to assemble software in the way of building blocks, and it is convenient to create software with high cohesion and low coupling (extracting dependencies from the code) Collaborative development, debugging code (no need to integrate components together, they can be tested separately), and the code created is more robust.
Project development pays attention to high cohesion and low coupling, and the use of dependency injection can avoid using the new keyword area to create objects, thus reducing the coupling between classes
|