Design pattern-Behavioral mode-policy mode

Source: Internet
Author: User

Policy mode

I used the strategy mode in the actual work, but why should have the environment role ?

Here I put in English on the meaning of the introduction,

The strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients.

And then look at the composition of this design pattern,

In general, the strategy model is divided into the following three roles:
1. Environment Role (context): holding a policy class reference

Advantages: (1) Increase the security of the system if necessary, and reduce the redundancy of the code (detailed below).
2. Abstract strategy (Strategy): Defines the public interface of multiple specific policies, the different algorithms in the specific policy classes implement this interface in different ways, and the context uses these interfaces to invoke algorithms of different implementations. In general, we use interfaces or abstract classes to implement.
3. Specific policy (Concretestrategy): implements the associated algorithm or operation in an abstract policy class.

I put on a simple implementation

Abstract class (abstract policy)

 Public Abstract class abstractstrategy {    publicabstractvoid  algorithm ();}

Specific strategy-A

 Public class extends abstractstrategy {    @Override    publicvoid  algorithm () {        SYSTEM.OUT.PRINTLN ("strategy A");}    }

Concrete strategy-B

 Public class extends abstractstrategy {    @Override    publicvoid  algorithm () {        SYSTEM.OUT.PRINTLN ("strategy B");}    }

How does the environmental role work?

 Public class Context {    private  abstractstrategy strategy;      Public Context (Abstractstrategy strategy) {        this. Strategy = strategy;    }       Public void algorithm () {        this. Strategy.algorithm ();    }}

We use it when we're using it.

 Public Static void Main (string[] args) {    //strategy Pattern Demo    new Context (new Concretestrategya ());    Context.algorithm ();     New Context (new  concretestrategyb ());    Context.algorithm ();}

Careful as you should think I use the following code can also be implemented, the existence of the context is for what?

Newnew  concretestrategyb (); Abstractstrategy.algorithm ();

Here we look at the first phenomenon:

Modifying methods in an abstract policy

 Public Abstract void Algorithm (int number);

Modify the implementation of a specific policy

@Override  Public void algorithm (int number ) {    System.out.println (number);};

See here you will not feel this parameter number has security risk, pass in the legal? So I want to judge, so, you can each specific strategy to write a section of the same logic to judge, or to judge the argument, then do not want to copy you are now looking for a place to unify judgment, is the context (in. NET, there are dbcontext ... This moment shows the importance, we can judge in the constructor, then we summarize the first advantage of the context: if necessary, it can increase the security of the system and reduce the redundancy of the Code . What are the advantages? We need to be in the actual coding to summarize, I hope we can discuss together.

This needs to be compared with the factory model and differentiated.

  • Create pattern
  • Single-Case mode
  • Factory mode
  • Abstract Factory mode
  • Creator mode
  • Prototype mode
  • Structural patterns
  • Adapter mode
  • Bridge mode
  • Combination mode
  • Decoration mode
  • Appearance mode
  • Enjoy meta mode
  • Proxy mode
  • Behavioral patterns
  • Responsibility chain Model
  • Template method Mode
  • Interpreter mode
  • Command mode
  • Iterator mode
  • Broker mode
  • Memo Mode
  • Observer pattern
  • State mode
  • Policy mode
  • Visitor mode

Design pattern-Behavioral mode-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.