The strategy mode of Java design pattern

Source: Internet
Author: User

1. Definition
Policy mode: Defines a set of algorithms that encapsulate each algorithm so that they can be replaced with each other. The policy pattern allows the algorithm to change independently of the client that calls it. The policy pattern enables this set of algorithms to change when the client calls them.
2. There are three characters in the strategy mode

(1) abstract strategy (strategy): typically implemented by an interface or abstract class. A common interface that defines a number of specific policies, the different algorithms in a particular policy class implement this interface in different ways, and the context uses these interfaces to invoke algorithms of different implementations.

(2) Specific strategy (CONCRETESTRATEGY): Implement the Strategy interface or inherit from the abstract class strategy, encapsulating the specific algorithm and behavior.

(3) Environment Class (Contex): holds a reference to a public policy interface that is called directly to the client.

3. Structure diagram of the strategy mode
4. The strategy mode realizes the thought

(1) Define a common interface (strategy) for a policy object

1  Public Interface Strategy {2      Public void algorithm (); 3 }

(2) Define a specific policy class (Concretestrategy) to implement the above interface

 Public class Implements Strategy {    @Override    publicvoid  algorithm () {        System.out.println ("first algorithm.") );    }}
 Public class Implements Strategy {        @Override    publicvoid  algorithm () {        System.out.println ( "Second algorithm." );    }    }

(3) Define an environment class (Contex), which holds a reference to the public interface, and the corresponding get, set method, constructor method

 Public classContext {strategy strategy;  PublicContext (Strategy strategy) {//constructor Function        Super();  This. Strategy =strategy; }         PublicStrategy Getstrategy () {//Get Method        returnstrategy; }         Public voidSetstrategy (Strategy strategy) {//Set Method         This. Strategy =strategy; }         Public voidalgorithm () {strategy.algorithm (); }}

Finally, the client freely invokes the policy ^_^

 Public classstrategyclient { Public Static voidMain (string[] args) {//Selecting a policy using a constructorContext context=NewContext (Newfirststrategy ());                Context.algorithm (); //using the GET, set method to switch policies//Context Context=new context (); //Contex.set (New Firststrategy ()); //Context.algorithm (); //Switch to another policyContext secondcontext=NewContext (Newsecondstrategy ());                Secondcontext.algorithm (); //Context Secondcontext=new context (); //Secondcontext.set (New Secondstrategy ()); //secondcontext.algorithm ();    }}

5. Summary
(1) The key point of the strategy mode is what kind of action is performed on what kind of strategy is passed to the object. (2) Strategy mode Advantages : Can easily expand and change the strategy, can dynamically change the behavior of the object. (3) Strategy mode disadvantage : The client must know all the policy classes and decide which one to use at its own discretion. Each specific strategy produces a new class, which results in a number of policy classes.
6, I wrote a complete strategy mode demo, decompression can run, the need to leave the mailbox.

----------------------------------I have a bottom line----------------------------------

The strategy mode of Java design pattern

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.