一、商场收银软件
用简单工厂实现:
abstract class CashSuper { // 现金收费抽象类
public abstract double acceptCash(double money);
}
class CashNormal extends CashSuper { // 正常收费子类
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money;
}
}
class CashRebate extends CashSuper { // 打折收费子类
private double moneyRebate = 1d;
public CashRebate(double moneyRebate) { // 打折收费,初始化时要输入折扣率
this.moneyRebate = moneyRebate;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money * moneyRebate;
}
}
class CashReturn extends CashSuper { // 返利收费子类
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
public CashReturn(double moneyCondition, double moneyReturn) {
super();
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
double result = money;
if (money >= moneyCondition) {
result = money - Math.floor(money / moneyCondition) * moneyReturn;
}
return result;
}
}
class CashFactory { // 现金收费工厂类
public static CashSuper createCashAccept(String type) {
CashSuper cs = null;
switch (type) {
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
CashReturn cr1 = new CashReturn(300, 100);
cs = cr1;
break;
case "打8折":
CashRebate cr2 = new CashRebate(0.8);
cs = cr2;
break;
}
return cs;
}
}
二、策略模式
策略模式:它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户。
//策略模式
abstract class Strategy { // Strategy类,定义所有支持的算法的公共接口
public abstract void AlgorithmInterface();
}
class ConcreteStrategyA extends Strategy { // 具体算法A
@Override
public void AlgorithmInterface() {
// TODO Auto-generated method stub
System.out.println("算法A实现");
}
}
class ConcreteStrategyB extends Strategy { // 具体算法B
@Override
public void AlgorithmInterface() {
// TODO Auto-generated method stub
System.out.println("算法B实现");
}
}
class ConcreteStrategyC extends Strategy { // 具体算法C
@Override
public void AlgorithmInterface() {
// TODO Auto-generated method stub
System.out.println("算法C实现");
}
}
class Context { // Context,用一个ContextStrategy来配置,维护一个对Strategy对象的引用
Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void ContextInterface() {
strategy.AlgorithmInterface();
}
}
public class Main {
public static void main(String[] args) {
Context context;
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
context = new Context(new ConcreteStrategyC());
context.ContextInterface();
}
}
用策略模式实现银行收银:
//用策略模式实现银行收银
abstract class CashSuper { // 现金收费抽象类
public abstract double acceptCash(double money);
}
class CashNormal extends CashSuper { // 正常收费子类
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money;
}
}
class CashRebate extends CashSuper { // 打折收费子类
private double moneyRebate = 1d;
public CashRebate(double moneyRebate) { // 打折收费,初始化时要输入折扣率
this.moneyRebate = moneyRebate;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money * moneyRebate;
}
}
class CashReturn extends CashSuper { // 返利收费子类
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
public CashReturn(double moneyCondition, double moneyReturn) {
super();
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
double result = money;
if (money >= moneyCondition) {
result = money - Math.floor(money / moneyCondition) * moneyReturn;
}
return result;
}
}
class CashContext {
private CashSuper cs;
public CashContext(CashSuper csuper) {
this.cs = csuper;
}
public double GetResult(double money) {
return cs.acceptCash(money);
}
}
策略模式与简单工厂结合:
//策略与简单工厂结合
abstract class CashSuper { // 现金收费抽象类
public abstract double acceptCash(double money);
}
class CashNormal extends CashSuper { // 正常收费子类
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money;
}
}
class CashRebate extends CashSuper { // 打折收费子类
private double moneyRebate = 1d;
public CashRebate(double moneyRebate) { // 打折收费,初始化时要输入折扣率
this.moneyRebate = moneyRebate;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money * moneyRebate;
}
}
class CashReturn extends CashSuper { // 返利收费子类
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
public CashReturn(double moneyCondition, double moneyReturn) {
super();
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
double result = money;
if (money >= moneyCondition) {
result = money - Math.floor(money / moneyCondition) * moneyReturn;
}
return result;
}
}
class CashContext {
private CashSuper cs = null;
public CashContext(String type) {
switch (type) {
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
CashReturn cr1 = new CashReturn(300, 100);
cs = cr1;
break;
case "打8折":
CashRebate cr2 = new CashRebate(0.8);
cs = cr2;
break;
}
}
public double GetResult(double money) {
return cs.acceptCash(money);
}
}