In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use state mode instead of if-else". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use state mode instead of if-else.
Brief introduction
State pattern is a kind of behavioral design pattern. The design idea is to change the behavior of the object when its internal state changes. There is an one-to-one correspondence between state and behavior.
This mode is mainly used when the behavior of an object depends on its state, and its behavior is switched as the state changes.
State mode UML class diagram
Class diagram explanation
State: an abstract state interface (which can also be defined as an abstract class) that encapsulates the behavior corresponding to all states.
ConcreteStateA/B: concrete state class, which implements the abstract state interface and implements the methods defined in the interface according to its own corresponding state. Another function is to indicate how to transition to the next state.
Context: environment (context) role, which is responsible for state switching and holds an instance of State that represents the state of the current environment.
Case explanation
Case: realize the function of the self-service vending machine through the status mode.
State interface
Public interface State {/ / Select goods void choose (); / / pay boolean payment (); / / distribute goods void dispenseCommodity ();}
Select commodity status category
Public class ChooseGoods implements State {VendingMachine machine; public ChooseGoods (VendingMachine machine) {this.machine = machine;} @ Override public void choose () {if (machine.getCount () > 0) {System.out.println ("goods selected successfully, please pay in time!") ; machine.setState (machine.getPaymentState ());} else {System.out.println ("Unfortunately, the goods are sold out!") ; machine.setState (machine.getEmptyState ());}} @ Override public boolean payment () {System.out.println ("Please pick the goods first!") ; return false;} @ Override public void dispenseCommodity () {System.out.println ("Please pick the goods first!") ;}}
Payment status class
Public class PaymentState implements State {VendingMachine machine; public PaymentState (VendingMachine machine) {this.machine = machine;} @ Override public void choose () {System.out.println ("do not repeat the purchase");} @ Override public boolean payment () {Random random = new Random (); int num = random.nextInt (10) If (num% 2 = = 0) {System.out.println ("payment succeeded!") ; machine.setState (machine.getDispenseCommodityState ()); return true;} System.out.println ("payment failed, please pay again!") ; return false;} @ Override public void dispenseCommodity () {System.out.println ("Please pay first!") ;}}
Goods sold out status category
Public class EmptyState implements State {VendingMachine machine; public EmptyState (VendingMachine machine) {this.machine = machine;} @ Override public void choose () {System.out.println ("Sorry the goods are sold out!") ; @ Override public boolean payment () {System.out.println ("Sorry the goods are sold out!") ; return false;} @ Override public void dispenseCommodity () {System.out.println ("Sorry the goods are sold out!") ;}}
Distribute merchandise status class
Public class DispenseCommodityState implements State {VendingMachine machine; public DispenseCommodityState (VendingMachine machine) {this.machine = machine;} @ Override public void choose () {System.out.println ("Please pick up your goods in time!") ; @ Override public boolean payment () {System.out.println ("Please pick up your goods in time!") ; return false;} @ Override public void dispenseCommodity () {System.out.println ("Please pick up your goods in time!") ; machine.setState (machine.getChooseGoods ());}}
Vending machine = > Context role
Public class VendingMachine {/ / indicates the current status private State state = null; / / quantity of goods private int count = 0; private State chooseGoods = new ChooseGoods (this); private State paymentState = new PaymentState (this); private State dispenseCommodityState = new DispenseCommodityState (this); private State emptyState = new EmptyState (this); public VendingMachine (int count) {this.count = count; this.state = this.getChooseGoods () } / / purchase merchandise public void purchase () {/ / Select merchandise state.choose (); / / pay successfully if (state.payment ()) {/ / distribute merchandise state.dispenseCommodity ();}} / / subtract merchandise by one public int getCount () {return count-- after obtaining the merchandise } / / get and set methods.}
Client test class
Public class Client {public static void main (String [] args) {VendingMachine machine = new VendingMachine (1); for (int I = 1; I < 4; iTunes +) {System.out.println ("th" + I + "purchase.") ; machine.purchase ();}
Execution result
Summary
1. The state mode encapsulates the behavior corresponding to each state into a class, which greatly improves the readability of the code. And through this design, redundant if-else statements can be eliminated to facilitate the maintenance of the code.
2. The state mode accords with the "opening and closing principle", so it is easy to add and delete states.
3. Everything has its advantages and disadvantages, and the state mode is no exception. The most obvious problem is that each state corresponds to a class, and when there are too many states, a large number of classes will be generated, thus increasing the maintenance cost.
4. Application scenario: when a requirement has many states, and there are transitions between states, and different states correspond to different behaviors, you can consider using "state mode".
At this point, I believe you have a deeper understanding of "how to use state mode instead of if-else". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.