Command mode & Policy mode & Template method

Source: Internet
Author: User

First, the Strategy model

Policy mode: Encapsulates easily variable algorithms that can be replaced by each other.

GoF "design mode" says: Define a series of algorithms, encapsulate them one by one, and make them interchangeable. This pattern allows the algorithms to be independent of their customer changes.

For example: A push service class, push the way, can be divided into: QQ Push, mailbox push, App push, PC plug-in push.

Here are two points to be told:

       1, push the way can replace each other;

2, these push the way is simply belong to the push service this class itself.

Think about it. Key words: replace each other

Second, the command mode

Command pattern: Solving "behavior requestor" and "behavior implement" usually presents a "tight coupling" problem.

GoF "design mode" says: Encapsulate a request as an object, allowing you to parameterize the customer with different requests, queue up requests or log requests, and support revocable operations.

For example, an intelligent remote control, can be controlled home lights, electric kettles, routers, these open and close.

We use Strategy mode?

OK, remote control class, abstract open, abstract off. But how do you get the electric kettle?

So in the final analysis: definition is important. The command pattern has two important roles-the requestor and the implementing person.

The remote control is the requestor, these appliances are the realization. The key is the opening and closing of the electric kettle can not be replaced AH.

Three, template method: to be supplemented

Command mode & Policy mode & Template Method

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.