Saturday, February 21, 2009

Sample of Java Pattern -- State

OrderState.java
package state;

public interface OrderState {
public void handleOrder(String orderNo);
public void cancelOrder(String orderNo);
public void completeOrder(String orderNo);
}



OrderOpendState.java
package state;

public class OrderOpendState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderOpendState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("handle this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderInProgressState());
}

public void cancelOrder(String orderNo){
System.out.println("cancel this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderCancelledState());
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under opend status, orderNo=" + orderNo);
}
}



OrderInProgressState.java
package state;

public class OrderInProgressState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderInProgressState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under in progress status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("cancel this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderCancelledState());
}

public void completeOrder(String orderNo){
System.out.println("complete this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderClosedState());
}
}



OrderCancelledState.java
package state;

public class OrderCancelledState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderCancelledState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under cancelled status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("could not cancel this order under cancelled status, orderNo=" + orderNo);
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under cancelled status, orderNo=" + orderNo);
}
}



OrderClosedState.java
package state;

public class OrderClosedState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderClosedState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under closed status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("could not cancel this order under closed status, orderNo=" + orderNo);
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under closed status, orderNo=" + orderNo);
}
}



OrderStateMgmt.java
package state;

public class OrderStateMgmt {

private OrderOpendState orderOpendState;
private OrderInProgressState orderInProgressState;
private OrderCancelledState orderCancelledState;
private OrderClosedState orderClosedState;

private OrderState orderState;

public OrderStateMgmt(){
this.orderOpendState = new OrderOpendState(this);
this.orderInProgressState = new OrderInProgressState(this);
this.orderCancelledState = new OrderCancelledState(this);
this.orderClosedState = new OrderClosedState(this);
this.orderState = this.orderOpendState;
System.out.println("The order inital status is: opend.");
}

public void setState(OrderState orderState){
this.orderState = orderState;
}

public OrderOpendState getOrderOpendState() {
return orderOpendState;
}

public OrderInProgressState getOrderInProgressState() {
return orderInProgressState;
}

public OrderCancelledState getOrderCancelledState() {
return orderCancelledState;
}

public OrderClosedState getOrderClosedState() {
return orderClosedState;
}

public void handleOrder(String orderNo){
orderState.handleOrder(orderNo);
}

public void cancelOrder(String orderNo){
orderState.cancelOrder(orderNo);
}

public void completeOrder(String orderNo){
orderState.completeOrder(orderNo);
}
}



OrderStateTest.java
package state;

public class OrderStateTest {

public static void main(String[] args) {
OrderStateMgmt orderStateMgmtA = new OrderStateMgmt();

orderStateMgmtA.handleOrder("01");
orderStateMgmtA.completeOrder("01");

OrderStateMgmt orderStateMgmtB = new OrderStateMgmt();

orderStateMgmtB.handleOrder("02");
orderStateMgmtB.cancelOrder("02");

OrderStateMgmt orderStateMgmtC = new OrderStateMgmt();

orderStateMgmtC.completeOrder("03");
}

}



you may download the source code.

No comments:

Post a Comment