Detailed explanation of state mode of Android programming design mode

This paper illustrates the state mode of Android programming design mode. Share with you for your reference, as follows:

1、 Introduction

The behavior in the state mode is determined by the state, and there are different behaviors in different states. The structure of state mode and strategy mode is almost the same, but their purpose and essence are completely different. The behavior of state mode is parallel and irreplaceable, and the behavior of policy mode is independent and replaceable. In one sentence, the state pattern packages the behavior of objects in different state objects, and each state object has a common abstract state base class. The intention of state pattern is to make an object change its behavior when its internal state changes.

2、 Definition

When the internal state of an object changes, it is allowed to change its behavior. The object looks like it has changed its class.

3、 Usage scenario

(1) The behavior of an object depends on its state, and it must change its behavior according to its state at run time. (2) The code contains a large number of conditional statements related to the state of the object. For example, an operation contains a large number of multi branch statements (if else or switch case), and these branches depend on the state of the object.

The state mode puts each conditional branch into an independent class, which allows you to treat the state of the object as an object according to the situation of the object itself. This object can change independently without relying on other objects. In this way, too many and repeated if else and other branch statements can be removed through polymorphism.

4、 UML class diagram of state pattern

UML class diagram:

Role introduction:

Context: environment class, which defines the interface of interest to customers and maintains an instance of state subclass, which defines the current state of the object.

State: abstract state class or state interface, which defines one or a group of interfaces to represent the behavior in this state.

Concretestatea, concretestateb: concrete state classes. Each concrete state class implements the interfaces defined in the abstract state, so as to achieve different behaviors in different states.

5、 Simple example

Let's take the TV remote control as an example to demonstrate the implementation of state mode. Firstly, we simply divide the TV state into power on state and power off state. In the power on state, you can switch channels and adjust volume through the remote control. However, pressing the power on key repeatedly is invalid at this time; In the shutdown state, channel switching, volume adjustment and shutdown are invalid operations, which will take effect only when the startup button is pressed. In other words, the internal state of the TV determines the behavior of the remote control.

The first is the common implementation method:

It can be seen that each time the operation is performed by judging the current state, part of the code repeats, and it will be more and more difficult to maintain if the state and function increase. At this time, the status mode can be used as follows:

TV status interface:

Shutdown status:

Power on status:

Power operation interface:

Remote control:

Call:

The output results are as follows:

In the above implementation, we abstract a tvstate interface, which contains all functions for operating TV. The interface has two implementation classes, namely, power on state and power off state. In the boot state, only the boot function is invalid, that is, the user will not have any response when pressing the boot key when it is already powered on; In the shutdown state, only the startup function is available, and other functions will not take effect. The same operation, such as the turnup function to increase the volume, is invalid in the shutdown state. In the startup state, the volume of the TV will be increased, that is, the internal state of the TV affects the behavior of the TV remote control. State mode encapsulates these behaviors into state classes and forwards these functions to state objects during operation. Different states have different implementations. In this way, repeated and messy if else statements are removed in the form of polymorphism, which is the essence of state mode.

6、 Use of Android in actual combat

1. Log in to the system and judge the event handling method according to whether the user logs in or not. 2. Wi Fi management, WiFi scanning requests are processed differently in different states.

Take the login system as an example to explain the use of state mode in practice:

In the development of Android, the login interface is very common, and the state design pattern is widely used in the login interface. Users operate the logic differently in login and non login states. For example, the most common situation is that when playing Sina Weibo, users can complete the operation of commenting and forwarding Weibo only when they log in; When the user is not logged in, the operation of forwarding and commenting on microblog can only be performed after logging in the login interface. Therefore, in the face of these two different situations, it is best to use the state design pattern to design this example.

1. State base class

As we mentioned earlier, the principle of state design pattern is actually polymorphic. Here, we use userstate interface to represent this base class, including forwarding operation and comment. The code is as follows:

2. The user's implementation classes loginstate and logoutstate under login and non login conditions; The code is as follows:

In loginstate.java, users can forward and comment.

In logoutstate.java, users are not allowed to perform operations without logging in. Instead, they should jump to the login interface and perform login before they can perform operations.

3. Action character logincontext

The logincontext here is the context role in the state mode. It is the user operation object and management object. Logincontext delegates relevant operations to the state object. When the state changes, the behavior of logincontext also changes. The logincontext code is as follows:

reminder:

The single example we use here is because there is only one global logincontext to control the user state;

4. Interface display

Loginactivity.java. This interface performs login operation. After logging in, put logincontext. Getinstance(). Setstate (New loginstate()); Set to login status, mainactivity performs operations in login status, that is, you can forward comments;

Mainactivity.java, after the user logs in successfully, click forward and comment to perform the operations in the login status. When the user logs out, we set the logincontext status to the non login status; LoginContext.getInstance().setState(new logoutState()); At this time, when you click forward and comment, you will jump to the user login interface.

7、 Summary

The key point of state pattern is that different states have different responses to the same behavior, which is actually a concrete example of implementing if else with polymorphism. In the form of if else or switch case, judge according to different states. If it is state a, execute method a and state B, execute method B. However, this implementation makes the logic coupled together and easy to make mistakes. This kind of "ugly" can be well eliminated through the state mode "Of course, not any place where if else occurs should be reconstructed through state patterns. The application of patterns must consider the situation and the problems you want to solve. It is recommended to use the corresponding patterns only if they meet specific scenarios.

advantage:

Putting all the behaviors related to a specific state into a state object provides a better way to organize the code related to a specific state, and transforms the cumbersome state judgment into a clearly structured state class family, which not only avoids code expansion, but also ensures scalability and maintainability.

Disadvantages:

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

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>