2017-09-11 26 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); 

感谢所有帮助过我的人!