2017-09-11 58 views
0

我需要一些幫助構建流暢的界面(類包裝器),它允許我從數據庫中的曲線集(在myWell中)創建(並命名)2D雙重數組。理想的情況下,執行下面的語法(或類似的東西),當用戶可以創建一個二維double數組:數據列表的類包裝

Double [,] CurveDoubleName = CreateIPmathCurves.CreateCurve(comboBox1.Text).CurveName("NPHIL").Make() 

凡comboBox1的數據來自一個名爲frmMain WindowsForm。這是我到目前爲止:

namespace UserProgram 
{ 
     public class CreateIPmathCurve : frmMain, ICanNameCurve, ICanLocateCurve, ICanMakeCurve 
     { 
     private string _comboBoxName; 
     private string _CurveName; 
     private string _data; 

     // Private constructor - Only allow instantiated functions to create an IP math Curve 

     private CreateIPmathCurve() 
     { } 

     // Instantiating functions 

     public static ICanNameCurve CreateCurve() 
     { 
      return new CreateIPmathCurve(); 
     } 

     // Chaining functions 
     private CreateIPmathCurve(string data) { _data = data; } //private constructor prevents instantiation of your builder 

     public ICanLocateCurve SetCurveName(string CurveName) 
     { 
      _CurveName = CurveName; 
      return this; // this allows chaining. 
     } 

     public ICanMakeCurve SetComboBoxName(string comboBoxName) 
     { 
      _comboBoxName = comboBoxName; 
      return this; // this allows chaining. 
     } 

     // ending functions 

     public DataObject CreateCurveDoublArrayObj(string comboBoxName) 
     { 
      try 
      { 

       // User will need to populate comboBox.. example: comboBox1.text 
       char[] delimiter = { ':' }; 
       Curve1inText = comboBoxName; 

       string[] crvIn1 = Curve1inText.Split(delimiter); 

       string CurveSet = crvIn1[0]; 
       string Curve = crvIn1[1]; 

       ICurveSet InCurveSet = myWell.FindCurveSet(CurveSet); 
       ICurve InMyCurve = myWell.FindCurve(Curve); 

       if (InMyCurve == null) 
       { 
        MessageBox.Show("You need to input a curve"); 
       } 

       ILogReading[] In = InMyCurve.LogReadings.ToArray(); 

       double[,] CurveDouble = new double[In.Length, 2]; 

       int j = 0; 

       foreach (ILogReading reading in InMyCurve.LogReadings) 
       { 
        CurveDouble[j, 0] = reading.Depth; 
        CurveDouble[j, 1] = reading.Value; 
        j++; 
       } 

       return new DataObject(CurveDouble); 
      } 

      catch (Exception ex) 
      { 
       MessageBox.Show("Error building Double Array\n" + ex.Message + "\n" + ex.StackTrace); 
       return null; 
      } 

     } 

     public double[,] MakeCurve() 
     { 
      //this is where CurveName input ("NPHIL") should name the 2D double array listed from dataobject. 
      // I receive a "non-invocable member 'DataObject' cannot be used like a method" error.... 

      _CurveName = DataObject(_comboBoxName); 
      return this; 

      // I also receive a "cannot implicity convert type UserProgram.CreateIPmathCurve' to 'double [*,*]"... 
     } 
    } 

    // using interfaces to enforce Fluent Interface grammar -- can't make curve if you don't know the location of curve, etc... 
    public interface ICanNameCurve 
    { 
     ICanLocateCurve SetCurveName(string CurveName); 
    } 

    public interface ICanLocateCurve 
    { 
     ICanMakeCurve SetComboBoxName(string comboBoxName); 
    } 

    public interface ICanMakeCurve 
    { 
     DataObject CreateCurveDoublArrayObj(string comboBoxName); 
    } 

} 

你能幫我創建一個允許用戶命名數據對象的CurveName()函數嗎? (我嘗試了一個getter-setter訪問器,但我認爲我沒有正確地做,或者對它的工作原理缺乏強烈的概念性理解)

