Step by step. NET Design Mode learning notes 8. State (State Mode)

Source: Internet
Author: User

Overview

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

The State mode mainly solves the problem when the conditional expressions that control the status of an object are too complex. Transferring the status judgment logic to a series of classes that indicate different states can simplify the complicated judgment logic.

When an object's behavior depends on its state, and it must change its behavior according to its state at runtime, you can consider using the state mode.

<Design Pattern> State mode structure

 

Example Graph

Description: there are 12 months in a year and four quarters. Each month belongs to one quarter. The weather information of this quarter is obtained based on the month. The State mode can be used.

Relationship diagram:

 

Code Design:

Create the IQuarter. cs interface first:

View sourceprint? 01 public interface IQuarter

02 {

03 // <summary>

04 // obtain the quarter by month

05 /// </summary>

06 // <param name = "Month"> </param>

07 /// <returns> </returns>

08 Quarter GetQuarter ();

09

10 /// <summary>

11 // climatic conditions

12 /// </summary>

13 /// <returns> </returns>

14 string Climate ();

15}

Create Quarter. cs again:

View sourceprint? 01 public abstract class Quarter: IQuarter

02 {

03 private int _ Month = 0;

04 public int Month

05 {

06 get

07 {

08 return _ Month;

09}

10 set

11 {

12 _ Month = value;

13}

14}

15 public Quarter (int month)

16 {

17 this. Month = month;

18}

19

20 # region IQuarter Member

21

22 public abstract Quarter GetQuarter ();

23

24 public abstract string Climate ();

25

26 # endregion

27

28

29}
View sourceprint? 1. Create Spring. cs again:
View sourceprint? 01 public class Spring: Quarter

02 {

03

04 private Quarter quarter;

05 public override string Climate ()

06 {

07 return "pleasant spring breeze, birds and flowers, is a good season for tourism. ";

08}

09

10 public override Quarter GetQuarter ()

11 {

12 if (Month> 3)

13 {

14 quarter = new Summer (Month );

15 return quarter. GetQuarter ();

16}

17 return new Spring (Month );

18}

19 public Spring (int month): base (month)

20 {

21

22}

23}

Create Summer. cs again:

View sourceprint? 01 public class Summer: Quarter

02 {

03 private Quarter quarter;

04 public override string Climate ()

05 {

06 return "the weather is hot in summer and the heat is difficult .";

07}

08 public override Quarter GetQuarter ()

09 {

10 if (Month <4)

11 {

12 quarter = new Spring (Month );

13 return quarter. GetQuarter ();

14}

15 if (Month> 6)

16 {

17 quarter = new Autumn (Month );

18 return quarter. GetQuarter ();

19}

20

21 return new Summer (this. Month );

22

23}

24 public Summer (int month): base (month)

25 {

26

27}

28}

Create Autumn. cs again:

Vie

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.