In the study of design patterns, found in the book has repeatedly referred to the commission of the word, so after the online search to get results, and wrote a small example.
What is a delegate?
The delegation mode is a basic technique in software design pattern. In delegate mode, there are two objects involved in processing the same request, and the object that accepts the request delegates the request to another object for processing. The delegate mode is a basic technique, and many other patterns, such as state mode, policy mode, and visitor pattern, are in essence a delegate mode in more special situations. The delegate mode allows us to use aggregations instead of inheritance, which also allows us to simulate mixin. (Excerpt from rookie tutorial, hhh~)
Why should I use a delegate?
Meet the open and closed-principle, there are many combinations, less inheritance.
The pros and cons of a delegate?
Advantages : Meet design principles
cons : Will write more code
As an example,
A delegate is used in the adornment mode.
Like what:
The GetDescription method of the Mocha class here is to delegate the beverage object and get a description.
What is a delegate for Java?