The status mode is applicable to the One-Stop pipeline that requires constant switching between actual types, such as goods, checkout, and actual payment. During this period, a session user's status switching exists. You can use a controller class that implements the interface to control the switching of the actual status of each class. The specific control method is implemented in another context class. Each implementation class implements the singleton mode. Package StateMethod; public interface IShopState {public void shop (); public void generateBill (); public void pay (); // protected void changeState (ShopContext c, IShopState );} package StateMethod; public class ShopState implements isments state {public ShopState () {} public void shop () {} public void generateBill () {} public void pay () {} protected void changeState (ShopContext c, ShopState s) {c. ChangeState (s) ;}} package StateMethod; public class Shop extends ShopState {public static boolean instanceFlag = false; // true if have 1 instance private Shop () {} public static Shop getInstance () {if (! InstanceFlag) {instanceFlag = true; return new Shop ();} return null;} public void shop () {System. out. println ("The state is shopping now! ") ;}} Package StateMethod; public class GenerateBill extends ShopState {public static boolean instanceFlag = false; // true if have 1 instance private GenerateBill () {} public static GenerateBill getInstance () {if (! InstanceFlag) {instanceFlag = true; return new GenerateBill ();} return null;} public void generateBill () {System. out. println ("The state is generating bill now! ") ;}} Package StateMethod; public class Pay extends ShopState {public static boolean instanceFlag = false; // true if have 1 instance private Pay () {} public static Pay getInstance () {if (! InstanceFlag) {instanceFlag = true; return new Pay ();} return null;} public void pay () {System. out. println ("The state is pay now! ") ;}} Package StateMethod; public class ShopContext {private ShopState currentState; public ShopContext () {} public void changeState (ShopState s) {currentState = s;} public void shop () {currentState. shop ();} public void generateBill () {currentState. generateBill ();} public void pay () {currentState. pay () ;}} package StateMethod; public class Test {public static void main (String [] args) {Sh OpContext myContext = new ShopContext (); ShopState myShop = Shop. getInstance (); ShopState myGenBill = GenerateBill. getInstance (); ShopState myPay = Pay. getInstance (); myContext. changeState (myShop); myContext. shop (); myContext. changeState (myGenBill); myContext. generateBill (); myContext. changeState (myPay); myContext. pay (); myShop. changeState (myContext, myPay); myContext. pay () ;}} output: The state is sho Pping now! The state is generating bill now! The state is pay now! The state is pay now!