2015-03-13 64 views
0

例如,如何在UML序列圖中描述C#收益率回報?

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用於實現協同模型。協程被用作線程的替代。因此,在序列圖中顯示協程的邏輯部分是合理的。

+2

個人而言,我不會。序列圖是IMHO更適合於對象之間交互的一系列步驟。對於我而言,這些步驟僅僅是重複同一個動作,並重復執行一系列值,這對我來說似乎並不有用。 – 2015-03-13 07:30:11

+0

@PeterDuniho也許最好說清楚,我正在閱讀一些在Unity3D中編寫的代碼。 IEnumerator和yield用於實現_coroutine_模型。 _coroutine_用作線程的替代方案。所以說說這些步驟只是我用@PeterDuniho去做的同一個動作 – zwcloud 2015-03-13 09:22:22

+1

的重複是不合適的。在大多數情況下,用圖形描述代碼沒有附加價值。 SD應該用於通信概述。代碼描述算法要好得多。將SD中的區域使用降到最低! – 2015-03-13 11:10:11

回答

0

對於序列圖,你可以定義循環(while循環),只要產量關鍵字的執行擔心,請參考以下鏈接 internals of C# interators

我認爲這個鏈接將幫助您理解爲什麼收益率可​​以表示作爲循環。