Design mode: (5) Decoration mode

Source: Internet
Author: User

Decoration Mode:

As the name implies, is the decoration, such as mobile phone sets, used to decorate the mobile phone, but, as the realization of mobile phone sets are not affected by mobile phones, mobile phone sets like an adorner. In the case of photo frames, frame lace, they are all designed to add new extras to the photo, but the feature itself does not affect the nature of the photo.

Behavioral patterns

Intention:

Dynamically add additional responsibilities to an object. In terms of added functionality, the adornment mode is more flexible than generating subclasses.

More flexible than generating subclasses, it is true that adorner mode is very flexible compared to adding responsibilities with inheritance.

For example, flow operations, beginning with file flow and network flow, if in the way of inheritance, I would like to write:

class stream{public:    virtual handlestream () =0;     virtual ~Stream () {}     };

File stream:

#include <iostream>
using namespace std; class Public stream{public: FileStream () {} virtual ~FileStream () {} void handlestream () {cout<<" process file stream "<<endl ; } };

Network flow:

#include <iostream>usingnamespace  std; class  Public stream{public:    Networdstream () {}    virtual ~Networdstream     void Handlestream () {  cout<<" network stream "<<endl  ; }};

If you want to encrypt two streams, we have to write this with inheritance:

 class  cryptofilestream: public  Span style= "color: #000000;" > filestream{ public  : Cryptofilestream ( ): FileStream () {}  virtual  ~    Cryptofilestream ();  void   Handlestream () {Filestream::han         Dlestream (); cout  <<  encrypt   " <<ENDL; }};
class Cryptofilestream: public networdstream{public:    Cryptofilestream (): Networdstream () {}    virtual ~cryptofilestream ();     void Handlestream ()    {         networdstream::handlestream ();         cout<<" encryption "<<Endl;    };

If there is a transcoding decoration class, then we have to write two such classes, I do not write here, not only the code redundancy, and the class expansion is serious. If there are n specific subclasses and M collocation, rough calculate should be 1+n+n*m+n* (2^m-m-1) class, base class one, and then how many seed class may be N, and the class is a single decoration is n*m kind, m type of decorator should be 2^m-m-1 ( The two-term formula minus the first two items), n the Seed class has n (2^m-m-1).

Of course, this is not the point, the focus is how we can use the decorative mode to achieve, first look at the book to the structure of the diagram:

We see four basic roles:

1. Abstract interface Object (component)

The most basic institutional object for dynamically adding responsibilities

2. Specific actual objects, objects with specific responsibilities (concrete component)

3. Total Adorner (decorator)

You need to save a component object for polymorphism, while inheriting component to add additional responsibilities.

4. Concrete Decoration

Implement the additional responsibilities you need to achieve.

The most basic base class:

class abstream{public:    virtualvoid handlebuff () =0;     virtual ~Abstream () {}};

File Stream class:

class FileStream: public abstream{public:    FileStream () {}    virtual ~ FileStream () {}     void Handlebuff ();}; void Filestream::handlebuff () {    " process file stream " << Endl;}

Network flow:

class Networdstream: public abstream{public:    networdstream ();     virtual ~networdstream ();     void Handlebuff ();}; void Networdstream::handlebuff () {    " Process Network flow " << Endl;}

Next is the adorner:

 class  streamdocorator:public  Span style= "color: #000000;" > abstream{ public  :  virtual  ~streamdocorator ();  void   Handlebuff ();  protected  : Streamdocorator (abstream  *stream): _stream ( Stream) {}  private  : Abstream  *_stream;};  void   Streamdocorator::handlebuff () {_stream ->  

streamdocorator:: ~streamdocorator ()
{

}

Then is the encryption, as the specific decoration class:

class CryptoStream: public streamdocorator{public:    *stream): Streamdocorator (stream) {}    virtual ~CryptoStream () {}    void  Handlebuff ()    {        streamdocorator::handlebuff ();         " encrypt the data stream " << Endl;    }};

If there is a transcoding:

class Buffstream: public streamdocorator{public:    *stream): Streamdocorator ( Stream) {}    virtual ~Buffstream () {}    void  handlebuff ()    {        Streamdocorator::handlebuff ();         " Binary transcoding " << Endl;    }};

Then we can test it:

int Main () {    new  FileStream ();     New CryptoStream (a);    CS-Handlebuff ();
cout Endl; New Buffstream (CS); // You can also make composite decorations Bcs->Handlebuff (); return 0 ;}

Results after the run:

Compare adornment mode and strategy mode:

The most fundamental is that the decoration mode is transparent to the object of the service, the object of this service is like the FileStream in the above example, transparency should be so understood, for FileStream, it is completely unaware of the kind of brotherhood, The presence of the adorner does not have any effect on FileStream, and in the policy mode, such as the economic class in the last example, the logic is actually implemented with the strategy pattern, but for FileStream, it needs to keep about the adorner (policy class, This is assumed to be a reference to the implementation of the policy pattern, then this is opaque.

And if the abstract stream is too complex, the strategy model is also a good choice, of course, the adorner emphasizes a variety of possible (refers to decoration and strategy) collocation, and the strategy seems to be unable to achieve this requirement.

Of course, the final is still to feel, perhaps in the future development process can understand more, after all, the ancients often said: "The paper on the end of the light, I know this matter to preach."

Design mode: (5) 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.