尚硅谷设计模式学习(22)---[状态模式(State Pattern)]
Posted 小智RE0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尚硅谷设计模式学习(22)---[状态模式(State Pattern)]相关的知识,希望对你有一定的参考价值。
尚硅谷传送门==>B站尚硅谷Java设计模式
❤❤❤感谢尚硅谷❤❤❤
最近开始计划学习一下设计模式了,加油!!!
状态模式
来解决对象在多种状态转换时,需要对外输出不同的行为的问题。状态和行为一一对应,状态之间可以相互转换.
原理图
当一个对象的内在状态改变时,允许改变其行为,这个对象看起来就像是改变了这个类.
当一个对象有很多种状态,状态之间还会相互转换,对不同的状态要求有不同的行为,可考虑用状态模式
解决一个案例
APP抽奖活动 具体要求:
- 1假如每参加一次这个活动要扣除用户50积分,中奖概率是10%
- 2奖品数量固定,抽完就不能抽奖
- 3活动有四个状态: 可以抽奖、不能抽奖、发放奖品和奖品领完
类图分析
抽奖状态类State
/**
* @author by CSDN@小智RE0
*/
//状态抽象类;
public abstract class State {
//扣除积分方法;
public abstract void deductIntegral();
//抽奖;
public abstract boolean raffle();
//发放奖品;
public abstract void offeringPrizes();
}
待抽奖状态类NotRaffleState
/**
* @author by CSDN@小智RE0
*/
//不许抽奖状态(待抽奖状态);
public class NotRaffleState extends State{
//关联抽奖活动;
private RaffleActivity raffleActivity;
//初始化;
public NotRaffleState(RaffleActivity raffleActivity) {
this.raffleActivity = raffleActivity;
}
//扣除积分;
@Override
public void deductIntegral() {
System.out.println("扣掉50分啦,可以去抽奖啦!!!");
//状态转换==>可抽奖状态;
raffleActivity.setState(raffleActivity.getRaffleState());
}
@Override
public boolean raffle() {
System.out.println("对不起,扣除积分才能参与抽奖!");
return false;
}
@Override
public void offeringPrizes() {
System.out.println("没参与抽奖,不发奖品!!!");
}
}
抽奖参与状态类RaffleState
/**
* @author by CSDN@小智RE0
*/
//抽奖状态;
public class RaffleState extends State{
//关联抽奖活动;
private RaffleActivity raffleActivity;
//初始化;
public RaffleState(RaffleActivity raffleActivity) {
this.raffleActivity = raffleActivity;
}
//扣除积分;
@Override
public void deductIntegral() {
System.out.println("已扣过积分,参与抽奖吧1");
}
@Override
public boolean raffle() {
System.out.println("要抽奖啦!----->");
Random random = new Random();
int num = random.nextInt(10);
if(num == 5){
//只有10%的中奖机会; ==> 发放奖品状态;
raffleActivity.setState(raffleActivity.getOfferPrizesState());
return true;
}else{
System.out.println("没中奖啊,再接再厉===================>");
//跳转到不能抽奖的 状态;
raffleActivity.setState(raffleActivity.getNotRaffleState());
return false;
}
}
@Override
public void offeringPrizes() {
System.out.println("没中奖,不给发奖品");
}
}
领奖状态类OfferPrizesState
/**
* @author by CSDN@小智RE0
*/
//发放奖品状态;
public class OfferPrizesState extends State {
//关联抽奖活动;
private RaffleActivity raffleActivity;
//初始化;
public OfferPrizesState(RaffleActivity raffleActivity) {
this.raffleActivity = raffleActivity;
}
//扣积分方法;
@Override
public void deductIntegral() {
System.out.println("发奖品呢,扣什么积分啊!!!");
}
//抽奖发放
@Override
public boolean raffle() {
System.out.println("您已经在领奖了,还抽奖干嘛!");
return false;
}
@Override
public void offeringPrizes() {
if(raffleActivity.getCount()>0){
System.out.println("<==请领取您的奖品==>ლ(´ڡ`ლ)好吃的.");
//跳转到不能抽奖状态;
raffleActivity.setState(raffleActivity.getNotRaffleState());
}else {
System.out.println("o((⊙﹏⊙))o抱歉,您来晚了,没奖品了,要不您在试试别的活动!");
//跳转到没有奖品状态;
raffleActivity.setState(raffleActivity.getNoPrizesState());
}
}
}
奖品发放结束状态类NoPrizesState
/**
* @author by CSDN@小智RE0
*/
//奖品发完 状态;
public class NoPrizesState extends State{
//关联抽奖活动;
private RaffleActivity raffleActivity;
//初始化;
public NoPrizesState(RaffleActivity raffleActivity) {
this.raffleActivity = raffleActivity;
}
//扣除积分发放;
@Override
public void deductIntegral() {
System.out.println("奖品没了,就不扣您的积分了");
}
@Override
public boolean raffle() {
System.out.println("奖品都没了,您还抽什么奖啊!");
return false;
}
@Override
public void offeringPrizes() {
System.out.println("奖品都没了,不用来领奖了!");
}
}
抽奖活动类RaffleActivity
/**
* @author by CSDN@小智RE0
*/
//抽奖活动;
public class RaffleActivity {
//抽奖状态;
State state=null;
//奖品数量;
int count=0;
//初始化;定义奖品数量;不能抽奖;
public RaffleActivity(int count) {
System.out.println("快来玩啊,抽奖活动开始啦");
this.state = getNotRaffleState();
this.count = count;
}
//不能抽奖状态;
State notRaffleState = new NotRaffleState(this);
//可抽奖状态;
State raffleState = new RaffleState(this);
//发放奖品状态;
State offerPrizesState = new OfferPrizesState(this);
//奖品发放结束状态;
State noPrizesState = new NoPrizesState(this);
//扣除积分;
public void deductIntegral(){
state.deductIntegral();
}
//抽奖;
public void raffle(){
if(state.raffle()){
//抽奖了就做好发奖品的准备吧,
state.offeringPrizes();
}
}
//getter ,setter方法;
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public int getCount() {
return count--;
}
public void setCount(int count) {
this.count = count;
}
public State getNotRaffleState() {
return notRaffleState;
}
public void setNotRaffleState(State notRaffleState) {
this.notRaffleState = notRaffleState;
}
public State getRaffleState() {
return raffleState;
}
public void setRaffleState(State raffleState) {
this.raffleState = raffleState;
}
public State getOfferPrizesState() {
return offerPrizesState;
}
public void setOfferPrizesState(State offerPrizesState) {
this.offerPrizesState = offerPrizesState;
}
public State getNoPrizesState() {
return noPrizesState;
}
public void setNoPrizesState(State noPrizesState) {
this.noPrizesState = noPrizesState;
}
}
模拟客户端
/**
* @author by CSDN@小智RE0
*/
public class Client {
public static void main(String[] args) {
//开始活动;
RaffleActivity raffleActivity = new RaffleActivity(1);
//抽奖10次;
for (int i = 0; i < 10; i++) {
System.out.println("第"+(i+1)+"次抽奖--->");
//扣积分;抽奖
raffleActivity.deductIntegral();
raffleActivity.raffle();
}
}
}
以上是关于尚硅谷设计模式学习(22)---[状态模式(State Pattern)]的主要内容,如果未能解决你的问题,请参考以下文章