zoukankan      html  css  js  c++  java
  • State Design Pattern 状态设计模式

    设置好内部状态,然后依据不同的函数作为行为模式,进行状态转换。

    有点像Finite Automata算法,两者的思想是一样的。

    会Finite Automata,那么这个设计模式就非常easy了。


    #pragma once
    #include <stdlib.h>
    #include <math.h>
    #include <random>
    #include <time.h>
    
    enum STATES
    {
    	FULLY_RENTED, WAITING, GOT_APPLICATION, APARTMENT_RENTED
    };
    
    class RentalMethods
    {
    	STATES state;
    	int numberOfAppartments;
    public:
    	RentalMethods(int n):state(WAITING), numberOfAppartments(n)
    	{
    		srand((unsigned)time(NULL));
    	}
    	void getApplication()
    	{
    		switch (state)
    		{
    		case FULLY_RENTED:
    			puts("Sorry, we are fully rented.");
    			break;
    		case WAITING:
    			state = GOT_APPLICATION;
    			puts("Thanks for the application.");
    			break;
    		case GOT_APPLICATION:
    			puts("We already got your application.");
    			break;
    		case APARTMENT_RENTED:
    			puts("Hang on, we are renting you an apartment.");
    			break;
    		}
    	}
    
    	void checkApplication()
    	{
    		bool yesOrNo = rand() % 2;
    
    		switch (state)
    		{
    		case FULLY_RENTED:
    			puts("Sorry, we are fully rented.");
    			break;
    		case WAITING:
    			puts("You have to submit an application.");
    			break;
    		case GOT_APPLICATION:
    			{
    				if (yesOrNo && numberOfAppartments > 0)
    				{
    					puts("Congratulations, you were approved.");
    					state = APARTMENT_RENTED;
    					rentApartment();
    				}
    				else
    				{
    					puts("Sorry, you were not approved.");
    					state = WAITING;
    				}
    			}
    			break;
    		case APARTMENT_RENTED:
    			puts("Hang on, we are renting you an apartment.");
    			break;
    		}
    	}
    
    	void rentApartment()
    	{
    		switch (state)
    		{
    		case FULLY_RENTED:
    			puts("Sorry, we are fully rented.");
    			break;
    		case WAITING:
    			puts("You have to submit an application.");
    			break;
    		case GOT_APPLICATION:
    			puts("You must have your application checked.");
    			break;
    		case APARTMENT_RENTED:
    			puts("Renting you an apartment...");
    			numberOfAppartments--;
    			dispenseKeys();
    			break;
    		}
    	}
    
    	void dispenseKeys()
    	{
    		switch (state)
    		{
    		case FULLY_RENTED:
    			puts("Sorry, we are fully rented.");
    			break;
    		case WAITING:
    			puts("You have to submit an application.");
    			break;
    		case GOT_APPLICATION:
    			puts("You must have your application checked.");
    			break;
    		case APARTMENT_RENTED:
    			puts("Here are your key!");
    			state = WAITING;
    			break;
    		}
    	}
    };
    
    void States_Run()
    {
    	RentalMethods rentalMethods(9);
    
    	rentalMethods.getApplication();
    	rentalMethods.checkApplication();
    }

    执行有两种随机结果:


    2




  • 相关阅读:
    [Swift]LeetCode1035.不相交的线 | Uncrossed Lines
    [Swift]LeetCode1034.边框着色 | Coloring A Border
    [Swift]LeetCode1033. 移动石子直到连续 | Moving Stones Until Consecutive
    [Swift]美人征婚问题
    [Swift]动态变化顶部状态栏(statusBar)的颜色
    [Swift-2019力扣杯春季决赛]4. 有效子数组的数目
    [Swift-2019力扣杯春季决赛]3. 最长重复子串
    [Swift-2019力扣杯春季决赛]2. 按字典序排列最小的等效字符串
    转 ORA-12638: 身份证明检索失败
    转 构建镜像
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3899532.html
Copyright © 2011-2022 走看看