Java Design Pattern Learning Note Strategy Model (duck example)

Source: Internet
Author: User

0. Suppose an existing project (Duck) encounters an issue that adds functionality to a class, how do I design a class to add new functionality?

1. What are the disadvantages of using the duck (duck) provided by inheritance?

(1) Code repeats in multiple subclasses

(2) Many men know the whole behavior of all ducks

(3) The behavior of the operation is not easy to change

(4) Change will lead to the whole body, causing the other ducks do not want to change

"Design Principles"

Find out where changes may be needed in your application, separate them, and don't mix with code that doesn't need to change

2. Parts that change and do not change

Divide the duck into two parts, duck and duck behavior.

The intrinsic properties and common methods of a class are written in a class, and the behavior is another class (the class that is said here may also be an interface)

3. Design behavior

The Duck class implements a method that implements the design interface in a class so that it can dynamically load the behavior method at run time.

"Design Principles"

Programming for interfaces, not for implementation

"Programming for Interfaces" is the key to using polymorphic programming. With polymorphic programming, subclasses instantiate an action that does not need to be hardcoded in the code, but rather specifies a specific thought object at run time.

4. Integrated Duck Behavior

5, packaging behavior bigger picture, such as:

"Design Principles"

Multi-use combination, less inheritance.

Java Design Pattern Learning Note Strategy Model (duck example)

Related Article

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.