Step 1: first, I can visit five scenic spots and define an enumeration type for the five scenic spots. The Code is as follows: /// <summary> /// Palace Museum attractions /// </summary> enum ImperialPalaceEnum {Taihe Temple, qianqing Palace, royal garden, dongliu Palace, Jiulong wall} Step 2: the Code is as follows: /// <summary> /// class /// </summary> public class IPVisit {// <summary> // current location // </summary> private ImperialPalaceEnum ipCurrentSpot; public ImperialPalaceEnum IpCurrentSpot {set {ipCurrentSpot = value ;}}/// <summary> // feel the scenic spots /// </Summary> public void ExperienceSpot () {// feel the current Scenic Spot switch (ipCurrentSpot) {case ImperialPalaceEnum. taihe temple: Console. writeLine ("Taihe temple: the living room of the Emperor's house is really magnificent. I was excited in my mind when I held a large ceremony! "); Break; case ImperialPalaceEnum. Qian Qing palace: Console. WriteLine? "); Break; case ImperialPalaceEnum. royal Garden: Console. writeLine ("Royal Garden: The Royal Garden is really not as beautiful as you think, it is very lost. "); break; case ImperialPalaceEnum. dongliugong: Console. writeLine. "); break; case ImperialPalaceEnum. kowloon wall: Console. writeLine. "); break ;}} Step 3: The main function call code is as follows: IPVisit visit = new IPVisit (); visit. ipCurrentSpot = ImperialPalaceEnum. taihe temple; visit. experienceSpot (); visit. ipCurrentSpot = ImperialPalaceEnum. qian Qing palace; visit. experienceSpot (); visit. ipCurrentSpot = ImperialPalaceEnum. royal Garden; visit. experienceSpot (); visit. ipCurrentSpot = ImperialPalaceEnum. visit. experienceSpot (); visit. ipCurrentSpot = ImperialPalaceEnum. kowloon wall; visit. experienceSpot (); running result: Taihe temple: the living room of the Emperor's home is really magnificent. I was excited about the large ceremony! 2 Qian Qing palace: Is it because the Yangxin Temple is very close to the hougong and Yong Zheng was moved in? 3. Royal Garden: The Royal Garden is really not as beautiful as you think. It is very lost. 4: I can't see the living environment of my colleagues in that year. 5. Jiulong wall: mixed up in the palace. The game won't face people on the 16th card. 6. Press any key to continue... ii. Status mode analysis the above Code will find that the behavior of the object to visit IPVisit in the Forbidden City depends on its status. For example, when the ipCurrentEnum attribute is equal to different attribute values, you can see the method of scenic spots: the behavior of ExperienceSpot is different. This will first lead to different responsibilities for a method. If there are too many responsibilities for this method, maintenance will inevitably be a headache, for example, if you look at the code in the second step in the above scenario Code, every case in the switch implements a state behavior between the break. At this time, if a new attraction (Status) is added, the code here will be modified, which violates the "open-closed principle ". At this time, someone will say that sometimes our needs are not so complex. I think it is very good to use the switch to implement it. It is simple and clear, and I agree with it very much, the design is tailored to the actual needs of the design is the best design. It's a little far away, so how can we optimize the code above so that it can be flexibly expanded to reduce complexity? Don't worry. The elders of the four-person design model have provided a perfect solution to this type of problem, And the status model is on stage! 1. What is the status mode? The definition in the design patterns book is: an object changes its behavior when its internal state changes, and the object looks like it is modifying its class. 2. State pattern Context: maintain an instance in the current state. State: this interface abstracts the behaviors related to a specific State of Context. ConcreteState: implements the action of the specific state of Context. 3. The State mode code implementation Context class is used to maintain an instance in the current state, indicating the current state. The Code is as follows: /// <summary> /// the entity that maintains the current State // </summary> public class Context {public Context () {} public Context (state State) {this. currentState = state;} private State currentState; public State CurrentState {set {currentState = value ;}} /// <summary> /// current execution status /// </summary> public void Request () {currentState. handle (this);} State interface: Abstraction of State behaviors required by Context. The Code is as follows: public interface State {void Handle (Context context);} ConcreteState class: The instance that implements the State behavior required by the Context according to the State abstraction. The Code is as follows: /// <summary> /// execute the action corresponding to this State and set the next State instance /// </summary> public class ConcereteStateA: State {public void Handle (Context context) {Console. writeLine ("status A: The processing behavior is executed ..... switch the next status B ...... "); context. currentState = new ConcereteStateB () ;}/// <summary> // execute the action corresponding to this state and set the next state instance /// </summary> p Ublic class ConcereteStateB: State {public void Handle (Context context) {Console. writeLine ("status B: The processing behavior is executed ..... switch to the next status ...... "); context. currentState = new ConcereteStateA () ;}} main function call: Context context = new Context (new ConcereteStateA (); context. request (); context. request (); the output result is as follows: Status A: The processing behavior is executed ..... switch the next status B ...... status B: The processing behavior is executed ..... switch to the next status ...... 4. The advantages of the state mode split the behavior of a specific State, because the state mode puts all the behavior of the state into a specific object, through the implementation of the State Abstract (Interface State), you can easily add new States. Iii. Implementation of the State model of the old palace game first, I will Visit different scenic spots during my Visit (Visit), and I will see that the status of each scenic spot changes, that is to say, each time I visit a scenic spot, there will be a corresponding status. At this time, each scenic spot should correspond to a specific State implementation. So when I visit different scenic spots, the current status of the instance also needs to be switched, the class diagram is as follows: first, the visit to the Palace Museum class, each visit to a scenic spot will change my inner state, therefore, an attribute is required to indicate the current status. When you experience the scenic spot, the corresponding status is executed. The Code is as follows: // <summary> // visit the Palace Museum class // </summary> public class IPVisitContext {public IPVisitContext () {} public IPVisitContext (ISpotState state) {this. currentState = state;} // <summary> // current state // </summary> private ISpotState currentState; public ISpotState CurrentState {set {currentState = value ;}} /// <summary> /// view the corresponding status of the Scenic Spot. /// </summary> public void ExperienceSpot () {currentState. ex PerienceSpot (this) ;}} has different status instances for each attraction. In this case, a State abstraction is required. In the future, it is easy for users to expand new status instances. The Code is as follows: /// <summary> /// abstract of the visiting scenic spot status /// </summary> public interface ISpotState {void ExperienceSpot (IPVisitContext context );} next, the status of each scenic spot is implemented. The Code is as follows: /// <summary> // Taihe temple status behavior instance /// </summary> public class Taihe temple State: ISpotState {public void ExperienceSpot (IPVisitContext context) {Console. writeLine The scenes of large ceremonies are excited! "); Context. currentState = new Qian Qing palace State () ;}/// <summary> // Qian Qing palace State behavior instance /// </summary> public class Qian Qing palace State: ISpotState {public void ExperienceSpot (IPVisitContext context) {Console. writeLine? "); Context. currentState = new royal garden State () ;}/// <summary> // royal garden State behavior instance /// </summary> public class Royal Garden State: ISpotState {public void ExperienceSpot (IPVisitContext context) {Console. writeLine ("Royal Garden: The Royal Garden is really not as beautiful as you think, it is very lost. "); context. currentState = new State () ;}/// <summary> // /</summary> public class State: ISpotState {public void ExperienceSpot (IPVisitContext context) {Console. writeLine. "); context. currentState = new Jiulong wall State () ;}/// <summary> // Jiulong wall State behavior instance /// </summary> public class Jiulong wall State: ISpotState {public void ExperienceSpot (IPVisitContext context) {Console. writeLine. ") ;}} main function call: IPVisitContext context = new IPVisitContext (new Taihe temple State (); // visit context from Taihe Temple. experienceSpot (); context. experienceS Pot (); context. experienceSpot (); context. experienceSpot (); context. experienceSpot (); run the result as follows: 1 Taihe temple: the living room of the Emperor's home is really magnificent. I was excited about the large ceremony! 2 Qian Qing palace: Is it because the Yangxin Temple is very close to the hougong and Yong Zheng was moved in? 3. Royal Garden: The Royal Garden is really not as beautiful as you think. It is very lost. 4: I can't see the living environment of my colleagues in that year. 5. Jiulong wall: mixed up in the palace. The game won't face people on the 16th card. 6. Press any key to continue... so after talking about the benefits of the half-day state model, how can we flexibly expand the state? For example, we now need to join a scenic spot to visit shenwumen, that is, the exit of the palace. We only need to take the following steps: Step 1: Add a kind of shenwumen status. The Code is as follows: /// <summary> /// Wu Men State behavior instance /// </summary> public class Wu Men State: ISpotState {public void ExperienceSpot (IPVisitContext context) {Console. writeLine ("shenwumen: It's exhausting to finally finish the Palace Museum. ") ;}} Step 2: link the next state of the Kowloon wall to the martial arts status. The Code is as follows: /// <summary> // Jiulong wall status behavior instance // </summary> public class Jiulong wall State: ISpotState {public void ExperienceSpot (IPVisitContext context) {Console. writeLine. "); context. currentState = new shenwumen State ();}}