Big talk design mode Java version Chapter II policy mode

Source: Internet
Author: User

package strategy;//嵌入策略publicclass CashContext {    private Cash cs;    publicCashContext(Cash cs) {        super();        this.cs = cs;    }    publicdoublegetResult(double money){        return cs.acceptCash(money);    }}
package strategy; Public  interface  Cash {public  abstract  double   Acceptcash  (double  Money);}  
package strategy;/* * 正常收费 */publicclass CashNormal implements Cash{    @Override    publicdoubleacceptCash(double money) {        return money;    }}
 Packagestrategy;/ * Discount strategy * / Public  class cashrebate implements Cash{    Private DoubleMoneyrebate; Public cashrebate() {Super(); } Public cashrebate(DoubleMoneyrebate) {Super(); This. moneyrebate = moneyrebate; } Public Double getmoneyrebate() {returnMoneyrebate; } Public void setmoneyrebate(DoubleMoneyrebate) { This. moneyrebate = moneyrebate; }@Override     Public Double Acceptcash(DoubleMoney) {returnMoney*moneyrebate; }}
 Packagestrategy;/ * Rebate Charge Strategy */ Public  class Cashreturn implements Cash {    Private DoubleMoneycondition;Private DoubleMoneyreturn; Public Cashreturn() {Super(); } Public Cashreturn(DoubleMoneycondition,DoubleMoneyreturn) {Super(); This. moneycondition = moneycondition; This. Moneyreturn = Moneyreturn; } Public Double getmoneycondition() {returnMoneycondition; } Public void setmoneycondition(DoubleMoneycondition) { This. moneycondition = moneycondition; } Public Double Getmoneyreturn() {returnMoneyreturn; } Public void Setmoneyreturn(DoubleMoneyreturn) { This. Moneyreturn = Moneyreturn; }@Override     Public Double Acceptcash(DoubleMoney) {if(money>=moneycondition) {returnmoney-(int) (money/moneycondition) *moneyreturn; }returnMoney }}

Test class:

Package Strategy;import Java.util.Scanner;/ * * Test code * / Public classTest { Public Static void Main(string[] args) {Cashcontext cc=NULL;DoubleMoneyrebate=0;Doublemoney=0; Scanner sc=NewScanner (System.inch); System. out. println ("Please enter a discount type"); String type = Sc.next ();Switch(type) { Case "Normal charge": cc=NewCashcontext (NewCashnormal ()); Break; Case "Discount": System. out. println ("Please enter the discount ratio");            Moneyrebate=sc.nextdouble (); Cc=NewCashcontext (NewCashrebate (moneyrebate)); Break; Case "Rebate": cc=NewCashcontext (NewCashreturn ( -, -)); Break; } System. out. println ("Please enter Amount");        Money = Sc.nextdouble (); System. out. println (Cc.getresult (money)); }}

Big talk design mode Java version Chapter II 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.