State mode of design mode

Source: Internet
Author: User
Tags ack

Status mode: state

definition : When an object's internal state changes, allowing changes to its behavior, the object looks like it has changed its class.

Example:

Design pattern in the book, the example of TCP state transfer is given. For example, when a TCP connection receives a SYN in the Listen state and sends Syn+ack, it enters the SYN receive state. When an ACK is received in the SYN receive state, it enters the established state, and in a normal program, a large number of if else or switch are required to determine the command received and the corresponding state changes.

Such as:

if (stat = = listen)

if (Receive SYN and send Syn,ack)

Stat = established

else ...

State mode primarily addresses this type of problem. When the conditional expression that controls an object state transition is too complex,

Transfer the judgment logic of state transitions to a series of classes that represent different states. Complex logic can be simplified.

Class diagram for State mode:

State class: An abstract status class that defines the behavior that an interface uses to encapsulate the specific state of a context class.

Concertstate: A specific state class in which each subclass implements the behavior associated with the context state.

Context: Maintains an instance of the State class that represents the current status.

The TPC State Transfer program can be designed as follows:

1 classTcpconn2 {3   Public:4 tcpconn ();5     voidListen () {_stat->listen ( This);6     voidsynreceived ();7 ......8      voidSetState (tcpstat* state) {_state =State ;}9 Private:Tentcpstate*_state;  One }; A  - classtcpstate - { the piblic: -VirtaulvoidChangestae (Tcpconn *conn); -VirtaulvoidListen (tcpconn*conn); -VirtaulvoidSynreceived (tcpconn*conn); + }; -  + classTcplistenstate: Publictcpstate A { at      Virtual voidListen (tcpconn*conn) -       { -             //receive Ayn send ack and SYN -Conn.setstate (Newtcpsynreceivedstate ()); -        } - } in  -  to Client: +tcpconn* conn =NewTcpconn; -Conn->listen ();

Consider using state mode when the behavior of an object depends on its state, and it must change its behavior based on the state.

State mode of design mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.