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