买了九妹已经快有一年了,从经历了android2.3.5到深度优化的flyme1.1.3,风风雨雨,伴随着我一起走过。而我对魅族手机的设计也颇为留意。魅族是一个学习型的团队,很为用户考虑。今天要讲的魅族闹钟的新功能——事件定时器。以前看到了,只感觉很实用,近段时间在复习设计模式的时候,突然发现,魅族的这个新功能,尽然就是状态模式的真实实例。
为了让了解的和不了解的状态模式的同行们都看得懂我接下来说的,我先简要的介绍一下这个状态模式:
### 状态模式 State
状态模式允许一个对象在其内部状态改变时改变它的行为,让对象看起来似乎修改了它的类。
状态模式就是把系统的多个状态分割开来,分布到State的子类中,消除了庞大的分支语句,减少了耦合,同时也很容易增加新的状态的和转换。
这就相当于一个Schedule或School Timetable一样,时间的改变,我们的行程或课程也随之改变。
![](https://box.kancloud.cn/2016-01-14_56970cf16523f.gif)
今天借助M9的新功能,跟大家来实验一下用状态模式做一次清蒸鲤鱼,让你在学习设计模式的时候,还另有收获。别的不多说,先上图:
![](https://box.kancloud.cn/2016-01-14_56970cf2c171b.png)
其中状态有5种:PrepareTime,BoillingState,SteamshState,TurnOffFishState,SaucingState,对应操作的五个步骤。
![](https://box.kancloud.cn/2016-01-14_56970cf2d3605.png)
![](https://box.kancloud.cn/2016-01-14_56970cf2e7908.png)
~~~
/// <summary>
/// 制作清蒸鱼
/// </summary>
public class SteamFish
{
private State current;//存储当前状态
public SteamFish()
{
current = new PrepareState();
}
//总计时
private int timecount;
//记录当前操作用时
private int time = 0;
public int TimeCount
{
get { return timecount; }
set
{
//记录当前操作用时
time = timecount;
timecount = value;
}
}
//任务完成标识
private bool finish = false;
public bool TaskFinished
{
get { return finish; }
set { finish = value; }
}
//切换状态
public void SetState(State s)
{
current = s;
}
//清蒸操作
public void SteamFishes()
{
//继续操作
current.Steam(this,time);
}
}
~~~
~~~
/// <summary>
/// 抽象状态
/// </summary>
public abstract class State
{
//清蒸鱼
public abstract void Steam(SteamFish sf,int time);
}
~~~
~~~
/// <summary>
/// 准备状态
/// </summary>
public class PrepareState : State
{
private static int statictime;//本状态累积所用时间(秒)
public override void Steam(SteamFish sf, int time)
{
statictime += (sf.TimeCount - time);//计算本状态累积所用时间(秒)
if (sf.TimeCount <= 60)
{
if (statictime == (sf.TimeCount - time))
{
Console.WriteLine("\r【清蒸鱼菜谱教程】\n\n①.将少许姜、盐、麻油放入鱼腹。" + "\t\t" + "所需时间 1分0秒 ,已用去 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
else
{
Console.WriteLine("\t\t\t\t\t亲,请稍等 ,已维持该状态 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
}
else
{
statictime = 0;
//下一步操作是:烧水
sf.SetState(new BoillingState());
sf.SteamFishes();
}
}
}
~~~
~~~
/// <summary>
/// 烧水状态
/// </summary>
public class BoillingState:State
{
private static int statictime;//本状态累积所用时间(秒)
public override void Steam(SteamFish sf, int time)
{
statictime += (sf.TimeCount - time);//计算本状态累积所用时间(秒)
if (sf.TimeCount <= 150)
{
if (statictime == (sf.TimeCount - time))
{
Console.WriteLine("②.往锅里放入适量的水并烧开。\t\t所需时间 1分30秒,已用去 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
else
{
Console.WriteLine("\t\t\t\t\t亲,请稍等 ,已维持该状态 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
}
else
{
statictime = 0;
//下一步操作是:煮鱼
sf.SetState(new SteamFishState());
sf.SteamFishes();
}
}
}
~~~
~~~
/// <summary>
/// 蒸鱼状态
/// </summary>
class SteamFishState:State
{
private static int statictime;//本状态累积所用时间(秒)
public override void Steam(SteamFish sf, int time)
{
statictime += (sf.TimeCount - time);//计算本状态累积所用时间(秒)
if (sf.TimeCount <= 570)
{
if (statictime == (sf.TimeCount - time))
{
Console.WriteLine("③.把鱼放入锅中,合上盖子。" + "\t\t" + "所需时间 7分0秒,已用去 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
else
{
Console.WriteLine("\t\t\t\t\t亲,请稍等 ,已维持该状态 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
}
else
{
statictime = 0;
//下一步操作是:关火
sf.SetState(new TurnOffState());
sf.SteamFishes();
}
}
}
~~~
~~~
/// <summary>
/// 关火状态
/// </summary>
class TurnOffState:State
{
private static int statictime;//本状态累积所用时间(秒)
public override void Steam(SteamFish sf,int time)
{
statictime += (sf.TimeCount - time);//计算本状态累积所用时间(秒)
if (sf.TimeCount <= 870)
{
if (statictime == (sf.TimeCount - time))
{
Console.WriteLine("④.关火,别打开盖!" + "\t\t\t" + "所需时间 5分0秒,已用去 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
else
{
Console.WriteLine("\t\t\t\t\t亲,请稍等 ,已维持该状态 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
}
else
{
statictime = 0;
//下一步操作是:烧水
sf.SetState(new SaucingState());
sf.SteamFishes();
}
}
}
~~~
~~~
/// <summary>
/// 调料状态
/// </summary>
class SaucingState:State
{
private static int statictime;//本状态累积所用时间(秒)
public override void Steam(SteamFish sf, int time)
{
statictime += (sf.TimeCount - time);//计算本状态累积所用时间(秒)
if (sf.TimeCount <= 880)
{
if (statictime == (sf.TimeCount - time))
{
Console.WriteLine("⑤.开盖,调料淋遍鱼身!" + "\t\t\t" + "所需时间 0分10秒,已用去 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
else
{
Console.WriteLine("\t\t\t\t\t亲,请稍等 ,已维持该状态 {0}分{1}秒。\n", statictime / 60, statictime % 60);
}
}
else
{
sf.TaskFinished = true;
Console.WriteLine("开抢了,再不抢就没了!");
}
}
}
~~~
客户端代码:
~~~
static void Main(string[] args)
{
//【清蒸鱼操作教程】
SteamFish sf = new SteamFish();
sf.TimeCount = 60;
sf.SteamFishes();
sf.TimeCount = 100;
sf.SteamFishes();
sf.TimeCount = 150;
sf.SteamFishes();
sf.TimeCount = 570;
sf.SteamFishes();
sf.TimeCount = 600;
sf.SteamFishes();
sf.TimeCount = 870;
sf.SteamFishes();
sf.TimeCount = 880;
sf.SteamFishes();
sf.TimeCount = 1000;
sf.SteamFishes();
Console.Read();
}
~~~
效果:
![](https://box.kancloud.cn/2016-01-14_56970cf30c965.png)