Behavioral Mode (v) state mode

Source: Internet
Author: User

A summary of a sentence

Allows an object to change the behavior of its objects internally. Make the object look like a class that has changed it.

Plot

Love will change a person's attitude, Xiao Ming and the goddess today recalled the attitude of love, accidentally mentioned. Before the fall in love, Xiaoming invited the Goddess to eat, date, the goddess are refused.

Now please goddess dinner, date, the goddess lack of pleasure to receive. This is the difference between the two states, resulting in different results.

Then we use state mode to construct the above scene. Make different reactions to the goddess in different states.

Task One: Create an abstract class of love

We create abstract classes in the love period and define reaction methods. We will inherit this abstract class later and react differently.

 Public Abstract classLove {//Status         PublicAccount Account {Get;Set; } //Goodwill degree         Public intGoodWill {Get;Set; } //Eat         Public Abstract voidEat (); //Dating         Public Abstract voidYuehui (); //increase the degree of goodwill         Public Abstract voidAddgoodwill (intgood); //reduce the level of goodwill         Public Abstract voidDelgoodwill (intgood); }

Our account is the State class, and the different methods are made here.

Then we look directly at the account class code

 Public classAccount { PublicLove Love {Get;Set; }  PublicAccount (intgood) { Love=NewFriend (0, This); }         Public voidEat () {love.        Eat (); }         Public voidYuehui () {love.        Yuehui (); }         Public voidDelgoodwill (intgood) {Love.        Delgoodwill (good); }    }

We let them two people, starting with friends. So constructs the words, initializes the friend. and define different methods to invoke the abstract method of the goddess response.

If you inherit this, you just need to change the corresponding abstract method.

Task two: Creating a friend's reaction

We first create a friend's reaction, directly on the code.

 Public classFriend:love { PublicFriend (intgood, account Love) {             This. GoodWill =good;  This. Account =Love ; }         Public Override voidAddgoodwill (intgood) {             This. GoodWill + =good;        Ask (); }         Public Override voidDelgoodwill (intgood) {             This. GoodWill-=good;        Ask (); }         Public Override voidEat () {Addgoodwill (1); Console.WriteLine ("We're just friends, I'm not going.");        Ask (); }         Public Override voidYuehui () {Addgoodwill (1); Console.WriteLine ("We're just friends, I'm not going.");        Ask (); }        //ask if you have a good opinion         Public voidAsk () {if( This. GoodWill >5) {Account.love=NewLovers (); }        }    }

We define a goodwill mechanism so that he is more than 5 to become a lover. Less than or equal to 5, then every time you call out, do not go.

Task three: Create a lover's reaction

We create a lover's reaction. directly on the code.

 Public classLovers:love { Public Override voidAddgoodwill (intgood) {             This. GoodWill + =good;        Ask (); }         Public Override voidDelgoodwill (intgood) {             This. GoodWill-=good;        Ask (); }         Public Override voidEat () {Console.WriteLine ("We are lovers, I go"); }         Public Override voidYuehui () {Console.WriteLine ("We are lovers, I go"); }        //ask if you have a good opinion         Public voidAsk () {if( This. GoodWill <5) {Account.love=NewFriend ( This. GoodWill, This.            account); }        }    }

Thus, although the appearance has not changed, it is still a class. But actually, depending on the state, the implementation is different.

This is the state pattern. Let's finally call and look at the results.

Private Static voidMain (string[] args) {Console.WriteLine ("Two of people just met."); Account Account=NewAccount (0); Console.WriteLine ("about Dinner"); Account.            Eat (); Console.WriteLine ("go on and on, keep on eating.");  for(inti =0; I <5; i++) {account.            Eat (); } account.            Eat (); Account.            Yuehui ();        Console.ReadLine (); }

Summarize

Allows an object to change behavior on its own when its internal state changes. Let the calling module not be modified, but the execution results are different.

Common scenario: an object behaves differently in multiple states, and these States can be converted to each other.

Select key: whether these states often need to switch between different states at run time

Behavioral Mode (v) state 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.