Design Pattern C ++ description ---- 10. Decorator pattern

Source: Internet
Author: User

I. Example

I used to create a file system called MyFileSys. Later, the customer wanted to add some additional functions, such as compression, encryption, and anti-virus. These additional operations were not sequential, for example, you can first compress and then encrypt, or you can first compress the virus.

These additional features are optional. Some customers require these features, some do not, and some require several additional features. How to design it?

Solution 1:
Directly modify this independent File System MyFileSys to implement different file systems for different customers.


Later, with the increasing number of customers, it was found that the workload for maintenance and modification was growing. Because every time a customer is added, a new class will be generated, and the additional features that the customer wants will be added. Even more depressing is that only one customer needs to be modified many times, the customer needs these additional functions today and wants to add other functions tomorrow. This will be changed and the maintenance workload will be huge.

Solution 2:

Later, we switched to the second solution to implement a separate additional function class to keep the original file system unchanged, so that some additional functions can be easily added on the client.



The Code is as follows:


[Cpp] // defines an object interface. You can dynamically add roles to these objects.
Class FileSys
{
Public:
Virtual ~ FileSys ()
{
}
 
Virtual void Operation ()
{
}
Protected:
FileSys ()
{
}
};
 
// Define a specific object
Class MyFileSys: public FileSys
{
Public:
MyFileSys ()
{
}
 
~ MyFileSys ()
{
}
 
Void Operation ()
{
Cout <"MyFileSys operation..." <endl;
}
};
 
// Decoration abstract class
Class Decorator: public FileSys
{
Public:
Decorator (FileSys * fileSys)
{
This-> _ fileSys = fileSys;
}
 
Virtual ~ Decorator ()
{
Delete _ fileSys;
}
 
Void Operation ()
{
}
Protected:
FileSys * _ fileSys;
};
 
// Compress the decoration class
Class ZipDecorator: public Decorator
{
Public:
ZipDecorator (FileSys * fileSys): Decorator (fileSys)
{
}
 
~ ZipDecorator ()
{
}

Void Operation ()
{
_ FileSys-> Operation (); // first run the previous function

This-> AddedZipBehavior (); // additional functions
}

Void AddedZipBehavior ()
{
Cout <"Added Zip Behavior..." <endl;
}
};
 
// Anti-virus Decoration
Class KillVirDecorator: public Decorator
{
Public:
KillVirDecorator (FileSys * fileSys): Decorator (fileSys)
{
}

~ KillVirDecorator ()
{
}

Void Operation ()
{
_ FileSys-> Operation ();

This-> AddedKillVirBehavior ();
}

Void AddedKillVirBehavior ()
{
Cout <"Added Kill Virus Behavior..." <endl;
}
};
 
// Encryption and Decoration
Class EncryptDecorator: public Decorator
{
Public:
EncryptDecorator (FileSys * fileSys): Decorator (fileSys)
{
}

~ EncryptDecorator ()
{
}

Void Operation ()
{
_ FileSys-> Operation ();

This-> AddedEncrypeBehavior ();
}

Void AddedEncrypeBehavior ()
{
Cout <"Added Encrypt Behavior..." <endl;
}
};
 
//////////////////////////////////////// //////////////////////////////////
// Test
Int main ()
{
FileSys * fileSys = new MyFileSys ();

Decorator * dec1 = new ZipDecorator (fileSys); // added the compression function to the original file system.
Decorator * dec2 = new KillVirDecorator (dec1); // Add the anti-virus function based on the previous steps.
Decorator * dec3 = new EncryptDecorator (dec2); // Add the encryption function
 
Dec3-> Operation ();
 
Return 0;
}
// Define an object interface to dynamically add responsibilities to these objects
Class FileSys
{
Public:
Virtual ~ FileSys ()
{
}

Virtual void Operation ()
{
}
Protected:
FileSys ()
{
}
};

// Define a specific object www.2cto.com
Class MyFileSys: public FileSys
{
Public:
MyFileSys ()
{
}

~ MyFileSys ()
{
}

Void Operation ()
{
Cout <"MyFileSys operation..." <endl;
}
};

// Decoration abstract class
Class Decorator: public FileSys
{
Public:
Decorator (FileSys * fileSys)
{
This-> _ fileSys = fileSys;
}

Virtual ~ Decorator ()
{
Delete _ fileSys;
}

Void Operation ()
{
}
Protected:
FileSys * _ fileSys;
};

// Compress the decoration class
Class ZipDecorator: public Decorator
{
Public:
ZipDecorator (FileSys * fileSys): Decorator (fileSys)
{
}

~ ZipDecorator ()
{
}

Void Operation ()
{
_ FileSys-> Operation (); // first run the previous function

This-> AddedZipBehavior (); // additional functions
}

Void AddedZipBehavior ()
{
Cout <"Added Zip Behavior..." <endl;
}
};

// Anti-virus Decoration
Class KillVirDecorator: public Decorator
{
Public:
KillVirDecorator (FileSys * fileSys): Decorator (fileSys)
{
}

~ KillVirDecorator ()
{
}

Void Operation ()
{
_ FileSys-> Operation ();

This-> AddedKillVirBehavior ();
}

Void AddedKillVirBehavior ()
{
Cout <"Added Kill Virus Behavior..." <endl;
}
};

// Encryption and Decoration
Class EncryptDecorator: public Decorator
{
Public:
EncryptDecorator (FileSys * fileSys): Decorator (fileSys)
{
}

~ EncryptDecorator ()
{
}

Void Operation ()
{
_ FileSys-> Operation ();

This-> AddedEncrypeBehavior ();
}

Void AddedEncrypeBehavior ()
{
Cout <"Added Encrypt Behavior..." <endl;
}
};

//////////////////////////////////////// //////////////////////////////////
// Test
Int main ()
{
FileSys * fileSys = new MyFileSys ();

Decorator * dec1 = new ZipDecorator (fileSys); // added the compression function to the original file system.
Decorator * dec2 = new KillVirDecorator (dec1); // Add the anti-virus function based on the previous steps.
Decorator * dec3 = new EncryptDecorator (dec2); // Add the encryption function

Dec3-> Operation ();

Return 0;
} After that, it is very convenient to add additional functions. This mode is the decoration mode.

Ii. decoration Mode

Decoration mode: dynamically add some additional responsibilities to an object. As for the added function, the decoration mode is more flexible than the subclass generation.

 


Note:


Componet is mainly used to define an interface through which roles can be added to these objects (ConcreteComponent.


Dectorator, a decorative class, extends the functionality of the Component class through ConcreteDecorator. For Component, you do not need to know the existence of this abstract class.

ConcreteDecorator, specific decoration class, add specific additional features.

Advantages:

1. The decoration class dynamically adds more functions to existing functions.

2. effectively separate the core responsibilities of the class from the decorative functional area, and remove repeated decorative logic in the relevant class.


Iii. Question Discussion

As you can see, Decorator inherits from Component, and it becomes a brother to ConcreteComponent. However, the role of Decorator is to modify ConcreteComponent, which seems strange !! What cannot be said most is that the relationship between Decorator and Component is not-!!


I personally think:

1. This inheritance relationship should not be focused on. Here, inheritance is mainly used to reuse the Operation () interface for the purpose of modification.

2. The key is the combination of Decorator and Component. The decoration class has a Component pointer, which can be modified to a specific Component object only because of its existence.

 


Author lwbeyond

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.