PHP Object-oriented Advanced design mode: Adorner mode

Source: Internet
Author: User
What is adorner mode?

If you change the part or functionality of an existing object, but you do not need to modify the structure of the original object, the adorner design pattern works best.

Adorner mode application problems and solutions:

When we started learning about object-oriented programming, the first hurdle was often to understand the parent-child relationship in succession. Over time, we are more familiar with this programming approach. When faced with new challenges, experienced object-oriented programmers immediately extend more functionality to an object. However, just as all things have degrees, only modest use can ensure that this kind of work is well carried out.

The code base should have a limited number of class hierarchies. If an object begins to require too many subclasses to be enabled, then the code will sacrifice the programmer's understanding and maintainability. In general, I'm trying to ensure that there are no more than 3 parent-child relationships for an object. I found that as long as you create more parent-child relationships, the code becomes confusing and uncontrollable. In addition, the use of general paper is not able to promise any object in the application of UML diagram representation.

However, I do not want to prevent the use of class extensions. In fact, we often use the appropriate solution to extend the object. However, for some problems, using a class based on the adorner design pattern is a better solution.

Decorator Design Patterns apply to the following workplaces where programmers spend a lot of time: The change is fast and small, and hardly affects the rest of the application. The goal of designing a class with the adorner design pattern is to apply incremental changes to a base object without having to rewrite any existing functionality. Adorners are built in such a way that you should be able to insert one or more adorners directly into the main code flow, or "decorate" the target object without affecting other code flows.

Uml

The following UML diagram details A class design that uses the adorner design pattern.

Below the description of the face:

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

The 2.show itemsformatted () method is responsible for accepting the items array and submitting the output after using the predefined functionality to format the array.

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

The 4.MyObjectDecorator () method represents a constructor that takes a MyObject type parameter and stores it internally.

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

Let's take a look at the following example, in order to calculate the value of an area, we'll write the code like this:

Zone abstract class area{abstract public    function Treasure ();} Forest class, value 100class Forest extends area {public    function treasure ()    {        return;    }} Desert class, Value 10class Desert extends area{    function function Treasure ()    {        return;    }}

The code above looks like there is no problem, but what if you need to calculate the value of a damaged forest, add damageforest subclasses? Obviously not, because there are probably many other types of overlapping classes, which can cause duplicate code in the class, and the subclass will become more and more.

Adorner mode uses combinations and delegates instead of using inheritance to solve the above problem, we are looking at the following modified code:

//Area abstract class abstraction classes area{abstract public Function Treasure ();}   Forest class, value 100class Forest extends Area {public Function treasure () {return 100;   }}//Desert class, value 10class Desert extends area{function function treasure () {return 10;   The adorner class of the}}//zone class is abstract classes Areadecorateor extends area{protected $_area = null;   Public function __construct (area $area) {$this->_area = $area; }}//was destroyed after the area, only half the value of the previous class damaged extends areadecorateor{public Function treasure () {return $this->_area-   >treasure () * 0.5;  }}//now we are going to get the value of the destroyed forest class $damageforest = new damaged (new Forest ()); Echo $damageForest->treasure (); Return to 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.