Home>Article>Backend Development> The difference between php strategy mode and adapter mode

The difference between php strategy mode and adapter mode

王林
王林 Original
2019-10-12 11:46:37 3853browse

The difference between php strategy mode and adapter mode

Strategy pattern

defines a series of algorithms and encapsulates each algorithm so that they can be replaced with each other. The strategy pattern allows the algorithm to change independently of the client using it; the key point of use is object-oriented and interface-oriented programming.

Implementation steps:

1. Define abstract strategy class

2. Implement specific strategy class

3. Define environment class

Adapter pattern

I want to use an existing class, but its interface does not meet the requirements because during the coding process It is necessary to follow the principle of being open to extensions and closed to modifications, so the original classes cannot be modified. In this case, you need to use the adapter mode to adapt the original classes to the form you need. There are two types of adapters: class adapters and object adapters.

Implementation steps:

1. Determine the target interface

2. Determine the adaptee

3. Create a class adapter

4. Create an object adapter.

The strategy mode is better than the adapter mode:

1. First of all, to a large extent Simplified our code.

2. It reduces the coupling of our program code, and low coupling is an important advantage of object-oriented.

Recommended tutorial:PHP video tutorial

The above is the detailed content of The difference between php strategy mode and adapter mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP error handling function Next article:PHP error handling function