My design Tour [3]: Use template to change strategy pattern (OO)

Source: Internet
Author: User

In my design Journey: Strategy pattern (primary) (C + +) (OO C + +) (Template C + +), we used strategy pattern to let graph Er can draw triangle, circle and square

Because the need to change again,:D, we hope that Grapher will be able to print the text in each shape, the implementation of the results are as follows

Draw Hello Shape!! in Square
Draw Hello C++!! in Circle

In order to achieve this demand, we can change the IShape interface

class IShape {
public:
 virtual void draw(const char *text) const = 0;
};

But if the future needs to change, hope to draw is not text, but a picture, that interface must again change, in order to once and for all, we will pass the entire object to the Strategy,ishape interface into the following

class IShape {
public:
 virtual void draw(Grapher &grapher) const = 0;
};

Grapher::drawShpae()将*this传给各strategy

void drawShape() {
 this->shape->draw(*this);
}

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.