Policy Mode-java Implementation

Source: Internet
Author: User

Policy Mode-java Implementation

1. Real-World needs

I am now responsible for the development and maintenance of the assessment system, one of the modules called the preparation of work plans. Is the work plan will have time, our various reminders have been done, but still someone to the X-month work plan content write to Y months, really no language! Some might say that it's OK to let the customer correct all the wrong work plans directly, but do you think the customer will be willing? Customers always want to do the least thing to bring the most results.

Well, that's the direct modification of the time. This is the problem, if you change the time, already have a work plan what to do? Is it covering, or merging, or ...? At this point we need to provide our customers with a strategy for their choice. Of course, there are many restrictions on the specific business, which is not specifically described here. Maybe we are not thinking about all the situation, or the customer has a new demand, then we directly add to the strategy, change a little code. Basically accord with our object-oriented principle of the opening and closing principle (open to the extension, to modify the relationship), the realization of cohesion poly-low coupling.

2. Policy mode definition

The strategy pattern, also called the algorithm cluster pattern, is the definition of different algorithm families, and can be replaced between each other, this mode allows the algorithm to change independently of the customer using the algorithm.

3. Design principles
The design principle is to extract the parts of a class that are often changed or may change in the future, and then include an instance of the object in the class as an interface, so that an instance of the class can invoke the behavior of the class that implements the interface at run time. Here is an example.
The policy pattern belongs to the object behavior pattern, which is mainly for a set of algorithms, encapsulating each algorithm in a separate class with a common interface, so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client. Typically, a policy pattern is used when an application needs to implement a particular service or feature, and the program is implemented in a variety of ways.

4. There are three objects in the policy mode:
(1) Environment object: a reference to an interface or abstract class defined in an abstract policy is implemented in this class.
(2) Abstract policy object: it can be implemented by an interface or an abstract class.
(3) Specific policy object: it encapsulates different algorithms for implementing the same function.
Using the policy model to build the application, you can choose different algorithms to implement the functions of the application according to the user configuration and other content. The specific selection has the environment object to complete. This approach avoids the code clutter caused by the use of conditional statements, improving application flexibility and rationality.

5. To make it easier for everyone to understand, we will explain it in most cases on the Internet.

Liu Bei to the Jiangdong to marry his wife, go before Zhuge Liang to Zhao Yun (best man) Three ace, said is the secret to solve the thorny problem, hey, not to mention, really solve the big problem, get to the end is Zhou Yu accompany the lady and fold soldiers, then we first see what this scene is like.

First of all, talk about the elements of this scene: Three clever tricks, a brocade sac, a Zhao Yun, the ingenious plan is the bright elder brother gives, the ingenious idea puts in the brocade sac, is commonly known is the ace, that Zhao Yun is a work person, from the brocade sac takes out the ingenious plan, executes, then wins. How do you show these with Java programs?

Class Diagram:

Project structure:

Specific code

 Packagechengxuyuanzhilu.com.sp;/** * @authorPublic Number: Programmer's Road * First define a policy interface, this is Zhuge Liang to Zhao Yun's three tricks of the interface. */ Public InterfaceIStrategy {//Each of these tricks is an executable algorithm.      Public voidoperate ();} PackageChengxuyuanzhilu.com.sp.imp;ImportChengxuyuanzhilu.com.sp.IStrategy;/** * @authorPublic Number: Programmer's Road * Find Qiaoguo old help, so that Sun Quan can not kill Liu Bei*/ Public classBackdoorImplementsIStrategy {@Override Public voidoperate () {SYSTEM.OUT.PRINTLN ("Find Qiaoguo old help, let Wu too to Sun Quan pressure, so that he can't kill Liu Bei ..."); }} PackageChengxuyuanzhilu.com.sp.imp;ImportChengxuyuanzhilu.com.sp.IStrategy;/** * @authorPublic Number: Programmer's Road * neighbour, block the pursuers.*/ Public classBlackenemyImplementsIStrategy {@Override Public voidoperate () {SYSTEM.OUT.PRINTLN ("Neighbour, block the pursuers ..."); }} PackageChengxuyuanzhilu.com.sp.imp;ImportChengxuyuanzhilu.com.sp.IStrategy;/** * @authorPublic Number: Programmer's Road * Ask Wu to open a green light*/ Public classGivengreenlightImplementsIStrategy {@Override Public voidoperate () {SYSTEM.OUT.PRINTLN ("Ask Wu too open a green light, release!" "); }} Packagechengxuyuanzhilu.com.sp;/** * @authorthe public number: Programmer's Way * put strategy of the brocade SAC*/ Public classContext {//the advantage of an interface is that it can point to any implementation class that implements it    Privateistrategy strategy; //through the structure of the balloon, to specify the specific strategy to choose which to deal with the risks encountered     PublicContext (IStrategy strategy) { This. Strategy =strategy; }         Public voidoperate () { This. Strategy.operate (); }} Packagechengxuyuanzhilu.com.sp;ImportChengxuyuanzhilu.com.sp.imp.BackDoor;ImportChengxuyuanzhilu.com.sp.imp.BlackEnemy;ImportChengxuyuanzhilu.com.sp.imp.GivenGreenLight;/** * @authorPublic Number: Programmer's Road * Zhao Yun is our real-life customer*/ Public classZhaoyun {/*** Zhao Yun appearance, he according to Zhuge Liang to his account, and then disassemble the ingenious plan*/     Public Static voidMain (string[] args) {context context; //when I first came to Wu, I opened the one.System.out.println ("----------just opened the first---------------when he arrived in the Kingdom of Wu."); Context=NewContext (Newbackdoor ()); Context.operate ();//disassemble ExecutionSystem.out.println ("\ n"); //when Liu Bei had a second,System.out.println ("----------Liu Bei, took a second---------------"); Context=NewContext (Newgivengreenlight ()); Context.operate ();//disassemble ExecutionSystem.out.println ("\ n"); //What about Sun Quan's little pursuers? Unpack the third bag .System.out.println ("----------The Little pursuers of Sun Quan, what should I do?" Open the third---------------of the Sac "); Context=NewContext (NewBlackenemy ()); Context.operate ();//disassemble ExecutionSystem.out.println ("\ n"); }}

Execution structure:

Policy Mode-java Implementation

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.