


Let's talk about dependency injection, containers and appearance patterns in framework development (Part 1)
This article mainly introduces the dependency injection, container and appearance mode (upper part) about chatting framework development. It has a certain reference value. Now I share it with you. Friends in need can refer to it
1. Dependency injection and decoupling
Men and women in love often say, I can’t live without you. What a deep dependence this is~~
Programming The dependence in work is essentially the same as the dependence in our lives: my work cannot be done without your support. Without you, there would be no me.
There are two types of dependencies: one is functional, and the other is sequential. Let’s use examples to illustrate:
We now have such a task:
User login operations
1. Involves database operations, data verification, and template output;
2. Corresponds to Db class, Validate class, and View class respectively;
3.Only Demonstration and specific examples are required for students to complete by themselves;
Before formal coding, let’s briefly understand what a client is?
1. Client: As long as it can initiate a request, it can be regarded as a client, a browser, or a piece of code.
2. The following code instantiates the User class and calls Its internal logon method works
3. Therefore, $user = new User(); is the client code
4. Or, it can also be understood this way, anything written in a class or function Other than the code, it can be regarded as the client
The source code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
Although the above code can work normally, there are still the following problems:
1. Above Of the four classes, only User is the actual work class, and the other three are tool classes (Db, Validate, View)
2. Once the tool classes called in the work class change, these tools must be modified All reference codes of the class, such as Db parameter changes
3. The caller of the work class must be very familiar with all tool classes to be used, and must understand the parameters and return values
4 . The work class has formed a serious dependence on the above three tool classes, which is also called severe coupling between classes.
Now we use the most commonly used dependency injection (DI) to decouple the coupling
We First understand the basic ideas of decoupling:
1. Dependency injection is not divine
2. In essence, the instantiation of the tool class is not completed in the work class, but Outside the work class, that is, it is completed on the client
3. Since the instantiation of the tool class is completed on the client, it is in the work class, and there must be a receiver to save the instantiated tool object
4. At this time, the user can directly pass the tool object that has been instantiated on the client to the method of the work class in the form of parameters
5. This kind of object is directly passed in from the outside The way to reach the current working class is called dependency injection
The source code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
Although the instantiation of dependent classes is moved forward to the client, the dependency problem between classes is solved
However, there are still several problems:
1. In order to make the working class User a normal tool, all required classes must be instantiated externally in advance;
2 .As long as instantiation is involved, the client (caller) must be very familiar with the details of these dependent classes, such as parameters and return values
Can the user be allowed to omit the steps of instantiating the dependent classes? Isn't this better and simpler?
When we call an external dependent class, we only need to give a class name and a method (constructor) to create an instance of the class?
That is: we only give: the class name, the method of creating a class instance, and nothing else.
We will use the "container technology" to implement this fool-like decoupling process
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
The above is the detailed content of Let's talk about dependency injection, containers and appearance patterns in framework development (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This article will take you through dependency injection, introduce the problems that dependency injection solves and its native writing method, and talk about Angular's dependency injection framework. I hope it will be helpful to you!

Introduction to the method of using dependency injection (DependencyInjection) in the Phalcon framework: In modern software development, dependency injection (DependencyInjection) is a common design pattern aimed at improving the maintainability and testability of the code. As a fast and low-cost PHP framework, the Phalcon framework also supports the use of dependency injection to manage and organize application dependencies. This article will introduce you how to use the Phalcon framework

For testing dependency injection using JUnit, the summary is as follows: Use mock objects to create dependencies: @Mock annotation can create mock objects of dependencies. Set test data: The @Before method runs before each test method and is used to set test data. Configure mock behavior: The Mockito.when() method configures the expected behavior of the mock object. Verify results: assertEquals() asserts to check whether the actual results match the expected values. Practical application: You can use a dependency injection framework (such as Spring Framework) to inject dependencies, and verify the correctness of the injection and the normal operation of the code through JUnit unit testing.

In Go, the dependency injection (DI) mode is implemented through function parameter passing, including value passing and pointer passing. In the DI pattern, dependencies are usually passed as pointers to improve decoupling, reduce lock contention, and support testability. By using pointers, the function is decoupled from the concrete implementation because it only depends on the interface type. Pointer passing also reduces the overhead of passing large objects, thereby reducing lock contention. Additionally, DI pattern makes it easy to write unit tests for functions using DI pattern since dependencies can be easily mocked.

Answer: In Go language, dependency injection can be implemented through interfaces and structures. Define an interface that describes the behavior of dependencies. Create a structure that implements this interface. Inject dependencies through interfaces as parameters in functions. Allows easy replacement of dependencies in testing or different scenarios.

The core value of using dependency injection (DI) in PHP lies in the implementation of a loosely coupled system architecture. DI reduces direct dependencies between classes by providing dependencies externally, improving code testability and flexibility. When using DI, you can inject dependencies through constructors, set-point methods, or interfaces, and manage object lifecycles and dependencies in combination with IoC containers.

Using dependency injection (DI) in Golang unit testing can isolate the code to be tested, simplifying test setup and maintenance. Popular DI libraries include wire and go-inject, which can generate dependency stubs or mocks for testing. The steps of DI testing include setting dependencies, setting up test cases and asserting results. An example of using DI to test an HTTP request handling function shows how easy it is to isolate and test code without actual dependencies or communication.

Answer: Dependency injection and service containers in PHP help to flexibly manage dependencies and improve code testability. Dependency injection: Pass dependencies through the container to avoid direct creation within the function, improving flexibility. Service container: stores dependency instances for easy access in the program, further enhancing loose coupling. Practical case: The sample application demonstrates the practical application of dependency injection and service containers, injecting dependencies into the controller, reflecting the advantages of loose coupling.
