Detailed explanation of decoration mode

藏色散人
Release: 2023-04-05 20:54:01
forward
2846 people have browsed it

Decoration mode

Dynamically extends the functionality of an object without changing the original class file and using inheritance. It wraps the real object by creating a wrapping object, that is, decoration.

Features of Decoration Mode

(1) Decoration objects have the same interface as real objects. This way the client object can interact with the decorated object in the same way as a real object.

(2) The decorated object contains a reference to the real object

(3) The decorated object accepts all requests from the client. It forwards these requests to the real objects.

(4) Decoration objects can add some additional functions before or after forwarding these requests. This ensures that additional functionality can be added externally at runtime without modifying the structure of a given object. In object-oriented design, functional extension of a given class is usually implemented through inheritance.

Advantages

1. The purpose of Decorator pattern and inheritance relationship is to extend the functionality of objects, but Decorator can provide more flexibility than inheritance.

2. By using different specific decoration classes and the permutations and combinations of these decoration classes, designers can create many combinations of different behaviors.

Disadvantages

1. This feature, which is more flexible than inheritance, also means more complexity.

2. Decoration mode will lead to many small classes in the design. If overused, the program will become very complicated.

3. Decoration mode is for abstract component (Component) type programming. However, if you are programming for specific components, you should rethink your application architecture and whether decorators are appropriate. Of course, you can also change the Component interface, add new public behaviors, and implement the "translucent" decorator mode. Make the best choice in actual projects.

php code example

Abstract A worker class has a working method, and two subclasses (plumber, carpenter) implement the worker interface:

interface worker{
public function doSomeWork();
}
//水管工
class shuiguan implements worker{
public function doSomeWork(){
echo '修水管';
}
}
//木工
class mu implements worker{
public function doSomeWork(){
echo '修门窗';
}
}
Copy after login

Now there is a new requirement. Workers of Company A (including water pipes and carpenters) are required to say "Hello!" when entering the door. We want to implement this function uniformly without affecting the basic classes. We can use the decoration mode class to implement:

//a公司工人
class aWorker implements worker{
//具体的工人
public $worker;
//构造函数获取工人
public function __construct($worker){
$this->worker  = $worker;
}
public function doSomeWork(){
echo '您好!';
$this->worker->doSomeWork();
}
}
$aWorker = new aWorker(new shuiguan());
$aWorker->doSomeWork();
Copy after login

aWorker also implements the interface of the worker class. It requires a specific worker object. After completing the special requirements (saying hello), use the method of the original worker object. . This is decoration mode!

The above is the detailed content of Detailed explanation of decoration mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:hcoder.net
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!