What is a delegate for Java?

Source: Internet
Author: User

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?

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.