【学习贴】设计模式——责任链模式

【责任链模式】这个设计模式貌似蛮好理解的,想象一下富土康三号流水线质检员张全蛋~,一部IPhone要经过层层质检,合格后方能出厂,每一道工序的质检员都有一个编号,通过了就贴一个带编号的质检通过标签,以后这块出了问题,很容易就找到是谁质检时候在划水。
贴代码:(不好意思,这次质检主机)

using UnityEngine;
/// <summary>
/// 设计模式——责任链模式
/// </summary>
public class DesignMode_Responsibility : MonoBehaviour {

	// Use this for initialization
	void Start () {
        //质检100台电脑吧
        for (int i = 0; i < 100; i++)
        {
            Computer computer = new Computer();
            TestOne one = new TestOne();
            one.TestComputer(computer);
        }
    }
}
/// <summary>
/// 一台新主机出厂要经过3道质检
/// </summary>
public class Computer
{
    public bool isTestOne;
    public bool isTestTwo;
    public bool isTestThree;
    public void NextTest(Test test)
    {
        test.TestComputer(this);
    }
    /// <summary>
    /// 每台电脑出厂前每道质检都可能出问题,用随机布尔值来标识是否通过某道质检
    /// </summary>
    public Computer()
    {
        isTestOne = Random.value > 0.5f;
        isTestTwo = Random.value > 0.5f;
        isTestThree = Random.value > 0.5f;

    }
}
/// <summary>
/// 质检基类
/// </summary>
public abstract class Test
{
    public abstract void TestComputer(Computer computer);
}
public class TestOne : Test
{
    public override void TestComputer(Computer computer)
    {
        Debug.Log("1号质检员对电脑进行质检");
        if (computer.isTestOne)
        {
            Debug.Log("1号质检通过,送往2号质检");
            computer.NextTest(new TestTwo());
        }
        else
        {
            Debug.Log("1号质检不通过,不可以出厂");
        }
    }
}
public class TestTwo : Test
{
    public override void TestComputer(Computer computer)
    {
        Debug.Log("2号质检员对电脑进行质检");
        if (computer.isTestOne)
        {
            Debug.Log("2号质检通过,送往3号质检");
            computer.NextTest(new TestThree());
        }
        else
        {
            Debug.Log("2号质检不通过,不可以出厂");
        }
    }
}
public class TestThree : Test
{
    public override void TestComputer(Computer computer)
    {
        Debug.Log("3号质检员对电脑进行质检");
        if (computer.isTestOne)
        {
            Debug.Log("3号质检通过,可以出厂");
        }
        else
        {
            Debug.Log("3号质检不通过,不可以出厂");
        }
    }
}

运行截图(这个随机结果还是比较均匀的):
【学习贴】设计模式——责任链模式