Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the state pattern in the Python design pattern

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what the state pattern in the Python design pattern is. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

State mode

State mode, which allows objects to execute different processes when their internal state changes.

Advantages:

Encapsulates the state transition rules.

The possible states are enumerated, and the type of state needs to be determined before enumerating the states.

Put all the behaviors related to a state into one class, and you can easily add new states. You only need to change the state of the object to change the behavior of the object.

Allows the state transition logic to be integrated with the state object, rather than a large block of conditional statements.

Multiple environment objects can share a state object, thus reducing the number of objects in the system.

Disadvantages:

The use of state mode will inevitably increase the number of system classes and objects.

The structure and implementation of the state mode are complex, and if it is not used properly, it will lead to confusion of the program structure and code.

The state mode is not very good for the "open-close principle". For the state mode that can be switched, adding a new state class needs to modify the source code responsible for the state transition, otherwise it is impossible to switch to the new state. and to modify the behavior of a state class also needs to modify the source code of the corresponding class.

Application scenario

A scene in which the behavior changes with the state.

The replacement of condition and branch statement.

Code example

This is a status chart, with "25 cents", "no 25 cents", "sold candy", "candy sold out" four states. At the same time, there are also four actions: "invest 25 cents", "return 25 cents", "turn the crank" and "hand out candy".

Class State: # define state base class def insert_quarter (self): pass def eject_quarter (self): pass def turn_crank (self): pass def dispense (self): passclass SoldOutState (State): # inherit State class def _ _ init__ (self Gumball_machine): self.gumball_machine = gumball_machine def _ str__ (self): return "sold_out" def insert_quarter (self): print ("You can't insert a quarter, the machine is sold out") def eject_quarter (self): print ("You can't eject, you haven't inserted a quarter yet") def turn_crank (self): print ("You turned" But ther are no gumballs ") def dispense (self): print (" No gumball dispensed ") class SoldState (State): # inherit State class def _ init__ (self, gumball_machine): self.gumball_machine = gumball_machine def _ str__ (self): return" sold "def insert_quarter (self): print (" Please wait " We're already giving you a gumball ") def eject_quarter (self): print (" Sorry " You already turned the crank ") def turn_crank (self): print (" Turning twice doesn't get you another gumball ") def dispense (self): self.gumball_machine.release_ball () if gumball_machine.count > 0: self.gumball_machine.state = self.gumball_machine.no_quarter_state else: print (" Oops, out of gumballs! ") Self.gumball_machine.state = self.gumball_machine.soldout_stateclass NoQuarterState (State): # inherit the State class def _ _ init__ (self Gumball_machine): self.gumball_machine = gumball_machine def _ str__ (self): return "no_quarter" def insert_quarter (self): # coin and change the status print ("You inserted a quarter") self.gumball_machine.state = self.gumball_machine.has_quarter_state def eject_quarter (self): print ("You haven't" Insert a quarter ") def turn_crank (self): print (" You turned " But there's no quarter ") def dispense (self): print (" You need to pay first ") class HasQuarterState (State): # inherit State class def _ _ init__ (self Gumball_machine): self.gumball_machine = gumball_machine def _ str__ (self): return "has_quarter" def insert_quarter (self): print ("You can't insert another quarter") def eject_quarter (self): print ("Quarter returned") self.gumball_machine.state = self.gumball_machine.no_quarter_state def turn_crank (self): print ("You turned...") Self.gumball_machine.state = self.gumball_machine.sold_state def dispense (self): print ("No gumball dispensed") class GumballMachine: def _ _ init__ (self, count=0): self.count = count # find all states and create instance variables to hold the current state Then define the value of the state self.soldout_state = SoldOutState (self) self.no_quarter_state = NoQuarterState (self) self.has_quarter_state = HasQuarterState (self) self.sold_state = SoldState (self) if count > 0: self.state = self.no_quarter_state else: self.state = self.soldout_state def _ str_ _ (self): return "> Gumball machine current state:% s"% self.state def insert_quarter (self): # invest 25 cents self.state.insert_quarter () def eject_quarter (self): # return 25 cents self.state.eject_quarter () # print ("state") Self.state, type (self.state) def turn_crank (self): # turn the crank # print ("state", self.state, type (self.state)) self.state.turn_crank () def release_ball (self): # hand out candy print ("A gumball comes rolling out the slot...") If self.count > 0: self.count-= 1 if _ _ name__ = "_ _ main__": # the following is the code test gumball_machine = GumballMachine (5) # load 5 candy print (gumball_machine) gumball_machine.insert_quarter () # invest 25 cents gumball_machine.turn_crank () # turn the crank print (gumball_machine) Gumball_machine.insert_quarter () # invest 25 cents gumball_machine.eject_quarter () # refund gumball_machine.turn_crank () # turn crank print (gumball_machine) gumball_machine.insert_quarter () # invest 25 cents gumball_machine.turn_crank () # turn crank gumball_machine.insert_quarter () # invest 25 cents Gumball_machine.turn_crank () # turn the crank gumball_machine.eject_quarter () # refund print (gumball_machine) about "what is the state pattern in the Python design pattern" this article ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report