Introduction to Strategy, strategy

Source: Internet
Author: User

Introduction to Strategy, strategy
I. Introduction to Strategy

The rule mode is the action mode.

Behavior mode:Defines the responsibilities of each object and the communication mode between objects. It standardizes the call and data transmission methods between objects.

The rule mode is suitable for situations where algorithms change frequently.

The algorithm changes will not affect the customers who use the algorithm. The algorithm can be independent of the customers who use the algorithm.

Ii. Simple Example

Ordinary customers, members, and VIP members purchase different prices.

Abstract class

Package strategy;/** abstract class for commodity price calculation */public abstract class Account {abstract public double getPrice (int amount, double d );}

Normal

Package strategy;/*** commodity price calculation for common customers */public class CommonAccount extends Account {@ Override public double getPrice (int amount, double price) {return amount * price ;}}

 

Member

Package strategy;/*** calculate the commodity price of ordinary members. */public class InsiderAccount extends Account {@ Override public double getPrice (int amount, double price) {return amount * price * 9/10 ;}}

 

VIP

Package strategy;/*** calculate the commodity price of VIP members */public class VipAccount extends Account {@ Override public double getPrice (int amount, double price) {return amount * price * 8/10 ;}}

Context

Package strategy;/*** this class is used to maintain reference of the Policy class */public class Context {// reference private Account account Account; // initialize public Context (account Account account) {this. account = account;} // call the calculation method public double doAccount (int amount, double d) {return account. getPrice (amount, d );}}

Test class

Package strategy;/*** this policy mode Test class */public class Test {public static void main (String [] args) {// declare the product String name = "DVD "; int amount = 2; double price = 50; double sum = 0; // declare the object // normal customer Context context = new Context (new CommonAccount (); sum = context. doAccount (amount, price); System. out. println ("ordinary Customer: no discount, purchased product name:" + name + "Quantity:" + amount + "unit price:" + price + "Payable amount: "+ sum); // normal member context = new Context (new InsiderAccount (); sum = context. doAccount (amount, price); System. out. println ("ordinary member: Off, purchased product name:" + name + "Quantity:" + amount + "unit price:" + price + "Payable amount: "+ sum); // VIP context = new Context (new VipAccount (); sum = context. doAccount (amount, price );
System. out. println ("VIP Customer: off, purchased product name:" + name + "Quantity:" + amount + "unit price:" + price + "Payable amount: "+ sum );}}

Result:

Ordinary customers: no discount, purchased product name: Number of DVDs: 2 unit price: 50.0 amount payable: 100.0 ordinary members: discount, purchased product name: Number of DVDs: 2 unit price: 50.0 payable amount: 90.0VIP customers: 50.0 off, purchased product name: Number of DVDs: 2 unit price: 80.0 payable amount:

 

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.