My name is Zemian Deng, and I am a Senior Application Engineer working at Oracle for the Enterprise Knowledge Management product. NOTE: The views expressed on my blog and social network are my own and do not necessarily reflect the views of my employer. Zemian is a DZone MVB and is not an employee of DZone and has posted 77 posts at DZone. You can read more from them at their website. View Full User Profile

How to Bring Pseudo Code to Life

05.05.2013
| 3019 views |
  • submit to reddit

When designing a piece of software, I like to start with some pseudo code first, then work my way out. I tend to try mapping out the normal workflow (one that runs without any corner cases) with high level of actions as series of steps. Then the detailed implementation of the code can be filled in at later time.

Now with Java, you can easily do this with pseudo code, bring it to life, and still make it very maintainable. Try this out.

package deng.javademo;

/**
 * @author Zemian Deng
 */
public class PseudoCodeToLife {
    public static void main(String[] args) {
        CreditCardProcessor creditCardProcessor = new CreditCardProcessor()
                .step(webServiceRequest())
                .step(validateCardNumber())
                .step(validateSecurityCode())
                .step(checkAvailableCredit())
                .step(chargeCard())
                .step(webServiceResponse());

        creditCardProcessor.run();
    }
}

Now that's as high level as you can get, but it still let me fill in the implementation without losing the overall requirement flow. To implement such workflow, we need a contract that allow processor to move data from one step to another, and yet it needs to perform some action along the way. How about an interface like this.

package deng.javademo;

/**
 * @author Zemian Deng
 */
public interface StepAction {
    public void onAction(Exchange exchange);
}

Next is how to wire steps together. For now we can just do it inside CreditCardProcessor, but you can easily abstract that into a re-usable base class.

package deng.javademo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zemian Deng
 */
public class CreditCardProcessor {
    List<StepAction> steps = new ArrayList<StepAction>();

    public CreditCardProcessor step(StepAction stepAction) {
        steps.add(stepAction);
        return this;
    }

    public void run() {
        Exchange exchange = new Exchange();
        for (StepAction step : steps) {
            step.onAction(exchange);
        }
    }
}

Pretty straight forward. Here I used an Exchange as message, passing between each steps to allow the workflow to exchange data. You can wrap just about any real payload and/or add a Map of header properties.

Now what we are missing is just filling in each step of actions. Since we have an explicit step name when defining the workflow, we can just generate these method and fill in the actions per step required. I will add them all inside the PseudoCodeToLife and reprint it here.

package deng.javademo;

/**
 * @author Zemian Deng
 */
public class PseudoCodeToLife {
    public static void main(String[] args) {
        CreditCardProcessor creditCardProcessor = new CreditCardProcessor()
                .step(webServiceRequest())
                .step(validateCardNumber())
                .step(validateSecurityCode())
                .step(checkAvailableCredit())
                .step(chargeCard())
                .step(webServiceResponse());

        creditCardProcessor.run();
    }

    private static StepAction webServiceRequest() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("webServiceRequest step.");
            }
        };
    }

    private static StepAction validateCardNumber() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("validateCardNumber step.");
            }
        };
    }

    private static StepAction validateSecurityCode() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("validateSecurityCode step.");
            }
        };
    }

    private static StepAction checkAvailableCredit() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("checkAvailableCredit step.");
            }
        };
    }

    private static StepAction chargeCard() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("chargeCard step.");
            }
        };
    }

    private static StepAction webServiceResponse() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("webServiceResponse step.");
            }
        };
    }
}


When designing a piece of software, I like to start with some pseudo code first, then work my way out.

That's all to this. What do you think? I think it's a simple way to tackle business problems. Simple software make less bugs and easier to maintain.

Published at DZone with permission of Zemian Deng, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Alessandro Puzielli replied on Sun, 2013/05/05 - 2:23pm

The snippets remember me the Strategy design pattern..

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.