文章目录
一、状态模式
允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。
二、状态模式示例代码
模拟会员等级打折。金卡会员打5折,银卡会员打7.5折,铜卡会员打8折。
1.接口
会员卡信息
package behavior.StatePattern;
import java.math.BigDecimal;
/**
* 会员卡信息:
* 积分1元钱
* 1000积分金卡
* 800积分银卡
* 500积分铜卡
*/
public class MemberCardInfo {
/**
* 会员等级
*/
private MemberCardLevel memberCardLevel;
/**
* 会员积分
*/
private Integer memberPoints = 0;
public MemberCardInfo(MemberCardLevel memberCardLevel) {
this.memberCardLevel = memberCardLevel;
memberCardLevel.updateMemLevel();
}
public MemberCardInfo(MemberCardLevel memberCardLevel, Integer memberPoints) {
this.memberCardLevel = memberCardLevel;
this.memberPoints = memberPoints;
this.memberCardLevel.setMemberCardInfo(this);
}
public Integer getMemberPoints() {
return memberPoints;
}
public void setMemberPoints(Integer memberPoints) {
this.memberPoints = memberPoints;
}
public MemberCardLevel getMemberCardLevel() {
return memberCardLevel;
}
public void setMemberCardLevel(MemberCardLevel memberCardLevel) {
this.memberCardLevel = memberCardLevel;
this.memberCardLevel.setMemberCardInfo(this);
}
/**
* 结算
* @param amt
*/
public void doBalance(BigDecimal amt){
this.memberCardLevel.doBalance(amt);
}
}
package behavior.StatePattern;
import java.math.BigDecimal;
/**
* 提供一个会员等级接口
*/
public abstract class MemberCardLevel {
protected MemberCardInfo memberCardInfo;
public void setMemberCardInfo(MemberCardInfo memberCardInfo) {
this.memberCardInfo = memberCardInfo;
}
public abstract void updateMemLevel();
/**
* 结算
*/
public abstract void doBalance(BigDecimal amt);
}
2.会员实现
/**
* 金卡会员
*/
public class GoldCardMember extends MemberCardLevel {
@Override
public void updateMemLevel() {
System.out.println("你已经是金卡会员啦");
}
@Override
public void doBalance(BigDecimal amt) {
System.out.print("金卡会员打折前金额:"+amt);
System.out.println(",金卡会员打5折之后金额:"+amt.multiply(new BigDecimal("0.5")));
}
}
package behavior.StatePattern;
import java.math.BigDecimal;
/**
* 银卡会员
*/
public class SilverCardMember extends MemberCardLevel{
@Override
public void updateMemLevel() {
System.out.println("恭喜成为银卡会员!");
}
@Override
public void doBalance(BigDecimal amt) {
if(memberCardInfo.getMemberPoints() + amt.intValue() > 1000 ){
memberCardInfo.setMemberCardLevel(new GoldCardMember());
}else{
memberCardInfo.setMemberPoints(memberCardInfo.getMemberPoints() + amt.intValue());
System.out.print("银卡会员打折前金额:"+amt);
System.out.println(",银卡会员打7.5折之后金额:"+amt.multiply(new BigDecimal("0.75")));
}
}
}
package behavior.StatePattern;
import java.math.BigDecimal;
/**
* 铜卡会员
*/
public class CopperCardMember extends MemberCardLevel {
@Override
public void updateMemLevel() {
System.out.println("恭喜你成为铜卡会员!");
}
@Override
public void doBalance(BigDecimal amt) {
if ( memberCardInfo.getMemberPoints() + amt.intValue() > 1000){
memberCardInfo.setMemberCardLevel(new GoldCardMember());
}else if ((memberCardInfo.getMemberPoints() + amt.intValue())> 800 &&
(memberCardInfo.getMemberPoints() + amt.intValue() )< 1000){
memberCardInfo.setMemberCardLevel(new SilverCardMember());
}else {
memberCardInfo.setMemberPoints(memberCardInfo.getMemberPoints() + amt.intValue());
System.out.print("铜卡会员打折前金额:" + amt);
System.out.println(",铜卡会员打8折之后金额:" + amt.multiply(new BigDecimal("0.8")));
}
}
}
package behavior.StatePattern;
import java.math.BigDecimal;
/**
* 普通卡
*/
public class BasicCard extends MemberCardLevel{
@Override
public void updateMemLevel() {
}
@Override
public void doBalance(BigDecimal amt) {
if ((memberCardInfo.getMemberPoints() + amt.intValue()) > 1000) {
memberCardInfo.setMemberCardLevel(new GoldCardMember());
} else if ((memberCardInfo.getMemberPoints() + amt.intValue()) > 800 &&
(memberCardInfo.getMemberPoints() + amt.intValue()) < 1000) {
memberCardInfo.setMemberCardLevel(new SilverCardMember());
}else if((memberCardInfo.getMemberPoints() + amt.intValue()) > 500 && (memberCardInfo.getMemberPoints() + amt.intValue()) < 800){
memberCardInfo.setMemberCardLevel(new CopperCardMember());
} else {
memberCardInfo.setMemberPoints(memberCardInfo.getMemberPoints() + amt.intValue());
System.out.print("普通卡不能打卡金额:" + amt);
}
}
}
总结
允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。对象的行为依赖于它的状态(属性),并且可以根据它的状态改变而改变它的相关行为。