Conflict between inheritance and aggregation in decorator mode

Source: Internet
Author: User

One: Background: Decorator

*decorator is often translated into "decoration", I think the translation into a "painter" more image point, painter (decorator) is used to brush paint, then the object of painting paint we call decoratee. Both entities are required in the decorator model.

*decorator definition:

Dynamically add some extra responsibility to an object, like paint on a wall. Using decorator mode to achieve functional expansion is more flexible than generating subclasses.

* Why use Decorator?

We can usually use inheritance to extend the functionality, if these need to expand the variety of functions, it is bound to generate a lot of subclasses, increase the complexity of the system, at the same time, the use of inheritance to achieve functional expansion, we must be able to foresee these expansion functions, these functions are compiled when it is determined, is static.

The rationale for using decorator is that these features require the user to dynamically decide how and when to join. Decorator provides a "Plug and Play" method that determines when to add functionality during run time.

* For the model, it is initially summed up as

1. Basic ability for Interface

The 2.Decorator parameter is the interface itself and the interface for the next decorator parameter

3. The basic function class implements the interface and acts as a parameter to the decorator constructor to add new functionality on this basis

4. Additional functionality is handled by the data structure in decorator

Two: Questions

This is an example of a decorator design pattern implemented as follows:

Basic function: Counter class

Features that need to be added

1: Upper Control

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.