状态模式

public abstract class AccountState {
    double balance;
    protected Account  account;
    public abstract void qukuan(double a);
    public abstract void cunkuan(double a);
    public abstract void check();
}
public class Yellow extends AccountState {
    public Yellow(AccountState accountState) {
        this.balance = accountState.balance;
        this.account = accountState.account;
    }

    public Yellow(double d, Account account) {
        this.account = account;
        this.balance = d;
    }

    @Override
    public void qukuan(double a) {
        balance -= a;
        check();
    }

    @Override
    public void cunkuan(double a) {
        balance += a;
        check();
    }

    @Override
    public void check() {
        if (balance<-1000) {
            account.setstate(new Red(this));
            
        }else if (balance>=0) {
            account.setstate(new Green(this));      
        }
    }
}

public class Green extends AccountState{
    public Green(AccountState accountState) {
    this.balance=accountState.balance;
    this.account=accountState.account;
    }
    public Green(double d, Account account) {
        this.account=account;
        this.balance=d;
        }
    
    @Override
    public void qukuan(double a) {
        balance-=a;
        check();
    }

    @Override
    public void cunkuan(double a) {
        balance+=a;
        check();
    }

    @Override
    public void check() {
        if (balance<-1000) {
            account.setstate(new Red(this));
            
        }else if (balance<0) {
            account.setstate(new Yellow(this));
            
        }
    }

}
public class Red extends AccountState{
    public Red(AccountState accountState) {
        this.balance=accountState.balance;
        this.account=accountState.account;
        }
        public Red(double d, Account account) {
            this.account=account;
            this.balance=d;
            }
    @Override
    public void qukuan(double a) {
    System.out.println("您为红色状态,禁止取款");
    }

    @Override
    public void cunkuan(double a) {
        balance+=a;
        check();
    }

    @Override
    public void check() {
        if (balance>=0) {
            account.setstate(new Green(this));
            
        }else if (balance>-1000) {
            account.setstate(new Yellow(this));
            
        }
    }
}

public class Yellow extends AccountState {
    public Yellow(AccountState accountState) {
        this.balance = accountState.balance;
        this.account = accountState.account;
    }

    public Yellow(double d, Account account) {
        this.account = account;
        this.balance = d;
    }

    @Override
    public void qukuan(double a) {
        balance -= a;
        check();
    }

    @Override
    public void cunkuan(double a) {
        balance += a;
        check();
    }

    @Override
    public void check() {
        if (balance<-1000) {
            account.setstate(new Red(this));
            
        }else if (balance>=0) {
            account.setstate(new Green(this));
            
        }
    }

}
public class Account {
    /**用户名*/
    private String owner;
    /**账户操作类-含账号信息以及账号操作*/
    private AccountState state;

    public void setstate(AccountState state) {
        this.state = state;
    }

    public Account(String string, Double d) {
        this.owner = string;
        this.state = new Green(d, this);
        System.out.println(owner + ":已开户       账号余额为" + state.balance);
        System.out.println("其账户状态为:" + state.getClass().getName());
        System.out.println("——————————————————————————————————————————————————————————");
    }

    public void quqian(double d) {
        state.qukuan(d);
        System.out.println("取款:" + d + "  账户余额为:" + state.balance);
        System.out.println("其账户状态为:" + state.getClass().getName());
        System.out.println("——————————————————————————————————————————————————————————");
    }
    public void cunqian(double d) {
        state.cunkuan(d);
        System.out.println("存款:" + d + "  账户余额为:" + state.balance);
        System.out.println("其账户状态为:" + state.getClass().getName());
        System.out.println("——————————————————————————————————————————————————————————");
    }
}
public class Client {
    public static void main(String[] args) {
        Account account=new Account("皇上", 5.0);
        account.quqian(5200);
        account.quqian(200);
        account.cunqian(5000);
        account.cunqian(200.0);
    }
}

状态模式和策略模式的主要区别(个人理解)

  • 策略模式:策略模式的客户端必须对所有的策略类相当了解,我们会对固有的一个状态直接选择一个策略进行操作
  • 状态模式:状态模式,状态是流转的,其状态的变化会引起其内部的行为发生变化.通常用于一次请求会由多次操作,行为可能变更的情况;

你可能感兴趣的:(状态模式)