Home > Backend Development > PHP Tutorial > PHP object-oriented advanced design pattern: decorator pattern

PHP object-oriented advanced design pattern: decorator pattern

巴扎黑
Release: 2023-03-07 20:02:01
Original
1754 people have browsed it

What is the decorator pattern?

If you make changes to part or functionality of an existing object, but do not need to modify the structure of the original object, then using the decorator design pattern is most suitable.

Decorator pattern application problems and solutions:

When we first learn about object-oriented programming, the first obstacle is often understanding the parent-child relationship in inheritance. relation. Over time, we will become more familiar with this programming method. When faced with a new challenge, experienced object-oriented programmers immediately extend more functionality to an object. However, just as everything has a degree, only moderate use can ensure the good development of this kind of work.

The code base should have a limit on the number of class hierarchies. If objects begin to require too many subclasses, the corresponding code sacrifices programmer understanding and maintainability. Generally, I try to ensure that there are no more than 3 parent-child relationships for an object. I found that as long as more parent-child relationships are created, the code becomes confusing and uncontrollable. In addition, using ordinary paper cannot produce a UML diagram representation of any object in the application.

However, I don't want to prevent the use of class extensions. In fact, we often extend objects using appropriate solutions. However, for some problems, using classes based on the decorator design pattern is a better solution.

The decorator design pattern is suitable for situations where programmers spend a lot of time: changes are quick and small, and have little impact on the rest of the application. The goal of designing a class using the decorator design pattern is to apply incremental changes to a base object without having to override any existing functionality. Decorators are built in such a way that it should be possible to insert one or more decorators that change or "decorate" the target object directly into the main code flow without affecting other code flows.

UML

The UML diagram below details a class design using the decorator design pattern.

PHP object-oriented advanced design pattern: decorator pattern

The following description of the above picture:

1.MyObject is a base class with existing functionality. This class contains a public array named items and a public method named show ItemsFormatted().

2. The show ItemsFormatted() method is responsible for accepting the items array, formatting the array using predefined functionality and submitting the output.

3.The MyObjectDecorator class contains a private instance of MyObject and two public methods: MyObjectDecorator() and decorateItems().

4.The MyObjectDecorator() method represents the constructor, which accepts a MyObject type parameter and stores it internally.

5. The decorateItems() method can modify the items array of the MyObject instance.

Let’s look at the following example. In order to calculate the value of an area, we write the code as follows:

// 区域抽象类
abstract class Area
{
    abstract public function treasure();
}
//森林类,价值100
class Forest extends Area 
{
    public function treasure()
    {
        return 100;
    }
}
//沙漠类,价值10
class Desert extends Area
{
    function function treasure()
    {
        return 10;
    }
}
Copy after login

The above code seems to have no problem, but if you need to give an area What should I do to calculate the value of the destroyed forest? Should I add a DamageForest subclass? Obviously it is not feasible, because there are likely to be many other classes with overlapping types, which will lead to duplicate code in the class and more and more subclasses.

The decorator pattern uses composition and delegation instead of inheritance to solve the above problems. Let's look at the following improved code:

// 区域抽象类
abstract class Area
{    
   abstract public function treasure();
}

//森林类,价值100
class Forest extends Area 
{
   public function treasure()
   {
       return 100;
   }
}
//沙漠类,价值10
class Desert extends Area
{
   function function treasure()
   {
       return 10;
   }
}
//区域类的装饰器类
abstract class AreaDecorateor extends Area
{
   protected $_area = null;

   public function __construct(Area $area)
   {
       $this->_area = $area;
   }
}

//被破坏了后的区域,价值只有之前的一半
class Damaged extends AreaDecorateor
{
   public function treasure()
   {
       return $this->_area->treasure() * 0.5;
   }
}

//现在我们来获取被破坏的森林类的价值
$damageForest = new Damaged(new Forest());
echo $damageForest->treasure();  //返回50
Copy after login

The above is the detailed content of PHP object-oriented advanced design pattern: decorator pattern. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template