Java方法流程状态设计

标签 java design-patterns

您好,我的代码将遵循顺序过程。

每个可能的部分都会出现失败情况。

我想知道进程被哪一部分阻止了。 所以有一个状态可以知道进程在哪个方法上失败了。

public class MethodSequenceDesign {
    public static final int STATE_WAIT = 0;
    public static final int STATE_A_RUNNING = 1;
    public static final int STATE_A_FAIL = 2;
    public static final int STATE_B_RUNNING = 3;
    public static final int STATE_B_FAIL = 4;
    public static final int STATE_C_RUNNING = 5;
    public static final int STATE_C_FAIL = 6;
    public static final int STATE_END = 7;

    static int status = STATE_WAIT;

    public void start() {
        if (!methodA()) {
            status = STATE_A_FAIL;
            return;
        }
        if (!methodB()) {
            status = STATE_B_FAIL;
            return;
        }
        if (!methodC()) {
            status = STATE_C_FAIL;
            return;
        }
        status = STATE_END;
    }
    public boolean methodA() {
        status = STATE_A_RUNNING;
        String str = "do some operation in method A";
        System.out.println(str);
        return randomBoolean();

    }
    public boolean methodB() {
        status = STATE_B_RUNNING;
        String str = "do some operation in method B";
        System.out.println(str);
        return randomBoolean();
    }
    public boolean methodC() {
        status = STATE_C_RUNNING;
        String str = "do some operation in method C";
        System.out.println(str);
        return randomBoolean();
    }
    public static void main(String[] args) {
        MethodSequenceDesign method = new MethodSequenceDesign();
        method.start();
        System.out.println("Final state = " + status);
    }
    public boolean randomBoolean() {
        Random random = new Random();
        return random.nextBoolean();
    }
}

我这里有我的示例代码: https://ideone.com/4Rw0Zs

我的问题是,是否有一种设计模式可以美化我的代码。

最佳答案

对于错误处理,请使用异常:

public class MethodSequenceDesign {


    public void start() {
        if (!methodA()) {
            throw new RuntimeException("methodA failed");
        }
        if (!methodB()) {
            throw new RuntimeException("methodB failed");
        }
        if (!methodC()) {
            throw new RuntimeException("methodC failed");
        }
    }
    public boolean methodA() {
        String str = "do some operation in method A";
        System.out.println(str);
        return randomBoolean();

    }
    public boolean methodB() {
        String str = "do some operation in method B";
        System.out.println(str);
        return randomBoolean();
    }
    public boolean methodC() {
        String str = "do some operation in method C";
        System.out.println(str);
        return randomBoolean();
    }
    public static void main(String[] args) {
        MethodSequenceDesign method = new MethodSequenceDesign();
        try{
            method.start();
        } catch (Exception e) {
            e.printStacktrace();
        }
    }
    public boolean randomBoolean() {
        Random random = new Random();
        return random.nextBoolean();
    }
}

关于Java方法流程状态设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49062369/

相关文章:

java - JTextfield 未在 GUI 中动态更新

java - 后台线程安卓

java - 用于从中央 Controller 委派任务的调度程序模式

javascript - 如何使用 Object.create() 而不是 new 创建具有私有(private)成员的对象

PHP 面向对象 : business logic layer - DB layer

JAVA GUI - 面板上出现 "cannot find symbol"错误。 "class, interface or enum expected"上

java - 在java中从客户端向服务器发送字符串时出现问题

java - 单击页面上已存在的元素时出现 TimeoutException

design-patterns - 您是否允许 Web 层直接访问 DAL?

design-patterns - "Brokered definition set"设计模式——众所周知的另一个名字?