The pattern of decoration design in Java and the difference between inheritance

Source: Internet
Author: User

The initial contact with the decorative design mode, has not understood the meaning of doing so, read a lot of information on the Internet, for the difference with the inheritance is not very clear analysis, until you see this blog: http://www.cnblogs.com/rookieFly-tdiitd/p/ 4914593.html This is a very clear explanation of the difference between the decorative design pattern and the inheritance. Here are some of my own understanding.

First, the concept

Decoration mode, also known as packaging mode, dynamically adds functionality to objects, and is an alternative to inheritance relationships.

Second, choose

Subclasses can also add functionality through inheritance, but consider the following example of how to choose between adornment mode and inheritance.

Suppose: Pancake is the parent class, the member variable is money, you can also add ham, chili, potato silk, egg on the pancake. If you use subclasses to inherit the way the parent class is, how many things might happen to eat such a pancake?

Here we see that if we use subclasses to inherit the parent class, then we have to write a lot of subclasses, which undoubtedly increases the program's bloated nature, not very flexible. At this time, the decoration mode was born.

Third, format

First of all we have a real object and decorative objects, they are two to achieve the same interface, there are specific decorative objects, the simple is the real object as a concrete decorative object of the construction method parameters, to decorate objects to add functionality.

The pattern of decoration design in Java and the difference between inheritance

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.