behavioral pattern - state pattern

State mode (State)

Troubleshoot object state transitions

describe

By encapsulating the state of the object into different classes, the object can have different behaviors in different states. This method can avoid a large number of conditional statements or switch-case structures, and can also improve the maintainability and scalability of the system.

Applicable environment

Use when an object's behavior depends on its internal state; use when you need to change the object's behavior based on state.

advantage:

It avoids the use of conditional statements to judge the state of the object, which improves the maintainability and scalability of the system; all state-related behaviors are encapsulated in the state class, making the code clearer.

shortcoming:

For objects with more states, it may lead to an increase in the complexity of the system; state patterns may lead to an increase in the number of classes.

violation of principles

Open-closed principle: If you need to add a new state, you need to modify all state classes, which violates this principle.

Code

Use an e-reader to read books In order to be able to read easily in a dark environment, you need to add a night mode.
Adjust the background color and font color to a color that is more suitable for night reading. You can use the state mode to solve this problem.
Create a A class named Display defines multiple state classes to represent different display states, day mode night mode
When you want to switch to another state, the Display class will automatically update its current state and change the background color and font color accordingly. The following is the specific implementation:


from abc import ABC, abstractmethod

class DisplayState(ABC):
    @abstractmethod
    def set_display(self, display):
        pass

    @abstractmethod
    def switch_mode(self):
        pass


class DayMode(DisplayState):
    def set_display(self, display):
        self.display = display

    def switch_mode(self):
        print("切换至夜间模式")
        self.display.set_state(NightMode())


class NightMode(DisplayState):
    def set_display(self, display):
        self.display = display

    def switch_mode(self):
        print("切换至白天模式")
        self.display.set_state(DayMode())


class Display:
    def __init__(self):
        self.state = DayMode()
        self.state.set_display(self)

    def set_state(self, state):
        self.state = state
        self.state.set_display(self)

    def switch_mode(self):
        self.state.switch_mode()


class Book:
    def __init__(self, title):
        self.title = title
        self.display = Display()

    def switch_mode(self):
        self.display.switch_mode()

Five classes are created in the code:
DisplayState: state abstract class defines set_display and switch_mode methods
DayMode, NightMode: specific state classes represent day mode and night mode respectively
Display: display class is used to manage different display states
Book: book class Contains the display settings of the e-reader.
You can switch to different display modes by using the status mode. When you want to switch the display mode, you only need to call the switch_mode() method of the Book class. This method
will automatically update the display status of the e-reader and respond accordingly. Change background color and font color freely. If you want to switch back to the previous display mode, you only need to call the switch_mode() method

Guess you like

Origin blog.csdn.net/u010349629/article/details/130031746