State Design Pattern-learning Summary-difference between state design pattern and policy design pattern

Source: Internet
Author: User

Intent:

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

 

 

Applicable environment:

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 state is usually represented by one or more enumerated constants. Generally, multiple operations contain the same condition structure. In State mode, each condition branch is placed in an independent class. This allows you to take the object state as an object based on the object's own situation. This object can be independent of other objects.

 

Structure:

 

Implementation:

 

State. h file

 

# Pragma
Once

 

Class
Context;

Class
State

{

Public:

State (void );

Virtual ~ State (void );

Virtualvoid
Operationinterface (Context * con );

Virtualvoid
Operationchangestate (Context * con );

Protected:

Boolchangestate (Context * con, state * st );

 

};

 

Class
Concretestatea: Public
State

{

Public:

Concretestatea ();

Virtual ~ Concretestatea ();

 

Virtualvoid
Operationinterface (Context * con );

Virtualvoid
Operationchangestate (Context * con );

 

Protected:

 

};

 

Class
Concretestateb: Public
State

{

Public:

Concretestateb ();

Virtual ~ Concretestateb ();

 

Virtualvoid
Operationinterface (Context *);

Virtualvoid
Operationchangestate (Context *);

 

Protected:

 

};

 

State. cpp File

# Include
"Stdafx. H"

# Include
"State. H"

# Include
"Context. H"

 

# Include
"Iostream"

Using namespace
STD;

 

State: State (void)

{

}

 

State ::~ State (void)

{

}

 

Void
State: operationinterface (Context * con)

{

Cout <"State:" <Endl;

}

 

Void
State: operationchangestate (Context * con)

{

 

}

 

Bool
State: changestate (Context * con, state * st)

{

Con-> changestate (ST );

Returntrue;

}

 

Concretestatea: concretestatea ()

{

 

}

 

Concretestatea ::~ Concretestatea ()

{

 

}

 

Void
Concretestatea: operationinterface (Context * con)

{

Cout <"concretestatea: operation interface..." <Endl;

}

 

Void
Concretestatea: operationchangestate (Context * con)

{

Operationinterface (CON );

This-> changestate (con, new
Concretestateb ());

Cout <"change state to B" <Endl;

}

 

 

//

Concretestateb: concretestateb ()

{

 

}

 

Concretestateb ::~ Concretestateb ()

{

 

}

 

Void
Concretestateb: operationinterface (Context * con)

{

Cout <"concretestateb: operation interface..." <Endl;

}

 

Void
Concretestateb: operationchangestate (Context * con)

{

Operationinterface (CON );

This-> changestate (con, new
Concretestatea ());

Cout <"change state to a" <Endl;

}

 

Context. h file

# Ifndef
_ State_h

# Define
_ State_h

Class
State;

 

Class
Context

{

Public:

Context (void );

Context (State * State );

Voidoperationinterface ();

Voidoperationchangestate ();

Virtual ~ Context (void );

 

PRIVATE:

Friendclass
State;
// Indicates that the private field of the context class can be accessed in the state class.

Bool
Changestate (State * State );

State * _ state;

};

# Endif

 

Context. cpp File

# Include
"Stdafx. H"

# Include
"Context. H"

# Include
"State. H"

 

Context: Context (void)

{

}

 

Context ::~ Context (void)

{

If (_ state! = NULL)

{

Delete_state;

_ State = NULL;

}

 

 

}

 

Context: Context (State * State)

{

This-> _ state = State;

}

 

Void
Context: operationinterface ()

{

_ State-> operationinterface (this );

 

}

 

Bool
Context: changestate (State * State)

{

This-> _ state = State;

Returntrue;

}

 

Void
Context: operationchangestate ()

{

_ State-> operationchangestate (this );

}

 

Main function:

// Statepatten. cpp: defines the entry point of the console application.

//

 

# Include
"Stdafx. H"

# Include
"State. H"

# Include
"Context. H"

# Include
<Iostream>

Using namespace
STD;

 

Int _ tmain (intargc,
_ Tchar * argv [])

{

State * ST = new
Concretestatea ();

Context * con = new
Context (ST );

Con-> operationinterface ();

Con-> operationchangestate ();

 

If (con! = NULL)

{

Deletecon;

Con = NULL;

}

 

If (st! = NULL)

{

Deletest;

St = NULL;

}

 

Return0;

}

 

Code Description:

The State mode has two key points:

(1) declare the state as the friend class of the context. Its function is to allow the State mode to access the protected interface changestate () of the context ();

(2) All the operations in the state and its sub-classes pass context * as a parameter. The main purpose of this parameter is that the state class can call the method in context through this pointer, this is also the difference between the State mode and the Strategy mode;

 

The State mode and the Strategy Mode are similar to each other to a large extent: they all have a context class, both through delegation (combination) implement the context algorithm logic for a multi-state base class with multiple Derived classes. The biggest difference between the two is that the derived class in state mode holds a reference to the context object and calls the method in context through this reference, but this is not the case in Strategy Mode. Therefore, it can be said that a State instance is also an instance in strategy mode, but the opposite is not true. In fact, the difference between the State mode and the Strategy Mode is that they have different points of attention: State
The mode is mainly to adapt to the implementation of different processing policies for the object when the state changes, while the strategy is mainly to decouple the specific algorithm and implementation interface (coupling ).

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.