本文以 商场促销 为例子来实现这种模式。
商场三种活动:
1.按照原来价格收费
2.八折收费
3.满300反100
一:工厂模式实现
二:策略模式实现
三:工厂+策略模式实现
package com.wzs.design;
/**
* 工厂模式的实现
*
* @author Administrator
*
*/
public class SimpleFactory_1 {
public static void main(String[] args) {
CashFactory cashFactory = new CashFactory();
System.out.println(cashFactory.createCashAccept(1).acceptCash(500));
System.out.println(cashFactory.createCashAccept(2).acceptCash(500));
System.out.println(cashFactory.createCashAccept(3).acceptCash(500));
System.out.println(cashFactory.createCashAccept(4).acceptCash(500));
}
}
/*
* 现金收费工厂
*/
class CashFactory {
public CashSuper createCashAccept(int type) {
CashSuper cashSuper = null;
switch (type) {
case 1:
System.out.println("正常收费.");
cashSuper = new CashNormal();
break;
case 2:
System.out.println("八折收费.");
cashSuper = new CashRebate(0.8);
break;
case 3:
System.out.println("满300反100.");
cashSuper = new CashReturn(300, 100);
break;
default:
System.out.println("默认正常收费.");
cashSuper = new CashNormal();
break;
}
return cashSuper;
}
}
/*
* 现金收取抽象类
*/
abstract class CashSuper {
public abstract double acceptCash(double money);
}
/*
* 正常收费
*/
class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
return money;
}
}
/*
* 打折收费
*/
class CashRebate extends CashSuper {
private double moneyRebate = 1d;// 打折率
public CashRebate(double moneyRebate) {
this.moneyRebate = moneyRebate;
}
@Override
public double acceptCash(double money) {
return money * moneyRebate;
}
}
/*
* 返利
*/
class CashReturn extends CashSuper {
private double moneyCondition = 0d;// 返利条件
private double moneyReturn = 0d;// 返利值
public CashReturn(double moneyCondition, double moneyReturn) {
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double money) {
if (money >= moneyCondition) {
return money - Math.floor(money / moneyCondition) * moneyReturn;
}
return money;
}
}---------------------------------------------------
package com.wzs.two;
import java.util.Scanner;
/**
* 策略模式
*
* @author Administrator
*
*/
public class StrategyPattern {
public static void main(String[] args) {
System.out.println("请输入收费模式:(1.正常收费 2.八折收费 3.满300反100)");
Scanner input = new Scanner(System.in);
int type = input.nextInt();
CashContext cashContext;
switch (type) {
case 1:
System.out.println("正常收费.");
cashContext = new CashContext(new CashNormal());
break;
case 2:
System.out.println("八折收费.");
cashContext = new CashContext(new CashRebate(0.8));
break;
case 3:
System.out.println("满300反100.");
cashContext = new CashContext(new CashReturn(300, 100));
break;
default:
System.out.println("默认正常收费.");
cashContext = new CashContext(new CashNormal());
break;
}
System.out.println(cashContext.getResult(500));
}
}
/*
* 收费策略
*/
class CashContext {
private CashSuper cashSuper;
public CashContext(CashSuper cashSuper) {
this.cashSuper = cashSuper;
}
public double getResult(double money) {
return cashSuper.acceptCash(money);
}
}
/*
* 现金收取抽象类
*/
abstract class CashSuper {
public abstract double acceptCash(double money);
}
/*
* 正常收费
*/
class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
return money;
}
}
/*
* 打折收费
*/
class CashRebate extends CashSuper {
private double moneyRebate = 1d;// 打折率
public CashRebate(double moneyRebate) {
this.moneyRebate = moneyRebate;
}
@Override
public double acceptCash(double money) {
return money * moneyRebate;
}
}
/*
* 返利
*/
class CashReturn extends CashSuper {
private double moneyCondition = 0d;// 返利条件
private double moneyReturn = 0d;// 返利值
public CashReturn(double moneyCondition, double moneyReturn) {
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double money) {
if (money >= moneyCondition) {
return money - Math.floor(money / moneyCondition) * moneyReturn;
}
return money;
}
}---------------------------------------------------
package com.wzs.three;
/**
* 策略模式和工厂模式结合
*
* @author Administrator
*
*/
public class StrategyAndFactory {
public static void main(String[] args) {
System.out.println(new CashContext(1).getResult(500));
System.out.println(new CashContext(2).getResult(500));
System.out.println(new CashContext(3).getResult(500));
System.out.println(new CashContext(4).getResult(500));
}
}
/*
* 收费策略
*/
class CashContext {
private CashSuper cashSuper;
public CashContext(int type) {
switch (type) {
case 1:
System.out.println("正常收费.");
cashSuper = new CashNormal();
break;
case 2:
System.out.println("八折收费.");
cashSuper = new CashRebate(0.8);
break;
case 3:
System.out.println("满300反100.");
cashSuper = new CashReturn(300, 100);
break;
default:
System.out.println("默认正常收费.");
cashSuper = new CashNormal();
break;
}
}
public double getResult(double money) {
return cashSuper.acceptCash(money);
}
}
/*
* 现金收取抽象类
*/
abstract class CashSuper {
public abstract double acceptCash(double money);
}
/*
* 正常收费
*/
class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
return money;
}
}
/*
* 打折收费
*/
class CashRebate extends CashSuper {
private double moneyRebate = 1d;// 打折率
public CashRebate(double moneyRebate) {
this.moneyRebate = moneyRebate;
}
@Override
public double acceptCash(double money) {
return money * moneyRebate;
}
}
/*
* 返利
*/
class CashReturn extends CashSuper {
private double moneyCondition = 0d;// 返利条件
private double moneyReturn = 0d;// 返利值
public CashReturn(double moneyCondition, double moneyReturn) {
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double money) {
if (money >= moneyCondition) {
return money - Math.floor(money / moneyCondition) * moneyReturn;
}
return money;
}
}
1563





