Design Pattern in Symfony2-decorator pattern, symfony2 design pattern _ PHP Tutorial

Source: Internet
Author: User
In Symfony2, the design mode is the decorator mode and the symfony2 design mode. The design pattern in Symfony2-The Decorator pattern. The definition of the decorator pattern in the symfony2 design pattern does not need to change the original class file and use inheritance, dynamically expand the design mode in the Symfony2 of an object-the decorator mode and the symfony2 design mode

Decorator mode definition

Dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object.

In the modifier mode, each function to be decorated is placed in a separate class and the class is used to wrap the object to be decorated. Therefore, when special behavior is required, the client code can use the decoration function to wrap objects selectively and sequentially as needed during running.

Use cases

Imagine that if we want to create a student with a different dress on different occasions, for example, students in school need to wear school uniforms and students in dance need to wear formal attire, at home, students can wear bare clothes (a little abnormal). of course, they can also learn Superman to wear underpants out. At this time, the question is, should we write a different student class for each occasion? What if we want a pair of children's shoes that are wearing school uniform pants and formal clothes that are exposed? StudentWithSchoolUniform, StudentWithFormalWear, StudentWithNaked, example ...... endless class ~~~ Tired! Yes, if this leads to a class explosion, and the demand increases, the class will continue to increase, and the maintenance difficulty of the entire system can be imagined.

At this time, the decorator model can play its role. The Underpants, formal attire, school uniforms, shoes, and glasses are all specific decorers. students are specific objects to be decorated, the decorated object and the abstract class of the decoration owner both inherit the same parent class. Wearing different clothes for students is actually a process of dressing people (students) who are decorated with decoration packages.

Class and interface

  • Component (base class of the decorated object, corresponding to the Person class in the example)
  • ConcreteComponent (the specific object to be decorated, corresponding to the Student class in the example)
  • Decorator (Decorator base class, corresponding to Costume in the example)
  • ContreteDecorator (specific modifier class, corresponding to example Pants, Shirt, etc)

Example

Person. php

1 View Code

Student. php

1 Name = $ name; 13} 14 15 public function show () {16 echo 'student I am ', $ this-> name; 17} 18}View Code

Costume. php

1 View Code

Shirt. php

1 Person = $ person; 14 15} 16 17 public function show () {18 19 echo $ this-> person-> show (), ', wearing a shirt '; 20} 21 22}View Code

Pants. php

1 Person = $ person; 13 14} 15 16 public function show () {17 18 echo $ this-> person-> show (), ', wear pants '; 19} 20 21}View Code

Glas. php

1 Person = $ person; 13 14} 15 16 public function show () {17 18 echo $ this-> person-> show (), ', with glasses '; 19} 20 21}View Code

UnderWear. php

1 Person = $ person; 13 14} 15 16 public function show () {17 18 echo $ this-> person-> show (), ', wearing DK '; 19} 20 21}View Code

Client. php

1
  Show (); // I am a student JC14 echo'
'; 15 16 // use the UnderWear class to decorate Person17 $ underwear = new UnderWear ($ jc); 18 $ underwear-> show (); // I am a student JC, wearing DK19 echo'
'; 20 21 // use the Pants class to decorate Person22 $ pants = new Pants ($ underwear); 23 $ pants-> show (); // I am a student JC, wearing DK, pants 24 echo'
'; 25 26 // use the Shirt class to decorate Person27 $ shirt = new Shirt ($ pants); 28 $ shirt-> show (); // I am a student JC, wearing DK, trousers and shirts 29 echo'
'; 30 31 // use the Glasses class to decorate Person32 $ glasses = new Glasses ($ shirt); 33 $ glasses-> show (); // I am a student JC, wearing DK, trousers, shirts, glasses 34 echo'
';

Output result

Application of the Symfony2 EventDispatch component to the decorator mode

The Symfony2 EventDispatch component uses the decoration mode.

Configure EventDispatcher in Framework

  • Symfony \ Component \ EventDispatcher \ EventDispatcherInterface is the decorated interface
  • Symfony \ Component \ EventDispatcher and Symfony \ Component \ EventDispatcher \ ContainerAwareEventDispatcher are specific objects to be decorated.
  • Symfony \ Component \ EventDispatcher \ Debug \ TraceableEventDispatcherInterface contact
  • Symfony \ Component \ EventDispatcher \ Debug \ TraceableEventDispatcher base class
  • Symfony \ Component \ HttpKernel \ Debug \ TraceableEventDispatcher specific modifier object

The decorator object Symfony \ Component \ HttpKernel \ Debug \ TraceableEventDispatcher: dispatch () method. The core is to call the decorator's specific object Symfony \ Component \ EventDispatcher :: the dispatch () method works, but the modifier object Symfony \ Component \ HttpKernel \ Debug \ TraceableEventDispatcher: dispatch () method adds the corresponding functions, for example, preProcess (), preDispatch (), postDispatch (), and postProcess () are called before and after the Symfony \ Component \ EventDispatcher: dispatch () method ():

1/** 2 * {@ inheritdoc} 3 */4 public function dispatch ($ eventName, Event $ event = null) 5 {6 if (null ===$ event) {7 $ event = new Event (); 8} 9 10 // features added to the modifier object 11 $ this-> preProcess ($ eventName ); 12 $ this-> preDispatch ($ eventName, $ event); 13 14 $ e = $ this-> stopwatch-> start ($ eventName, 'core '); 15 16 // The core is still to call the decorated object Symfony \ Component \ EventDispatcher: dispatch () method 17 $ this-> dispatcher-> dispatch ($ eventName, $ event); 18 19 if ($ e-> isStarted () {20 $ e-> stop (); 21} 22 23 // added feature 24 $ this-> postDispatch ($ eventName, $ event); 25 $ this-> postProcess ($ eventName ); 26 27 return $ event; 28}

Advantages

Disadvantages

The definition of the annotator pattern dynamically extends an object without changing the original class file and using inheritance...

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.