Java 8 Combat (note) Chapter II

Source: Internet
Author: User

Behavioral parameterization, a software development model that handles frequently changing requirements. Principle: After writing similar code, try to abstract it.

Method 1: Add parameters to the method.

Method 2: Define a family of algorithms, encapsulate them (called "policies"), and then select an algorithm at run time.

Behavioral parameterization: Allow methods to accept multiple behaviors (or strategies) as parameters and use them internally to accomplish different behaviors.

Strategy Patternisa behavioral software design pattern This enables selecting an algorithm at runtime. The Strategy pattern

    • Defines a family of algorithms,
    • Encapsulates each algorithm, and
    • Makes the algorithms interchangeable within that family.

Filter by abstract criteria

    • Pass code/Behavior to method
    • Multiple behaviors, one parameter.

You can use anonymous classes to reduce redundant code before Java8.

The behavior of the sort method can be parameterized with the Java.util.Comparator object.

You can use the Runnable interface to represent a block of code to execute (but not return any interfaces, void).

Java 8 Combat (note) Chapter II

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.