如何在UML序列图中描述C#yield return?

时间:2015-03-13 07:21:41

标签: c# uml yield

例如,

using System;
using System.Collections;

namespace ConsoleApplication
{
    class A : IEnumerable
    {
        B b;
        public A()
        {
            b = new B();
        }
        public IEnumerator GetEnumerator()
        {
            yield return b.FunA(0);
            yield return b.FunB(1);
            yield return b.FunC(2);
            yield return b.FunD(3);
            yield return b.FunE(4);
        }
    }

    class B
    {
        public int FunA(int x)
        {
            return x;
        }
        public int FunB(int x)
        {
            return x * 2;
        }
        public int FunC(int x)
        {
            return x * 3;
        }
        public int FunD(int x)
        {
            return x * 4;
        }
        public int FunE(int x)
        {
            return x * 5;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var a = new A();
            foreach(var number in a)
            {
                Console.WriteLine(number);
            }
            Console.ReadLine();
        }
    }
}

如何在序列图中描述该程序? 我认为它应该类似于下面的循环或alt部分。

              +------------+            +------------+      +------------+
              |  Program   |            |     A      |      |     B      |
              +-----+------+            +------+-----+      +------+-----+
                    |                          |                  |
                    |                          |                  |
                    |                          |                  |
                    |                          |                  |
     +-----+--------------------------------------------------------------+ 
     |loop |        |   [0]                    |                  |       |
     +-----+        |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     +--------------------------------------------------------------------+ 
     |              |   [1]                    |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     +--------------------------------------------------------------------+ 
     |              |   [2]                    |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     +--------------------------------------------------------------------+ 
     |              |   [3]                    |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     +--------------------------------------------------------------------+ 
     |              |   [4]                    |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     |              |                          |                  |       |
     +--------------------------------------------------------------------+ 
                    |                          |                  |         
                    |                          |                  |         

修改 我正在分析Unity3D插件的源代码。 IEnumerator和yield用于实现协程模型。协程用作线程的替代。因此,在序列图中显示协程逻辑的一部分是合理的。

1 个答案:

答案 0 :(得分:0)

对于序列图,您可以定义循环(while循环),只要涉及yield关键字执行,请参阅以下链接 internals of C# interators

我认为这个链接可以帮助您理解为什么收益率可​​以表示为循环。