Design Mode note status mode State

Source: Internet
Author: User

Design Mode note status mode State

 

// State mode ---- object behavior mode

 

/*

1: Intention:

Allows an object to change its behavior when its internal state changes. The object seems to have modified its class.

2: alias:

Status object (Objects for States)

3: motivation:

4: Usability:

1> the behavior of an object depends on its state, and it must change its behavior according to its state at runtime.

2> An operation contains a large number of multi-branch condition statements, and these branches depend on the state of the object. This status usually uses one or

Multiple enumerated constants.

5: structure:

Context:

State ----------------------------------> State:

Request () Handle ()

{State-> Handle ()} |

----------------------

|

ConcreteStateA: ConcreteStateB:

Handle ()

6: participants:

1> Context:

1) define interfaces of interest to the customer.

2) maintain an instance of the ConcreteState subclass, which defines the current status.

2> State:

Defines an interface to encapsulate a behavior related to a specific State of Context.

3> ConcreteState:

Each subclass implements a state-related Behavior of Context.

7: collaboration:

1> Context delegates state-related requests to the current ConcreteState object for processing.

2> Context can pass itself as a parameter to the status object that processes the request. This allows the State object to access Context when necessary.

3> Context is the main interface used by the customer. You can use a State object to configure a Context. Once a Context is configured,

Its customers no longer need to deal with State objects directly.

4> the Context or ConcreteState subclass can determine which State is the successor and under which conditions the State is converted.

8: effect:

1> it separates local behaviors related to a specific State and does not use the state:

Splits a large number of case statements into different state subclasses to make the structure clearer and easy to increase.

And switch to a status.

2> make the status transition explicit:

In Context, State conversion is atomic-you only need to re-bind a State variable. This ensures that the internal status is consistent.

3> the State object can be shared:

If the State object does not have instance variables, it can be easily shared, because they only have the external State, but not the internal State.

9: Implementation:

1> who defines State Conversion:

1) Context: the conversion follows a fixed principle.

2) State itself specifies the successor State and when to convert: This is more flexible, but the State subclass must have information of other subclasses.

In this way, the dependency is generated, that is, the coupling becomes larger.

2> create and destroy State objects:

1) create and destroy them only when the State object is required.

2) create them in advance and never destroy them.

That is, the confrontation between space and time. The first method focuses on space utilization, and the second method focuses on the importance of time.

10: Sample Code :*/

// Indicates that these two are classes

Class TCPOctetStream;

Class TCPState;

 

// Context: defines the interface

Class TCPConnection

{

Public:

TCPConnection ();

 

Void ActiveOpen ();

Void PassiveOpen ();

Void Close ();

Void Send ();

Void Acknowledge ();

Void Synchronize ();

Void ProcessOctet (TCPOctetStream *);

Private:

// You can use the ChanggeState interface only when you set it to a friend class.

Friend class TCPState;

Void ChangeState (TCPState *);

Private:

TCPState * _ state;

};

 

// Abstract State: defines interfaces and implements default operations.

Class TCPState

{

Virtual void Transmit (TCPConnection *, TCPOctetStream *);

Virtual void ActiveOpen (TCPConnection *);

Virtual void PassiveOpen (TCPConnection *);

Virtual void Close (TCPConnection *);

Virtual void Send (TCPConnection *);

Virtual void Acknowledge (TCPConnection *);

Virtual void Synchronize (TCPConnection *);

Protected:

Void ChangeState (TCPConnection *, TCPState *);

};

 

// The Initialization is disabled.

TCPConnection: TCPConnection ()

{

_ State = TCPClosed: Instance ();

}

 

// Set the status

Void TCPConnection: ChangeState (TCPState *)

{

_ State = s;

}

 

// All calls are transferred to the status object for calling

Void TCPConnection: ActiveOpen ()

{

_ State-> ActiveOpen (this );

}

Void TCPConnection: PassiveOpen ()

{

_ State-> PassiveOpen (this );

}

Void TCPConnection: Close ()

{

_ State-> Close (this );

}

Void TCPConnection: Send ()

{

_ State-> Send (this );

}

Void TCPConnection: Acknowledge ()

{

_ State-> Acknowledge (this );

}

Void TCPConnection: Synchronize ()

{

_ State-> Synchronize (this );

}

 

// Define the default implementation

Void TCPState: Transmit (TCPConnection *, TCPOctetStream *){}

Void TCPState: ActiveOpen (TCPConnection *){}

Void TCPState: PassiveOpen (TCPConnection *){}

Void TCPState: Close (TCPConnection *){}

Void TCPState: Send (TCPConnection *){}

Void TCPState: Acknowledge (TCPConnection *){}

Void TCPState: Synchronize (TCPConnection *){}

 

 

Void TCPState: ChangeState (TCPConnection *, TCPState * s)

{

T-> ChangeState (s );

}

 

// ConcreteState: all three are applicable to the singleton mode.

Class TCPEstablished: public TCPState

{

Public:

Static TCPState * Instance ();

Virtual void Transmit (TCPConnection *, TCPOctetStream *);

Virtual void Close (TCPConnection *);

};

 

// ConcreteState:

Class TCPListen: public TCPState

{

Public:

Static TCPState * Instance ();

Virtual void Send (TCPConnection *);

//...

};

 

// ConcreteState:

Class TCPClosed: public TCPState

{

Static TCPState * Instance ();

 

Virtual void ActiveOpen (TCPConnection *);

Virtual void PassiveOpen (TCPConnection *);

};

 

// Perform some specific operations through the received parameter and use this parameter to convert the status

Void TCPClosed: ActiveOpen (TCPConnection * t)

{

// Send SYN, reveive SYN, ACK, etc.

ChangeState (t, TCPEstablished: Instance ());

}

 

Void TCPClosed: PassiveOpen (TCPConnection * t)

{

ChangeState (t, TCPListen: Instance ());

}

 

Void TCPEstablished: Close (TCPConnection * t)

{

// Send FIN, reveive ACK of FIN

ChangeState (t, TCPListen: Instance ());

}

 

Void TCPEstablished: Transmit (TCPConnection * t, TCPOctetStream * o)

{

T-> ProcessOctet (o );

}

 

Void TCPListen: Send (TCPConnection * t)

{

// Send SYN, receive SYN, ACK etc

ChangeState (t, TCPEstablished: Instance ());

}

 

 

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.