• 【设计模式】-行为型-5-状态模式


    主要角色

    1. 环境(Context)角色:也称为上下文,它定义了客户感兴趣的接口,维护一个当前状态,并将与状态相关的操作委托给当前状态对象来处理。
    2. 抽象状态(State)角色:定义一个接口,用以封装环境对象中的特定状态所对应的行为。
    3. 具体状态(Concrete State)角色:实现抽象状态所对应的行为。

    代码示例

    package state;
    public class StatePatternClient
    {
        public static void main(String[] args)
        {       
            Context context=new Context();    //创建环境       
            context.Handle();    //处理请求
            context.Handle();
            context.Handle();
            context.Handle();
        }
    }
    //环境类
    class Context
    {
        private State state;
        //定义环境类的初始状态
        public Context()
        {
            this.state=new ConcreteStateA();
        }
        //设置新状态
        public void setState(State state)
        {
            this.state=state;
        }
        //读取状态
        public State getState()
        {
            return(state);
        }
        //对请求做处理
        public void Handle()
        {
            state.Handle(this);
        }
    }
    //抽象状态类
    abstract class State
    {
        public abstract void Handle(Context context);
    }
    //具体状态A类
    class ConcreteStateA extends State
    {
        public void Handle(Context context)
        {
            System.out.println("当前状态是 A.");
            context.setState(new ConcreteStateB());
        }
    }
    //具体状态B类
    class ConcreteStateB extends State
    {
        public void Handle(Context context)
        {
            System.out.println("当前状态是 B.");
            context.setState(new ConcreteStateA());
        }
    }
    

    以多线程状态处理为例

    package state;
    public class ThreadStateTest
    {
        public static void main(String[] args)
        {
            ThreadContext context=new ThreadContext();
            context.start();
            context.getCPU();
            context.suspend();
            context.resume();
            context.getCPU();
            context.stop();
        }
    }
    //环境类
    class ThreadContext
    {
        private ThreadState state;
        ThreadContext()
        {
            state=new New();
        }
        public void setState(ThreadState state)
        {
            this.state=state;
        }
        public ThreadState getState()
        {
            return state;
        }   
        public void start()
        {
            ((New) state).start(this);
        }
        public void getCPU()
        {
            ((Runnable) state).getCPU(this);
        }
        public void suspend()
        {
            ((Running) state).suspend(this);
        }
        public void stop()
        {
            ((Running) state).stop(this);
        }
        public void resume()
        {
            ((Blocked) state).resume(this);
        }
    }
    //抽象状态类:线程状态
    abstract class ThreadState
    {
        protected String stateName; //状态名
    }
    //具体状态类:新建状态
    class New extends ThreadState
    {
        public New()
        {       
            stateName="新建状态";
            System.out.println("当前线程处于:新建状态.");   
        }
        public void start(ThreadContext hj)
        {
            System.out.print("调用start()方法-->");
            if(stateName.equals("新建状态"))
            {
                hj.setState(new Runnable());
            }
            else
            {
                System.out.println("当前线程不是新建状态,不能调用start()方法.");
            }
        }   
    }
    //具体状态类:就绪状态
    class Runnable extends ThreadState
    {
        public Runnable()
        {       
            stateName="就绪状态";
            System.out.println("当前线程处于:就绪状态.");   
        }
        public void getCPU(ThreadContext hj)
        {
            System.out.print("获得CPU时间-->");
            if(stateName.equals("就绪状态"))
            {
                hj.setState(new Running());
            }
            else
            {
                System.out.println("当前线程不是就绪状态,不能获取CPU.");
            }
        }   
    }
    //具体状态类:运行状态
    class Running extends ThreadState
    {
        public Running()
        {       
            stateName="运行状态";
            System.out.println("当前线程处于:运行状态.");   
        }
        public void suspend(ThreadContext hj)
        {
            System.out.print("调用suspend()方法-->");
            if(stateName.equals("运行状态"))
            {
                hj.setState(new Blocked());
            }
            else
            {
                System.out.println("当前线程不是运行状态,不能调用suspend()方法.");
            }
        }
        public void stop(ThreadContext hj)
        {
            System.out.print("调用stop()方法-->");
            if(stateName.equals("运行状态"))
            {
                hj.setState(new Dead());
            }
            else
            {
                System.out.println("当前线程不是运行状态,不能调用stop()方法.");
            }
        }
    }
    //具体状态类:阻塞状态
    class Blocked extends ThreadState
    {
        public Blocked()
        {       
            stateName="阻塞状态";
            System.out.println("当前线程处于:阻塞状态.");   
        }
        public void resume(ThreadContext hj)
        {
            System.out.print("调用resume()方法-->");
            if(stateName.equals("阻塞状态"))
            {
                hj.setState(new Runnable());
            }
            else
            {
                System.out.println("当前线程不是阻塞状态,不能调用resume()方法.");
            }
        }   
    }
    //具体状态类:死亡状态
    class Dead extends ThreadState
    {
        public Dead()
        {
            stateName="死亡状态";
            System.out.println("当前线程处于:死亡状态.");   
        }   
    }
    
  • 相关阅读:
    usaco PROB Checker Challenge 搜索
    usaco Superprime Rib 搜索
    hdu_1056_HangOver_201311071354
    hdu_1049_Climbing Worm_201311061331
    hdu_1048_The Hardest Problem Ever_201311052052
    hdu_1041_Computer Transformation_201311051648
    hdu_1039_Easier Done Than Said_201311051511
    hdu_1038_Biker's Trip Odometer_201311021643
    hdu_1037_Keep on Truckin'_201311021600
    hdu_1036_Average is not Fast Enough_201311021335
  • 原文地址:https://www.cnblogs.com/tuofan/p/12377130.html
Copyright © 2020-2023  润新知