7. State design pattern

7. State design pattern

1. What is the state mode?

State Pattern: It is mainly used to solve the problem that objects need to output different behaviors when they are in a variety of state transitions. There is a one-to-one correspondence between states and behaviors, and the states can be converted to each other. When the internal state of an object changes, it is allowed to change its behavior, and the object looks like it has changed its class. For example: Taobao order, order pending payment, payment pending delivery, pending receipt, pending evaluation, completed and other statuses. The behavior corresponding to each state is different, and when one state is completed, it will flow to the next state.

We usually program stateful objects. Our solution is to think about all the possible states, then use if-else or switch-case statements to make state judgments, and then perform different treatments according to different states. Such as the above case-Taobao order:

public class ClientApplication { public static void main (String[] args) { String status = "Pending payment" ; if ( "Pending payment" .equals(status)) { //Execute payment logic } else if ( "Pending shipment" .equals(status)) { //Execute delivery logic } else if ( "pending receipt" .equals(status)) { //execute receiving logic } else if ( "pending evaluation" .equals(status)) { //execute evaluation logic } else if ( "pending receipt" . equals(status)) { //execute harvest logic } } } Copy code

The disadvantages of a large number of if...else are obvious

  1. Violation of the principle of opening and closing: When adding a state, the original logic needs to be modified
  2. When there are many states, the code segment is very long, bloated, not easy to maintain, and poor scalability.

State mode can solve this problem well. The idea of state mode : When the conditional statement indicates that the state transition of an object is too complicated, the "judgment logic" in the conditional judgment can be extracted and placed in a separate class. The current context is in that state, and the corresponding state is used directly Class objects are processed. The advantage of this is that the original complex judgment logic can be simplified, and conditional judgment statements such as if-else and switch-case can be eliminated. The code is more hierarchical, and it has good scalability and maintainability. .

2. The structure of the state pattern

The state mode packs the object behavior that changes due to environmental changes in different state classes, and its purpose is to ensure that when the state of the object changes, its behavior also changes. Next, let's take a look at the structure of the state pattern.

From this we can see that there are four components:

1. Environmental

The environment class is also a context class. The change of state depends on the environment, and the environment records the current state. The environment class has objects with multiple states. The state of the environment class is diverse, and the behavior of objects in different states is different, so the states are independent Go out to form a separate state class.

> 1) Maintain an instance of the abstract state State in the environment class, which stores the current state. > 2) Define all state execution methods in the environment class. Copy code

package com.lxl.www.designPatterns.statePattern.context; /** * Context class */ public class Context { //Maintain an instance of the abstract state State in the environment class, which stores the current state. private State state; public Context () { this .state = new ConcreteStateA( this ); } public void setState (State state) { this .state = state; } /** * Define all state execution methods in the environment class. */ public void handler1 () { this .state.handler1(); } public void handler2 () { this .state.handler2(); } } Copy code

2. State abstract state class

Declare an environmental role in the abstract environment, provide self-access for each state class, and provide abstract behaviors of all states, which are implemented by each implementation class.

/** * State abstract class */ public abstract class State { /** * Environmental context */ public Context context; public State (Context context) { this .context = context; } /** * Define abstract methods for all states */ abstract void handler1 () ; abstract void handler2 () ; } Copy code

3. Specific status

Specific state realization, here to define three specific state classes ConcreteStateA, ConcreteStateB, ConcreteStateC. The concrete state class inherits from the State abstract class and implements abstract methods.

package com.lxl.www.designPatterns.statePattern.context; public class ConcreteStateA extends State { public ConcreteStateA (Context context) { super (context); } @Override void handler1 () { System.out.println( "Execute the logic of handler1 in ConcreteStateA" ); this .context.setState( new ConcreteStateB(context)); } @Override void handler2 () { System.out.println( "Execute the logic of handler2 in ConcreteStateA" ); this .context.setState( new ConcreteStateC(context)); } } Copy code

Here, we have overridden the state method of the state class. After executing the business logic of method 1, change the state to ConcreteStateB. After executing the business logic of handler2, change the state to ConcreteStateC. This kind of automatic change state is unaware of the caller.

4. Client

Construct an instance object of the context class of the Context environment, set the initial state to ConcreteStateA during initialization, and execute the behavior observation result.

package com.lxl.www.designPatterns.statePattern.context; public class Client { public static void main (String[] args) { Context context = new Context(); context.handler1(); context.handler1(); context.handler1(); context.handler2(); } } Copy code

All source code:

package com.lxl.www.designPatterns.statePattern.context; /** * Context class */ public class Context { //Maintain an instance of the abstract state State in the environment class, which stores the current state. private State state; public Context () { this .state = new ConcreteStateA( this ); } public void setState (State state) { this .state = state; } /** * Define all state execution methods in the environment class. */ public void handler1 () { this .state.handler1(); } public void handler2 () { this .state.handler2(); } } /** * State abstract class */ public abstract class State { /** * Environmental context */ public Context context; public State (Context context) { this .context = context; } /** * Define abstract methods for all states */ abstract void handler1 () ; abstract void handler2 () ; } public class ConcreteStateA extends State { public ConcreteStateA (Context context) { super (context); } @Override void handler1 () { System.out.println( "Execute the logic of handler1 in ConcreteStateA" ); this .context.setState( new ConcreteStateB(context)); } @Override void handler2 () { System.out.println( "Execute the logic of handler2 in ConcreteStateA" ); this .context.setState( new ConcreteStateC(context)); } } public class ConcreteStateB extends State { public ConcreteStateB (Context context) { super (context); } @Override void handler1 () { System.out.println( "Execute the logic of handler1 in ConcreteStateB" ); this .context.setState( new ConcreteStateC(context)); } @Override void handler2 () { System.out.println( "Execute the logic of handler2 in ConcreteStateB" ); this .context.setState( new ConcreteStateA(context)); } } public class ConcreteStateC extends State { public ConcreteStateC (Context context) { super (context); } @Override void handler1 () { System.out.println( "Execute the logic of handler1 in ConcreteStateC" ); this .context.setState( new ConcreteStateA(context)); } @Override void handler2 () { System.out.println( "Execute the logic of handler2 in ConcreteStateC" ); this .context.setState( new ConcreteStateB(context)); } } public class Client { public static void main (String[] args) { Context context = new Context(); context.handler1(); context.handler1(); context.handler1(); context.handler1(); } } Copy code

Client-side direct result analysis:

  1. When the Context instance is spent, the state is ConcreteStateA
  2. Execute context.handler1() for the first time; switch the state to ConcreteStateB
  3. Execute context.handler1() for the second time; switch the state to ConcreteStateC
  4. Execute context.handler1() for the third time; switch the state to ConcreteStateA

So, the final running result is:

Execute the logic
of handler1 in
ConcreteStateA Execute the logic of handler1 in
ConcreteStateB Execute the logic of handler1 in ConcreteStateC Execute the logic of handler1 in ConcreteStateA

3. Application examples of state mode

Let's take a product order as an example to take a look at the application of the status mode.

In the process of shopping in the e-commerce system, orders generally have these statuses, waiting to be paid, waiting to be shipped out, waiting to be received, waiting to be evaluated, and completed. In different states, the behavior that users see is different.

Let's go from the shallower to the deeper, let's take a look at the realization of several state modes.

The first type: State is the realization logic of a line

Let's take a look at the process of this method:

The characteristics of this state are: the previous state flows to the next state, the next flow goes to the next, and so on. Until the last one, no state flowed. Take a look at the source code:

The first step: Define the context environment class.

Use orderStatus to record the current status. And defines the methods of all states.

package com.lxl.www.designPatterns.statePattern.order; /** * Order context */ public class OrderContext { /**Record the order status in the context class*/ private IOrderStatus orderStatus; public OrderContext () { //At the beginning is the pending payment status this .orderStatus = new Pending(); } public void setOrderStatus (IOrderStatus orderStatus) { this .orderStatus = orderStatus; } /** * Business logic operations */ public void businessHandler () { this .orderStatus.businessHandler( this ); } /** * Print current business */ public void printInfo () { this .orderStatus.printInfo(); } } Copy code

Step 2: Define the state abstract class.

It defines the methods to be implemented by all state classes.

package com.lxl.www.designPatterns.statePattern.order; /** * Order Status */ public interface IOrderStatus { void businessHandler (OrderContext context) ; void printInfo () ; } Copy code

There are two methods here, one is the business logic to be executed by the state, and the other is to print the current state.

The third step: Define the concrete class of the state.

5.specific categories are defined here, namely: to be paid, to be shipped out, to be received, to be evaluated, and completed

package com.lxl.www.designPatterns.statePattern.order; /** * Pending payment */ public class Pending implements IOrderStatus { @Override public void businessHandler (OrderContext context) { //Execute the business, complete the payment, and enter the next state System.out.println( "payment completed" ); context.setOrderStatus( new WaitOut()); } @Override public void printInfo () { System.out.println( "Current status etc.:: Pending payment" ); } } package com.lxl.www.designPatterns.statePattern.order; /** * To be out of the library */ public class WaitOut implements IOrderStatus { @Override public void businessHandler (OrderContext context) { //Execute the business, complete the payment, and enter the next state System.out.println( "The goods have been shipped out" ); context.setOrderStatus( new WaitReceive()); } @Override public void printInfo () { System.out.println( "Current status, etc.:: to be out of the library" ); } } package com.lxl.www.designPatterns.statePattern.order; /** * To be received */ public class WaitReceive implements IOrderStatus { @Override public void businessHandler (OrderContext context) { //Execute the business, complete the payment, and enter the next state System.out.println( " Received " ); context.setOrderStatus( new OrderEvaluation()); } @Override public void printInfo () { System.out.println( "Current status, etc.:: to be received" ); } } package com.lxl.www.designPatterns.statePattern.order; /** * Order evaluation */ public class OrderEvaluation implements IOrderStatus { @Override public void businessHandler (OrderContext context) { //Execute the business, complete the payment, and enter the next state System.out.println( "Evaluated" ); context.setOrderStatus( new Finish()); } @Override public void printInfo () { System.out.println( "Current status etc.:: To be evaluated" ); } } package com.lxl.www.designPatterns.statePattern.order; /** * Order completed */ public class Finish implements IOrderStatus { @Override public void businessHandler (OrderContext context) { //Execute the business, complete the payment, and enter the next state System.out.println( "Completed work processing is completed" ); } @Override public void printInfo () { System.out.println( "Current Status:: Order Completed" ); } } Copy code

Step 4: Define the client class to simulate the process of placing an order

package com.lxl.www.designPatterns.statePattern.order; public class OrderClient { public static void main (String[] args) { OrderContext orderContext = new OrderContext(); //Start ordering System.out.println( "==========Start ordering ==========" ); orderContext.printInfo(); //Payment System.out.println( "========== Payment==========" ); orderContext.businessHandler(); orderContext.printInfo(); //Goods out of storage System.out.println( "========== Goods out of storage==========" ); orderContext.businessHandler(); orderContext.printInfo(); //Receipt System.out.println( "========== Receipt==========" ); orderContext.businessHandler(); orderContext.printInfo(); //Evaluation order System.out.println( "========== Evaluation order==========" ); orderContext.businessHandler(); orderContext.printInfo(); //Order completed System.out.println( "==========Order completed==========" ); orderContext.businessHandler(); } } Copy code

Next, let's take a look at the running effect:

==========Start ordering==========
Current status, etc.:: Pending payment
========== Payment========= =
Payment completed
Current status, etc.:: To be
shipped out ==========
Goods are out of storage ========== Goods have been shipped out
Current status, etc.:: To be received
==== ======Receipt==========Received
goods
Current status, etc.:: Pending evaluation
==========Evaluation order==========
Already evaluated
Current status::Order completed
==========Order completed==========
Completed work processing completed

The second type: with canceled payment and return refund status.

Initially, refunds can only be returned after receipt of the goods.

This method is different from the first one. There is more than one status transfer between pending payment and confirmation receipt. When there are multiple status transfers, or even more statuses, how should we deal with it?

The first step: Define the context environment class.

The environment context here defines all state methods.

package com.lxl.www.designPatterns.statePattern.order2; /** * Order context */ public class OrderContext { /**Record the order status in the context class*/ private IOrderStatus orderStatus; public OrderContext () { System.out.println( "Start shopping" ); //At the beginning is the pending payment status this .orderStatus = new Pending(); } public void setOrderStatus (IOrderStatus orderStatus) { this .orderStatus = orderStatus; } /** * payment completed */ public void pending () { this .orderStatus.pending( this ); } public void waitOut () { this .orderStatus.waitOut( this ); } public void waitReceive () { this .orderStatus.waitReceive( this ); } public void confirmReceived () { this .orderStatus.confirmReceived( this ); } public void orderEvaluation () { this .orderStatus.orderEvaluation( this ); } public void finish () { this .orderStatus.finish( this ); } public void cancelPay () { this .orderStatus.cancelPay( this ); } public void refunds () { this .orderStatus.refunds( this ); } } Copy code

Step 2: Define the abstract state class

package com.lxl.www.designPatterns.statePattern.order2; /** * Order Status */ public interface IOrderStatus { /* * Pending payment */ void pending (OrderContext context) ; /* * Cancel payment */ void cancelPay (OrderContext context) ; /* * To be out of the library */ void waitOut (OrderContext context) ; /* * Refunds */ void refunds (OrderContext context) ; /* * To be received */ void waitReceive (OrderContext context) ; /* * Confirm receipt */ void confirmReceived (OrderContext context) ; /* * Order evaluation */ void orderEvaluation (OrderContext context) ; /* * Order completed */ void finish (OrderContext context) ; } Copy code

Step 3: Define the specific status class.

Here we define a total of 8 statuses: pending payment, pending delivery, pending receipt, confirmed receipt, order evaluation, order completion, payment cancellation, and refund.

Pending payment

package com.lxl.www.designPatterns.statePattern.order2; /** * Pending payment */ public class Pending implements IOrderStatus { public Pending () { System.out.println( "Current Status:: Pending Payment" ); } @Override public void pending (OrderContext context) { System.out.println( "Payment is completed, to be out of the library" ); context.setOrderStatus( new WaitOut()); } /* * Cancel payment */ public void cancelPay (OrderContext context) { System.out.println( "Cancel payment" ); context.setOrderStatus( new Finish()); } @Override public void waitOut (OrderContext context) { System.out.println( "Go to pay-->Payment is completed, to be out of the library" ); context.setOrderStatus( new WaitOut()); } /* * Refunds */ public void refunds (OrderContext context) { } @Override public void waitReceive (OrderContext context) { } @Override public void confirmReceived (OrderContext context) { } @Override public void orderEvaluation (OrderContext context) { } @Override public void finish (OrderContext context) { } } Copy code

To be out of the library

package com.lxl.www.designPatterns.statePattern.order2; /** * To be out of the library */ public class WaitOut implements IOrderStatus { public WaitOut () { System.out.println( "Current Status:: To be Out of Library" ); } @Override public void pending (OrderContext context) { } @Override public void cancelPay (OrderContext context) { } /* * Refunds */ public void refunds (OrderContext context) { System.out.println( "Application for Return" ); context.setOrderStatus( new Refunds()); } @Override public void waitOut (OrderContext context) { System.out.println( "Outbound complete, to be received" ); context.setOrderStatus( new WaitReceive()); } @Override public void waitReceive (OrderContext context) { } @Override public void confirmReceived (OrderContext context) { } @Override public void orderEvaluation (OrderContext context) { } @Override public void finish (OrderContext context) { } } Copy code

To be received

package com.lxl.www.designPatterns.statePattern.order2; /** * To be received */ public class WaitReceive implements IOrderStatus { public WaitReceive () { System.out.println( "Current Status:: Pending Goods" ); } @Override public void pending (OrderContext context) { } @Override public void cancelPay (OrderContext context) { } @Override public void waitOut (OrderContext context) { } /* * Refunds */ public void refunds (OrderContext context) { System.out.println( "Application for Return" ); context.setOrderStatus( new Refunds()); } @Override public void waitReceive (OrderContext context) { System.out.println( "Receive the goods and complete the receiving action" ); context.setOrderStatus( new Confirm()); } @Override public void confirmReceived (OrderContext context) { } @Override public void orderEvaluation (OrderContext context) { } @Override public void finish (OrderContext context) { } } Copy code

Confirm receipt

package com.lxl.www.designPatterns.statePattern.order2; public class Confirm implements IOrderStatus { public Confirm () { System.out.println( "Current Status:: Confirm Receipt" ); } @Override public void pending (OrderContext context) { } @Override public void cancelPay (OrderContext context) { } @Override public void waitOut (OrderContext context) { } @Override public void refunds (OrderContext context) { System.out.println( "Request a refund" ); context.setOrderStatus( new Refunds()); } @Override public void waitReceive (OrderContext context) { } @Override public void confirmReceived (OrderContext context) { System.out.println( "Confirm receipt" ); context.setOrderStatus( new OrderEvaluation()); } @Override public void orderEvaluation (OrderContext context) { } @Override public void finish (OrderContext context) { } } Copy code

Order evaluation

package com.lxl.www.designPatterns.statePattern.order2; import com.sun.tools.corba.se.idl.constExpr.Or; /** * Order evaluation */ public class OrderEvaluation implements IOrderStatus { public OrderEvaluation () { System.out.println( "Current Status:: Order Pending Evaluation"); } @Override public void pending (OrderContext context) { } @Override public void cancelPay (OrderContext context) { } @Override public void waitOut (OrderContext context) { } @Override public void refunds (OrderContext context) { } @Override public void waitReceive (OrderContext context) { } @Override public void confirmReceived (OrderContext context) { } @Override public void orderEvaluation (OrderContext context) { System.out.println( "Order evaluation finished" ); context.setOrderStatus( new Finish()); } @Override public void finish (OrderContext context) { } } Copy code

Order completed

package com.lxl.www.designPatterns.statePattern.order2; /** * Order completed */ public class Finish implements IOrderStatus { @Override public void pending(OrderContext context) { } @Override public void cancelPay(OrderContext context) { } @Override public void waitOut(OrderContext context) { } /* * */ public void refunds(OrderContext context) { } @Override public void waitReceive(OrderContext context) { } @Override public void confirmReceived(OrderContext context) { } @Override public void orderEvaluation(OrderContext context) { } @Override public void finish(OrderContext context) { System.out.println(" "); } }

package com.lxl.www.designPatterns.statePattern.order2; /** * */ public class CancelPay implements IOrderStatus { @Override public void pending(OrderContext context) { } /* * */ public void cancelPay(OrderContext context) { } @Override public void waitOut(OrderContext context) { } /* * */ public void refunds(OrderContext context) { } @Override public void waitReceive(OrderContext context) { } @Override public void confirmReceived(OrderContext context) { } @Override public void orderEvaluation(OrderContext context) { } @Override public void finish(OrderContext context) { } }

package com.lxl.www.designPatterns.statePattern.order2; public class Refunds implements IOrderStatus{ public Refunds() { System.out.println(" :: "); } @Override public void pending(OrderContext context) { } @Override public void cancelPay(OrderContext context) { } @Override public void waitOut(OrderContext context) { } @Override public void refunds(OrderContext context) { System.out.println(" "); context.setOrderStatus(new Finish()); } @Override public void waitReceive(OrderContext context) { } @Override public void confirmReceived(OrderContext context) { } @Override public void orderEvaluation(OrderContext context) { } @Override public void finish(OrderContext context) { } }

package com.lxl.www.designPatterns.statePattern.order2; public class OrderClient { public static void main(String[] args) { System.out.println("========== =========="); OrderContext o1 = new OrderContext(); System.out.println("=========== =============="); o1.cancelPay(); o1.finish(); System.out.println(); System.out.println(); System.out.println("========== =========="); OrderContext o2 = new OrderContext(); System.out.println("=========== =============="); o2.pending(); System.out.println("=========== =============="); o2.refunds(); o2.refunds(); o2.finish(); System.out.println(); System.out.println(); System.out.println("========== =========="); OrderContext o3 = new OrderContext(); System.out.println("=========== =============="); o3.pending(); System.out.println("=========== =============="); o3.waitOut(); System.out.println("=========== =============="); o3.waitReceive(); System.out.println("=========== =============="); o3.confirmReceived(); System.out.println("=========== =============="); o3.orderEvaluation(); System.out.println("=========== =============="); o3.finish(); } }

========== ==========

::
=========== ==============


========== ==========

::
=========== ==============
,
::
=========== ==============

::


========== ==========

::
=========== ==============
,
::
=========== ==============
,
::
=========== ==============
,
::
=========== ==============

::
=========== ==============

=========== ==============

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public class OrderContext { /** */ private IOrderStatus orderStatus; public OrderContext() { System.out.println(" "); // this.orderStatus = new Pending(); } public void setOrderStatus(IOrderStatus orderStatus) { this.orderStatus = orderStatus; } /** * */ public void toPay() { ((Pending)this.orderStatus).toPay(this); } /** * */ public void toCancelPay() { ((Pending)this.orderStatus).toCancelPay(this); } /** * */ public void cancelPay() { ((CancelPay)this.orderStatus).cancelPay(this); } /** * */ public void toSendProduct() { ((WaitOut)this.orderStatus).toSendProduct(this); } /** * */ public void toRefunds() { ((Confirm)this.orderStatus).toRefunds(this); } /** * */ public void refunded() { ((Refunds)this.orderStatus).refunded(this); } public void toReceiveProduct() { ((WaitReceive)this.orderStatus).toReceiveProduct(this); } /** * */ public void toConfirmReceived() { ((Confirm)this.orderStatus).toConfirmReceived(this); } /** * */ public void toOrderEvaluation() { ((OrderEvaluation)this.orderStatus).toOrderEvaluation(this); } /** * */ public void finish() { ((Finish)this.orderStatus).finish(this); } }

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public interface IOrderStatus { }

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public class Pending implements IOrderStatus { public Pending() { System.out.println(" :: "); } public void toPay(OrderContext context) { System.out.println(" , "); context.setOrderStatus(new WaitOut()); } /* * */ public void toCancelPay(OrderContext context) { System.out.println(" , "); context.setOrderStatus(new CancelPay()); } }

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public class WaitOut implements IOrderStatus { public WaitOut() { System.out.println(" :: "); } /* * */ public void toRefunds(OrderContext context) { System.out.println(" , "); context.setOrderStatus(new Refunds()); } /** * * @param context */ public void toSendProduct(OrderContext context) { System.out.println(" , "); context.setOrderStatus(new WaitReceive()); } }

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public class WaitReceive implements IOrderStatus { public WaitReceive() { System.out.println(" :: "); } /* * */ public void refunds(OrderContext context) { System.out.println(" "); context.setOrderStatus(new Refunds()); } /** * * @param context */ public void toReceiveProduct(OrderContext context) { System.out.println(" , "); context.setOrderStatus(new Confirm()); } }

package com.lxl.www.designPatterns.statePattern.order3; public class Confirm implements IOrderStatus { public Confirm() { System.out.println(" :: "); } public void toRefunds(OrderContext context) { System.out.println(" , "); context.setOrderStatus(new Refunds()); } public void toConfirmReceived(OrderContext context) { System.out.println(" "); context.setOrderStatus(new OrderEvaluation()); } }

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public class OrderEvaluation implements IOrderStatus { public OrderEvaluation() { System.out.println(" :: "); } public void toOrderEvaluation(OrderContext context) { System.out.println(" "); context.setOrderStatus(new Finish()); } }

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public class Finish implements IOrderStatus { public void finish(OrderContext context) { System.out.println(" "); } }

package com.lxl.www.designPatterns.statePattern.order3; /** * */ public class CancelPay implements IOrderStatus { /* * */ public void cancelPay(OrderContext context) { System.out.println(" "); context.setOrderStatus(new Finish()); } }

package com.lxl.www.designPatterns.statePattern.order3; public class Refunds implements IOrderStatus { public Refunds() { System.out.println(" :: "); } public void refunded(OrderContext context) { System.out.println(" "); context.setOrderStatus(new Finish()); } }

package com.lxl.www.designPatterns.statePattern.order3; public class OrderClient { public static void main(String[] args) { System.out.println("========== =========="); OrderContext o1 = new OrderContext(); System.out.println("=========== =============="); o1.toCancelPay(); o1.cancelPay(); System.out.println(""); System.out.println(""); System.out.println("========== =========="); OrderContext o2 = new OrderContext(); System.out.println("=========== =============="); o2.toPay(); System.out.println("=========== =============="); o2.toSendProduct(); System.out.println("=========== =============="); o2.toReceiveProduct(); System.out.println("=========== =============="); o2.toRefunds(); o2.refunded(); System.out.println("=========== =============="); o2.finish(); System.out.println(); System.out.println(); System.out.println("========== =========="); OrderContext o3 = new OrderContext(); System.out.println("=========== =============="); o3.toPay(); System.out.println("=========== =============="); o3.toSendProduct(); System.out.println("=========== =============="); o3.toReceiveProduct(); System.out.println("=========== =============="); o3.toConfirmReceived(); System.out.println("=========== =============="); o3.toOrderEvaluation(); System.out.println("=========== =============="); o3.finish(); } }

========== ==========

::
=========== ==============
,

========== ==========

::
=========== ==============
,
::
=========== ==============
,
::
=========== ==============
,
::
=========== ==============
,
::

=========== ==============

========== ==========

::
=========== ==============
,
::
=========== ==============
,
::
=========== ==============
,
::
=========== ==============

::
=========== ==============

=========== ==============

  • switch case if else

  • 5

  • ---
  • ---
  • ---
  • - ---
  • ---- ---