最後,你還可以幫我創建一個Make()函數把所有這些放在一起?

+1

這被稱爲[*流暢界面*](https://en.wikipedia.org/wiki/Fluent_interface)。對於初學者來說可能會有點太高級了。爲什麼不從一般的方法/屬性開始? – Sinatr

+0

你想達到什麼結果?雙數組只包含雙精度值。所以沒有辦法命名或找到你的曲線。 – Iqon

+0

DataObject CreateCurveDoubleArray從我的井數據庫(使用comboBox輸入)找到曲線並填充名爲CurveDouble ....的2D雙重數組....我想將這個新的2D雙重數組分配給名稱(由CurveName指定)。我希望語法簡單些,類似於上面的語法(CreateIPmathCurves.CreateCurve(comboBox1.Text).CurveName(「NPHIL」)。Make()) – endlessforms

回答

1

要構建一個流暢的API,您需要找到一種方法來聚合所有數據,直到您撥打Make

一個簡單的方法是編寫一個保存所有數據的Builder。該構建器爲每個可配置字段提供了setter方法。

這裏的關鍵是每個setter方法返回Builder對象,允許您鏈接調用。

public class FluentCurveBuilder 
{ 
    private string _name; 
    private string _data; 
    private string _color; 

    private FluentCurveBuilder(string data) { _data = data; } // private constructor prevents instantiation of your builder 

    public FluentCurveBuilder SetName(string name) 
    { 
     _name = name; 
     return this; // this allows chaining. 
    }  

    public FluentCurveBuilder SetColor(string color) 
    { 
     _color = color; 
     return this; // this allows chaining. 
    } 

    public static FluentCurveBuilder CreateCurve(string data) 
    { 
     return new FluentCurveBuilder(data); 
    } 

    public Curve Make() 
    { 
     // Creation logic goes here 
    } 
} 

您現在可以使用該構建器來配置您的曲線。

var curve = FluentCurveBuilder.CreateCurve("Data") 
           .SetName("Test") 
           .Make(); 

我們有一個靜態的CreateCurve方法,它返回一個新的構建器。所有其他方法只應將數據存儲在實例字段中並返回此構建器。

Make方法,您現在可以訪問所有以前的彙總數據,並且您可以構建您的Curve

+0

謝謝lqon,這已經證明可以幫助我更靠近朝着可行的解決方案邁進。我已經更新了我的問題,並且代碼(上面)可以查看並查看我失蹤的內容嗎? – endlessforms

0

我想我是在分析我的問題 - 最簡單的解決方案是創建一個返回二維的方法雙數組,然後指定一個名稱的對象時,我調用該函數...所以用這個:

public double [,] CreateIPmathCurve(string comboBoxName) 
    { 

     string CurveInText = ""; 
     char[] delimiter = { ':' }; 
     CurveInText = comboBoxName; 

     string[] crvIn = CurveInText.Split(delimiter); 

     string CurveSet = crvIn[0]; 
     string Curve = crvIn[1]; 

     ICurveSet InCurveSet = myWell.FindCurveSet(CurveSet); 
     ICurve InMyCurve = myWell.FindCurve(Curve); 

     if (InMyCurve == null) 
     { 
      MessageBox.Show("You need to input a curve"); 
     } 

     ILogReading[] In = InMyCurve.LogReadings.ToArray(); 

     double[,] CurveDouble = new double[In.Length, 2]; 

     int j = 0; 

     foreach (ILogReading reading in InMyCurve.LogReadings) 
     { 
      CurveDouble[j, 0] = reading.Depth; 
      CurveDouble[j, 1] = reading.Value; 
      j++; 
     } 

     return CurveDouble; 

允許我創建這個數組:

double[,] NPHIL = CreateIPmathCurve(comboBox1.Text); 

感謝所有幫助過我的人!