状态模式(状态对象模式):将状态和行为绑定起来,允许一个对象内部状态改变的时候,改变其行为!
本质上是将行为封装在状态中。
角色:1、状态行为的抽象 2、实际状态对象 3、状态环境(将状态作为成员变量)
下面看实际代码:
状态行为抽象
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.state; /** * @description 贷款行为,实际上这个可以定义为抽象类 * @author panteng * @date 17-2-27. */ public interface ILoan { Float getLoan(Float loanAmount); }
实际状态(状态对应行为的实现)
名字命名不太恰当,实际上这个类并不是person,而是person的状态!
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.state; /** * @description * @author panteng * @date 17-2-27. */ public class GeneralPerson implements ILoan { String stat = "普通人员"; public Float getLoan(Float loanAmount){ System.out.println("排队... ..."); if (loanAmount > 10000) { System.out.println("贷款金额太大,不予审批"); } else { System.out.println("审批中... ..."); System.out.println("放款... ..."); return loanAmount; } return 0F; } }
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.state; /** * @description * @author panteng * @date 17-2-27. */ public class VIPPerson implements ILoan { String stat = "VIP 人员"; public Float getLoan(Float loanAmount){ System.out.println("走VIP通道... ..."); if (loanAmount > 100000) { System.out.println("审批中... ..."); if (loanAmount > 500000) { System.out.println("贷款金额太大,不予审批"); } else { System.out.println("审批通过"); System.out.println("放款... ..."); } } else { System.out.println("放款... ..."); return loanAmount; } return 0F; } }
状态环境
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.state; /** * @description 相当于状态的环境 * @author panteng * @date 17-2-27. */ public class Person { String name; String id; String phoneNum; ILoan state; //用户状态 public Person(){ } public Person(String name, String id, String phoneNum, ILoan state){ this.name = name; this.id = id; this.phoneNum = phoneNum; this.state = state; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getId(){ return id; } public void setId(String id){ this.id = id; } public String getPhoneNum(){ return phoneNum; } public void setPhoneNum(String phoneNum){ this.phoneNum = phoneNum; } public ILoan getState(){ return state; } public void setState(ILoan state){ this.state = state; } public void loanAction(Float amount){ this.state.getLoan(amount); } }
测试
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.state; import org.junit.Test; /** * @description * @author panteng * @date 17-2-27. */ public class StateTest { @Test public void stateTest(){ ILoan state1 = new GeneralPerson(); Person person = new Person("pt", "10001010112", "13840410382", state1); //普通状态 person.loanAction(1000F); System.out.println("========================================="); person.loanAction(100001F); //切换到VIP状态 ILoan state2 = new VIPPerson(); person.setState(state2); System.out.println("========================================="); person.loanAction(1000F); System.out.println("========================================="); person.loanAction(100001F); } }
===========================设计模式系列文章=========================