public class WindowState {
private String stateValue;
public WindowState(String stateValue) {
this.stateValue = stateValue;
}
public String getStateValue() {
return stateValue;
}
public void setStateValue(String stateValue) {
this.stateValue = stateValue;
}
public void handle() {
/*
* 根据不同状态做不同操作, 再切换状态
*/
if ("窗口".equals(stateValue)) {
switchWindow();
this.stateValue = "全屏";
} else if ("全屏".equals(stateValue)) {
switchFullscreen();
this.stateValue = "窗口";
}
}
private void switchWindow() {
System.out.println("切换为窗口状态");
}
private void switchFullscreen() {
System.out.println("切换为全屏状态");
}
}
/**
* 状态的使用
*/
public class WindowContext {
private WindowState state;
public WindowContext(WindowState state) {
this.state = state;
}
public WindowState getState() {
return state;
}
public void setState(WindowState state) {
this.state = state;
}
public void switchState() {
this.state.handle();
}
}
/*
* 状态(State)模式 行为型模式
* 既改变对象的状态,又改变对象的行为
* 根据状态,改变行为
*/
public class Test {
public static void main(String[] args) {
/*
* 本例的 状态值只有两个,由状态类自身控制
* 也可以把状态值的控制,交由客户端来设置
*/
WindowContext context = new WindowContext(new WindowState("窗口"));
context.switchState();
context.switchState();
context.switchState();
context.switchState();
}
}
切换为窗口状态 切换为全屏状态 切换为窗口状态 切换为全屏状态
/**
* 商品促销
* 本类为:收取现金的类
*/
public interface ICashSuper {
double acceptCash(double money);
}
/**
* 正常收取现金
* @author stone
*
*/
public class CashNormal implements ICashSuper {
@Override
public double acceptCash(double money) {
return money;
}
}
/**
* 打折收取现金
* @author stone
*
*/
public class CashRebate implements ICashSuper {
private double rebate; //折扣
public CashRebate (double rebate) {
this.rebate = rebate;
}
@Override
public double acceptCash(double money) {
return new BigDecimal(money * rebate / 10).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
/**
* 让利返现 收取现金
* @author stone
*
*/
public class CashReturn implements ICashSuper {
private double moneyCondition; //返现底限金额
private double returnMoney; //返还金额
public CashReturn(double moneyCondition, double returnMoney) {
this.moneyCondition = moneyCondition;
this.returnMoney = returnMoney;
}
@Override
public double acceptCash(double money) {//多重返利
if (money >= moneyCondition) {
return money - Math.floor(money / moneyCondition) * returnMoney;
} else {
return money;
}
}
}
/**
* 根据传递的的策略类,执行相应的行为
*/
public class CashContext {
private ICashSuper casher;
public CashContext() {
}
public CashContext(ICashSuper casher) {
this.casher = casher;
}
public void setCasher(ICashSuper casher) {
this.casher = casher;
}
//根据具体的策略对象,调用它的算法行为
public double acceptCash(double money) {
return this.casher.acceptCash(money);
}
}
public class Test {
public static void main(String[] args) {
double money = 998; //原价
CashContext cashContext = new CashContext(new CashNormal());
System.out.println("原价:" + cashContext.acceptCash(money)); //通常 策略
cashContext.setCasher(new CashRebate(8.5));
System.out.println("打85折:" + cashContext.acceptCash(money)); //折扣 策略 85折
cashContext.setCasher(new CashReturn(300, 50));
System.out.println("满300 返50:" + cashContext.acceptCash(money)); //返现 策略 满300 返50
}
}
原价:998.0 打85折:848.3 满300 返50:848.0
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有