One of the Java23 design modes: Policy mode

Source: Internet
Author: User
Tags ming

Due to the recent study and design patterns, I will use my own understanding of the design pattern to express the learning and understanding, through the most commonly used, well-remembered cases to remember and use design patterns, I hope to improve and improve the design code.

I. Application background
In software development often encountered this situation, to achieve a certain function has a number of algorithms or strategies, we can choose different algorithms or strategies according to the different scenarios to complete the function. Extract the parts of a class (a) that change frequently or may change in the future, as an interface (b), and then include this interface (b) in class (a) so that instances of class (a) can invoke the behavior of the class (C) that implements the interface at run time. For example, define a series of algorithms, encapsulate each algorithm, and make them interchangeable, so that the algorithm can be independent of the customers who use it to change. This is the policy mode.

Two. Advantages and disadvantages

Advantages:
1, can dynamically change the behavior of the object
Disadvantages:
1, the client must know all the policy classes, and decide for themselves which policy class to use
2. Policy mode will result in many policy classes

Three. Composition

1. Operating Environment class: strategy

The environment in which this strategy mode runs is actually where it is used

2. Application Scenario Class: Person

This is the class that the client accesses, that is, the policy that the object of the class holds

3 Specific policy classes: Car

Implementing policy classes specifically

4. Abstract Policy class: Carfunction

Interfaces that produce different strategies or algorithms based on different requirements

Four. Code implementation

1. Abstract Policy class: Carfunction

 1 package Com.design.strategy;  2 /** 3 * @ClassName: carfunction  4 * @Description: Policy class Span style= "COLOR: #008080" > 5 * 6 */ 7  8 public interface carfunction { 9 void run (); // Each car has different driving methods 10}   

2. Specific policy parent class

1 Package Com.design.strategy;2 /**3  * 4 * @ClassName: Car5 * @Description: Each car has the same properties and behavior6  *7  */8  Public classCar implements Carfunction {9     protectedString name;//Car nameTen     protectedString color;//Car Color One     PrivateCarfunction carfunction;//Policy Classes A      -      PublicCar (string name, string color) { -          This. Name =name; the          This. color =color; -     } -  - @Override +      Public voidrun () { -System. out. println (color +" "+ name +"on the road ... "); +     } A      at}

3. Specific policy Implementation subclass

1  PackageCom.design.strategy;2 /**3  * 4 * @ClassName: Smallcar5 * @Description: Specific policy implementation subclass6  *7  */8  Public classSmallcarextendsCar {9 Ten      PublicSmallcar (string name, string color) { One         Super(name, color); A     } -      -      Public voidrun () { theSYSTEM.OUT.PRINTLN (color + "+ name +" in high speed driving ...) "); -     } -      -}

4. Application Scenario Classes

1  PackageCom.design.strategy;2 /**3  * 4 * @ClassName: person5 * @Description: Application Scenario Class6  *7  */8  Public classPerson {9     PrivateString name;//nameTen     PrivateInteger age;//Age One     PrivateCar car;//owning a car A      -      Public voiddriver (car car) { -System.out.print (name + "" + age+ "years" + "open"); the Car.run (); -     } -  -      PublicPerson (String Name,integer age) { +          This. name=name; -          This. age=Age ; +     } A  at}

5. Operating Environment class: strategy

1  PackageCom.design.strategy;2 /**3  * 4 * @ClassName: Strategy5 * @Description: Operating Environment class: Strategy6 * @date: December 9, 2017 morning 11:43:587  *8  */9  Public classStrategy {Ten      Public Static voidMain (string[] args) { OneCar Smallcar =NewSmallcar ("Land Rover", "Black"); ACar Busscar =NewBusscar ("Mercedes", "White"); -person P1 =NewPerson ("xiaoming", 20); - P1.driver (smallcar); the P1.driver (busscar); -     } - } -  + Operation Result: -Xiao Ming 20years old with black Land Rover driving at high speed ...  +Xiao Ming 20-year-old driving in the white Mercedes-Benz slowly ...

Five. Summary

Strategy mode can be understood as the driver drive, but he today think of Land Rover, tomorrow to drive a Mercedes ... , according to his different needs, to produce different coping strategies, to remember is: Old driver driving!!! Ha ha...

Six. Extension

Also can extend to the business activities, consumption of the number of dollars minus 50 of such scenarios, and so on, extrapolate, will find that there are many examples of life is the use of strategic mode.

One of the Java23 design modes: Policy mode

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.