Php design pattern-decorator pattern-php design pattern Decoration

Source: Internet
Author: User
Tags drupal

Php design pattern-decorator pattern-php design pattern Decoration
1. Introduction

1. the Decorator mode allows you to dynamically add and modify class functions.
2. A class provides a function. to modify and add additional functions, in the traditional programming mode, you need to write a subclass to inherit it and implement the class method again.
3. You only need to add a modifier object at run time to use the decorator mode to achieve maximum flexibility.

2. Instance

Next, let's take an example. Use php to implement the function of a small canvas (draw a specified color image)

1. Traditional method before decorator is not used 1. Implement a canvas class
<? Php class Canvas {// save an array of the dot matrix public $ data; // initialize the Dot Matrix function init ($ width = 20, $ height = 10) {$ data = array (); for ($ I = 0; $ I <$ height; $ I ++) {for ($ j = 0; $ j <$ width; $ j ++) {$ data [$ I] [$ j] = '*'; }}$ this-> data = $ data ;} // initialize a square lattice function rect ($ a1, $ a2, $ b1, $ b2) {foreach ($ this-> data as $ k1 => $ line) {if ($ k1 <$ a1 or $ k1> $ a2) continue; foreach ($ line as $ k2 => $ char) {if ($ k2 <$ b1 or $ k2> $ b2) continue; $ this-> data [$ k1] [$ k2] = '& nbsp; ';}}// start drawing function draw () {foreach ($ this-> data as $ line) {foreach ($ line as $ char) {echo $ char;} echo "<br/> \ n ";}}}
2. Call
$canvas = new Canvas();$canvas->init(40, 20);$canvas->rect(4,15,9,30);$canvas->draw();
3. The result is shown as the next square.
*****************************************************************************************************************************************************************************************************************                      ******************                      ******************                      ******************                      ******************                      ******************                      ******************                      ******************                      ******************                      ******************                      *****************************************************************************************************************************************************************************************************************
4. The following question comes:
5. Solve the above problem (output the html code During display, and modify the draw method to see the result you want)
// Start to execute the drawing function draw () {echo "<div style = 'color: red; '>"; foreach ($ this-> data as $ line) {foreach ($ line as $ char) {echo $ char;} echo "<br/> \ n";} echo "</div> ";}
6. The problem is coming again.

1. If I don't want to add any color that day, I want to bold or tilt it. I need to modify the code, or I want to add a title on it... Wait for a moment. Next we will use the decorator mode to modify the above Code to decouple the above Code!

2. Use the decorator mode to implement the above functions 1. Implement the base class of a decorator
// Interface DrawDecorator {// operation function beforeDraw () before painting; // operation function afterDraw () after painting ();}
2. Implement a color Ornament
Class ColorDrawDecorator implements DrawDecorator {// color attribute protected $ color; // initialize the color function _ construct ($ color = 'red') {$ this-> color = $ color ;} // function beforeDraw () {echo "<div style = 'color: {$ this-> color}; '> ";} // operation function afterDraw () {echo "</div>" ;}} after painting ";}}
3. New canvas class
Class Canvas {// save an array of the dot matrix public $ data; // Save the modifier object protected $ decorators = array (); // initialize the Dot Matrix function init ($ width = 20, $ height = 10) {$ data = array (); for ($ I = 0; $ I <$ height; $ I ++) {for ($ j = 0; $ j <$ width; $ j ++) {$ data [$ I] [$ j] = '*' ;}$ this-> data = $ data ;} // register the decorator object function addDecorator (DrawDecorator $ decorator) {$ this-> decorators [] = $ decorator;} // draw the previous operation function beforeDraw () {foreach ($ this-> decorators as $ decorator) {$ decorator-> beforeDraw () ;}// operation function afterDraw () after painting () {$ decorators = array_reverse ($ this-> decorators); foreach ($ decorators as $ decorator) {$ decorator-> afterDraw () ;}}// start drawing function draw () {$ this-> beforeDraw (); foreach ($ this-> data as $ line) {foreach ($ line as $ char) {echo $ char ;} echo "<br/> \ n" ;}$ this-> afterDraw () ;}// rect ($ a1, $ a2, $ b1, $ b2) {foreach ($ this-> data as $ k1 = >$ line) {if ($ k1 <$ a1 or $ k1> $ a2) continue; foreach ($ line as $ k2 =>$ char) {if ($ k2 <$ b1 or $ k2> $ b2) continue; $ this-> data [$ k1] [$ k2] = '& nbsp ;';}}}}
4. Call
$ Canvas = new Canvas (); // inject the decorator object $ canvas-> addDecorator (new ColorDrawDecorator ('green'); $ canvas-> init (40, 20 ); $ canvas-> rect (4, 15, 9, 30); $ canvas-> draw ();
5. Results

Output a green rectangle

Similarly, if you want to use bold, skewed, and custom titles, you can create a specific decorator and inject it into the canvas.

6. Summary

1. the decorator adds custom operations before performing specific operations.
2. The implementation of the decorator is like the hook mechanism, such as the hook mechanism in drupal.
3. Usecall_user_funcOrcall_user_func_arrayYou can also implement the decorator mechanism. This can be achieved by referring to the drupal hook implementation, which is quite good! I will not introduce it here. It is not in the design mode. I will write it later.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.