Java Design Pattern Strategy pattern strategy and design pattern Strategy

Source: Internet
Author: User

Java Design Pattern Strategy pattern strategy and design pattern Strategy

This chapter describes the knowledge points related to policy patterns in java design patterns.

1. Rule mode definition

The rule mode, also known as the algorithm cluster mode, defines different algorithm families and can be replaced with each other. This mode allows algorithm changes to be independent of algorithm customers. The rule mode is the behavior mode of the object. It is intended to encapsulate an algorithm into an independent class with a common interface for mutual replacement. The policy mode allows the algorithm to change without affecting the client.

2. Rule mode structure

Policy mode encapsulates algorithms and separates the responsibility for using algorithms from the algorithms themselves and delegates them to different objects for management. Rule mode typically packs a series of algorithms into a series of policy classes and serves as a subclass of an abstract policy class. In one sentence, it is: "prepare a group of algorithms and encapsulate each algorithm to make them interchangeable ". The following describes the structure of the Policy mode instance with a schematic implementation.

1 // rule Mode Design Code 2 // calculation price base class 3 public interface MemberStrategy {4 public double calcPrice (double booksPrice ); 5} 6 // beginner member 7 public class PrimaryMemberStrategy implements MemberStrategy {8 @ Override 9 public double calcPrice (double booksPrice) {10 System. out. println ("No discounts for junior members"); 11 return booksPrice; 12} 13} 14 // intermediate member 15 public class IntermediateMemberStrategy implements MemberStrategy {16 @ Override17 public double calcPrice (double booksPrice) {18 System. out. println ("10% discount for intermediate members"); 19 return booksPrice * 0.9; 20} 21} 22 // senior member 23 public class AdvancedMemberStrategy implements MemberStrategy {24 @ Override25 public double calcPrice (double booksPrice) {26 System. out. println ("20% discount for senior members"); 27 return booksPrice * 0.8; 28} 29} 30 // calculation Price class 31 public class Price {32 // hold a specific policy object 33 private MemberStrategy strategy; 34/** 35 * constructor, input a specific policy object 36 * @ param strategy specific policy object 37 */38 public Price (MemberStrategy strategy) {39 this. strategy = strategy; 40} 41 42/** 43 * calculate the book price 44 * @ param booksPrice Original Price 45 * @ return calculate the discount price 46 */47 public double quote (double booksPrice) {48 return this. strategy. calcPrice (booksPrice); 49} 50} 51 // The Client calls 52 public class Client {53 public static void main (String [] args) {54 // select and create the expected policy object 55 MemberStrategy strategy = new AdvancedMemberStrategy (); 56 // create the environment 57 Price price = new Price (strategy ); 58 // calculation price 59 double quote = price. quote (300); 60 System. out. println ("final price of books:" + quote); 61} 62}

 

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.