zoukankan      html  css  js  c++  java
  • 22状态模式State

    一、什么是状态模式

      State模式也叫状态模式,是行为设计模式的 一种。State模式允许通过改变对象的内部状态 而改变对象的行为,这个对象表现得就好像修改 了它的类一样。

    二、状态模式的应用场景

      状态模式主要解决的是当控制一个对象状态转 换的条件表达式过于复杂时的情况。把状态的判 断逻辑转译到表现不同状态的一系列类当中,可 以把复杂的判断逻辑简化。

    三、状态模式的结构

    四、状态模式的角色和职责

      Context:用户对象 拥有一个State类型的成员,以标识对象的当前 状态;

      State:接口或基类 封装与Context的特定状态相关的行为;

      ConcreteState:接口实现类或子类 实现了一个与Context某个状态相关的行为。

     1 public class Person {
     2     private int hour;
     3 
     4     public int getHour() {
     5         return hour;
     6     }
     7 
     8     public void setHour(int hour) {
     9         this.hour = hour;
    10     }
    11     
    12     public void doSomething(){
    13         if(hour == 7) {
    14             System.out.println("吃早餐");
    15         } else if(hour == 12) {
    16             System.out.println("吃中饭");
    17         } else if(hour == 18) {
    18             System.out.println("吃晚饭");
    19         } else {
    20             System.out.println(hour + "未定义");
    21         }
    22     }
    23 }
     1 public class MainClass {
     2     public static void main(String[] args) {
     3         Person person = new Person();
     4         
     5         person.setHour(7);
     6         person.doSomething();
     7         
     8         person.setHour(12);
     9         person.doSomething();
    10         
    11         person.setHour(18);
    12         person.doSomething();
    13         
    14         person.setHour(8);
    15         person.doSomething();
    16     }
    17 }

    =================================================================

    使用状态模式

     1 public class Person {
     2     private int hour;
     3     private State state;
     4 
     5     public int getHour() {
     6         return hour;
     7     }
     8 
     9     public void setHour(int hour) {
    10         this.hour = hour;
    11     }
    12     
    13     public void doSomething(){
    14         if(hour == 7) {
    15             state = new MState();
    16             state.doSomething();
    17         } else if(hour == 12) {
    18             state = new LState();
    19             state.doSomething();
    20         } else if(hour == 18) {
    21             state = new SState();
    22             state.doSomething();
    23         } else {
    24             state = new NoState();
    25             state.doSomething();
    26         }
    27     }
    28 }

    状态  抽象

    1 public abstract class State {
    2     public abstract void  doSomething();
    3 }

    吃早餐

    1 public class MState extends State {
    2 
    3     public void doSomething() {
    4         System.out.println("吃早餐");
    5     }
    6 }

    吃中午饭

    1 public class LState extends State{
    2 
    3     public void doSomething() {
    4         System.out.println("吃中饭");
    5     }
    6 }

    吃晚饭

    1 public class SState extends State {
    2 
    3     public void doSomething() {
    4         System.out.println("吃晚饭");
    5     }
    6 }

    未定义

    1 public class NoState extends State {
    2 
    3     public void doSomething() {
    4         System.out.println("未定义");
    5     }
    6 }

    测试

     1 public class MainClass {
     2     public static void main(String[] args) {
     3         Person person = new Person();
     4         
     5         person.setHour(7);
     6         person.doSomething();
     7         
     8         person.setHour(12);
     9         person.doSomething();
    10         
    11         person.setHour(18);
    12         person.doSomething();
    13         
    14         person.setHour(8);
    15         person.doSomething();
    16     }
    17 }
  • 相关阅读:
    web10 动态action的应用
    web09 struts2配置 struts2入门
    web 08 struts2入门 struts2配置 struts包
    web07-jdbcBookStore
    web06-PanduanLogin
    web05-CounterServlet
    web04-LoginServlet
    web03-OutputInfo
    web02-welcomeyou
    web01-helloworld
  • 原文地址:https://www.cnblogs.com/justdoitba/p/9034954.html
Copyright © 2011-2022 走看看