Home >Backend Development >PHP Tutorial >Learn php's adapter pattern
The Learn phps adapter pattern mode of PHP design pattern, that is, the Adapter (alias Wrapper) mode: converts the Learn phps Learn phps adapter pattern pattern of one class into the Learn phps Learn phps adapter pattern pattern of another class that the customer expects. Adapters allow classes with originally incompatible Learn phps Learn phps adapter pattern patterns to work together seamlessly.
The Learn phps adapter pattern mode of PHP design pattern, that is, the Adapter (alias Wrapper) mode: converts the Learn phps Learn phps adapter pattern pattern of one class into the Learn phps Learn phps adapter pattern pattern of another class that the customer expects. Adapters allow classes with originally incompatible Learn phps Learn phps adapter pattern patterns to work together seamlessly. Key points of this design pattern: 1) The Learn phps adapter pattern pattern is mainly used when "you want to reuse some existing classes, but the Learn phps Learn phps adapter pattern pattern is inconsistent with the requirements of the reuse environment." It is very useful in legacy code reuse, class library migration, etc. 2) The Learn phps adapter pattern pattern has two implementation structures: object Learn phps adapter pattern and class Learn phps adapter pattern. However, the class Learn phps adapter pattern adopts the "multiple inheritance" implementation method, which brings undesirable high coupling, so it is generally not recommended. The object Learn phps adapter pattern adopts the "object combination" method, which is more in line with the spirit of loose coupling. Let’s take a look at its structure through the following two pictures: Adapter pattern structure diagram of classes (inheritance) Adapter pattern structure diagram of objects (composition) (code implementation of object Learn phps adapter pattern) Target: Define the Learn phps Learn phps adapter pattern patterns used by the Client related to specific fields public Learn phps Learn phps adapter pattern pattern Target { void request();} Adaptee: The existing Learn phps Learn phps adapter pattern pattern that needs to be adapted now public class Adaptee{ public void specificRequest(){}} Adapter: Adapt the Adaptee Learn phps Learn phps adapter pattern pattern and the Target Learn phps Learn phps adapter pattern pattern public class Adapter implements Target{ public Adapter(Adaptee adaptee) { super(); this.adaptee = adaptee; } public void request() { adaptee.specificRequest(); } private Adaptee adaptee;} Applicability: 1) The system needs to use existing classes, and the Learn phps Learn phps adapter pattern patterns of this type do not meet the needs of the system. 2) I want to create a reusable class that can be used to work with some classes that are not closely related to each other, including some classes that may be introduced in the future. These source classes do not necessarily have complex Learn phps Learn phps adapter pattern patterns. 3) (For object Learn phps adapter patterns) In the design, it is necessary to change the Learn phps Learn phps adapter pattern patterns of multiple existing subclasses. If the class Learn phps adapter pattern mode is used, an Learn phps adapter pattern must be made for each subclass, which is not practical. Let’s look at the advantages and disadvantages of this design pattern: Class Adapter: 1) Use a specific Adapter class to match Adaptee and Target. The result is that when we want to match a class and all its subclasses, the class Adapter will not do the job. 2) Allow Adapter to override (redefine) some behaviors of Adaptee, because Adapter is a subclass of Adaptee. Object Adapter: 1). Allow an Adapter to work with multiple Adaptee, that is, the Adaptee itself and all its subclasses (if there are subclasses) at the same time. Adapter can also add functions to all Adaptees at once. 2), making it difficult to override (redefine) the behavior of Adaptee. If you must override the Adaptee method, you have to first make a subclass of Adaptee to override the Adaptee method, and then use this subclass as the real Adaptee source for adaptation. |