小编AJS*_*S49的帖子

状态模式和封装

我最近一直在开发Java应用程序,并且我一直在尝试遵循GoF的状态模式来尝试整理代码.

该程序使用多代理系统的代理来代表"超级代理"评估指令(下面的示例).

超级代理可以以两种状态存在,并且如果语句到处检查状态然后执行特定于状态的行为,则会变得混乱.

这是该程序的(非常)简化版本.实际实现有更多的状态特定行为.

public class superAgent
{
    //the state of the super agent
    private States state;

    //Contains information related to the operation of exampleClass. This should not be exposed through mutator methods.
    private HashMap<String, SpecificInstructionData> instructionData

    private LinkedBlockingQueue<ExampleInstruction> exampleQueue

    private final Object instructionLock = new instructionLock

    public enum States
    {
        STATE1,
        STATE2;
    }

    public void setState(state s)
    {
        state = s
    }

    //Called by a thread that continuously takes from the queue 
    private void runningThread()
    {
        while(isRunning)
        {
            synchronized(instructionLock) …
Run Code Online (Sandbox Code Playgroud)

java oop state encapsulation agents

5
推荐指数
1
解决办法
854
查看次数

标签 统计

agents ×1

encapsulation ×1

java ×1

oop ×1

state ×1