C # design pattern of decorative design pattern (decorator)

Source: Internet
Author: User
Tags abstract wrapper

First, decoration (decorator) mode

The decoration (decorator) mode is also named wrapper (wrapper) mode [GOF95]. Decorative mode extends the functionality of objects transparently to the client, and is an alternative to inheritance relationships.

Introduction

The Monkey King has 72 changes, and each of his changes brings him an added skill. When he became a fish, he could swim in the water, and when he became a sparrow, he could fly in the sky. And not Takui how to change, in the eyes of the two lang God, he is always the only varlet.

Decorative mode dynamically attaches more responsibility to an object in a manner that is transparent to the customer. In other words, the client does not feel that the object is different before and after the decoration. The decorative pattern can extend the functionality of an object without using the creation of more subclasses.

Second, the structure of decorative mode

The decoration mode uses an instance of a subclass of the previously decorated class to delegate the client's invocation to the decorated class. The key to decorating the pattern is that the extension is completely transparent.

In the case of sun monkeys, the old sun into the fish is equivalent to the children of the sun, the fish and the interaction between the outside through the "delegation" to the old grandson of the Buddha, by the old grandson of the Act.

The class diagram of the decorative pattern is shown in the following illustration:

The various roles in the decoration mode are:

Abstract widget (Component) role: gives an abstract interface to the specification to prepare the object to receive additional responsibility.

Specific widget (concrete Component) Role: Defines a class that will receive additional responsibility.

Decorative (decorator) Role: Holds an instance of a widget (Component) object and defines an interface that is consistent with the abstract component interface.

Specific decoration (concrete decorator) role: Responsible for the Component object "affixed" additional responsibilities.

Related Article

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.