Design Mode 3-decoration Mode

Source: Internet
Author: User

The design model has not been updated recently. On the one hand, it is because I moved my house for several days on the national day and didn't have time to read books. On the other hand, I don't know much about the relationship between instances. Yesterday, I suddenly became interested in the virtual function table. I studied it and then looked at the decoration mode to understand how this mode runs.

 

Use Cases:

1. dynamically expand the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package real objects and dynamically adds some additional responsibilities to an object.

2. The class design is open to extensions and closed to modifications.

 

UML diagram

 

Let me talk about my understanding of this UML diagram: componet is a general abstract class, the concreatecomponent on the left is our specific object to be decorated, and the decorator on the right is a specific ornament. For example, I have two mobile phones, mx3 and iphone5s. I have decorated these two phones (with film and ornaments), so component refers to phone, concreatecomponent refers to mx3 and ipone5s, so the Decoration refers to decorator, and the film and Decoration refers to concreatedecorator.

 

My decoration pattern code

  1 #include <iostream>  2 #include <string>  3 using namespace std;  4   5 class Person {  6 public:  7     Person() {}  8     Person(string n):name(n) {}  9     virtual ~Person() {} 10     virtual void Show(){ cout << name << endl;} 11 private: 12     string name; 13 }; 14  15 class Finery : public Person { 16 public: 17     Finery(Person *p): p_person(p) {} 18     virtual ~Finery() {} 19     virtual void Show() { 20         p_person->Show(); 21     } 22 private: 23     Person *p_person; 24 }; 25  26 class Shirt : public Finery { 27 public: 28     Shirt(Person* p) : Finery(p) {} 29     virtual ~Shirt() {} 30     virtual void Show() { 31         cout << "Shirt " << endl; 32         Finery::Show(); 33     } 34 }; 35  36  37 class Tie : public Finery { 38 public: 39     Tie(Person* p) : Finery(p) {} 40     virtual ~Tie() {} 41     virtual void Show() { 42         cout << "Tie " << endl; 43         Finery::Show(); 44     } 45 }; 46  47 int main() 48 { 49     Person *p_person = new Person("Jack"); 50     Shirt *p_shirt = new Shirt(p_person); 51     Tie *p_tie = new Tie(p_shirt); 52     p_tie->Show(); 53     delete p_tie; 54     delete p_shirt; 55     delete p_person; 56 }

 

Let's talk about how the code runs,

Line 3 calls the show () function of p_tie and prints the "Tie". Then, it finds that the show () function of the parent class (finery) is called. The code is returned to line 20, it is found that the p_person instance is a shirt, and the show () function of the real object is called through the virtual function, and the "shirt" is printed; then the parent class (finery) is called) the show () function of returns to the 20 lines of code and finds that the instance of p_person is Jack. The show () function of the real object person called through the virtual function prints "Jack ". This is the use of class virtual functions. If you do not understand it, you can also study the C ++ virtual function table I have written.

Design Mode 3-decoration Mode

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.