vb.net中带有接口类的工厂方法模式 [英] Factory method pattern in vb.net with interface class

查看:94
本文介绍了vb.net中带有接口类的工厂方法模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想创建一个结构,其中有一个具有2个功能的源类:MySourceClass.GetData()作为Datatable和MySourceClass.GetSchema()作为Datatable.

当从我的代码中调用此函数时,它应该相应地进入接口类,并从用户选择的文件中读取数据.例如,如果用户选择一个excel文件,则在调用MySourceClass.GetData()时,它应读取excel文件对象类并获取数据,但是如果选择了CSV文件对象类,则在调用MySourceClass.GetData()时,它应读取CSV文件中的数据.

我想要实现的所有事情是,对SourceClass.GetData的调用将通过使用接口类读取适当的文件而去并获取数据表,而接口类规定了要读取的文件类型,而我不必传递MySourceClass,GetData()函数中的任何值,如FileName,FilePath,FileType,HasHeaderRow,Connection String等...,因为接口类已经具有所有这些信息...

我对此并不陌生,因此不胜感激,因此,我希望通过一些示例来说明如何实现这一目标....
这样的东西

Hello,

I want to create a structure where in I have a source class which has got 2 funtions: MySourceClass.GetData() As Datatable and MySourceClass.GetSchema() As Datatable.

When this functions are called from my code it should accordingly go to the interface class and read data from the user selected file. For example, if the user selects an excel file then when calling MySourceClass.GetData() it should read the excel file object class and get the data however if the CSV file object class is selected then when calling MySourceClass.GetData() it should read data from CSV file.

All in all what I want to achieve is that a call to SourceClass.GetData would go and fetch the datatable by reading the appropriate file using the interface class and the interface class dictates which type of file is to be read and I dont have to pass any values into the MySourceClass,GetData() function like FileName, FilePath, FileType, HasHeaderRow, Connection String etc... as the interface class would already have all that information...

I am quite new and thick to this and would therefore appreciate some kind of example to explain how can I achieve this....
Something like this

Public Interface ReadFile    
        Function GetData() As Datatable    
End Interface

Class ReadCsV    
      implements ReadFileEnd Class

Class ReadExcel    
      implements ReadFileEnd Class

Class MySource    
    Public Function GetData() as Datatable
          ''This would read the interface and 
          ''decide wether to read from CSV file or
          ''from excel file and accordingly returm the datatable
End Class



进行整个练习的原因是,我最终要实现的是用户可以选择多个源,例如CSV文件,excel文件,定界文件,数据库表的组合,并且我允许他们将这些数据映射到现有数据库表,将文件中的数据保存到该表中.

因此,创建一个对象结构,该对象结构具有最顶层的Import Job,在其中包含一个Import Task Collection对象,该对象具有Import任务对象,其中包含Source类,Field Mapping Collection类,Destination表详细信息类.
因此,当我执行ImportJob.ImportTaskCollection(i).ImportTask.DataSource.GetData()时,它将读取相应的文件并返回数据表.

通过阅读一点,我了解到ReadCsv应该具有GetData函数,该函数查看CSV文件并将数据加载到DataTable中,然后返回该数据表.它会知道路径,因为我将通过构造函数传递路径.

同样,ReadExcel将具有一个GetData函数,该函数返回一个DataTable,该数据表是通过在传递到构造函数的路径中找到的Excel文件中进行读取而填充的.
MySource类将包含实现ReadFile接口的类(ReadCsv或ReadExcel)的实例.从MySource.GetData,您可以在我有引用的ReadFile变量上调用GetData函数.我将不得不根据自己喜欢的条件将实例设置为ReadCsv或ReadExcel.

从理论上知道所有这些都很好,但是我如何将其放入代码中却是我无法理解的...

有人可以给我发一个代码示例让我正确理解吗??

谢谢.



The reason for this whole exercise is because all I want to finally achieve is that user can select multiple sources like a combination of CSV file, excel file, delimited file, database tables and I allow them to map this data to the existing database tables and save the data from the file in that table.

Therefore creating a object structure which has Import Job at the topmost level, within it a Import Task Collection object which has import task object containg Source Class, Field Mapping Collection Class, Destination table details class.
Therefore what would happen is when i do ImportJob.ImportTaskCollection(i).ImportTask.DataSource.GetData() it will read the appropriate file and return the datatable

From reading a bit I understand that the ReadCsv should have a function, GetData, that looks at a CSV file and loads the data into a DataTable, which it returns. It will know the path, because I will pass in the path via the constructor.

Similarly, ReadExcel will have a GetData function that returns a DataTable that was populated by reading in from an Excel file found at the path passed into the constructor.

The MySource class would hold an instance of a class (ReadCsv or ReadExcel) that implements the ReadFile interface. From MySource.GetData, you would call the GetData function on the ReadFile variable I have a reference to. I will have to set that instance to either the ReadCsv or ReadExcel earlier on based on whatever conditions I like.

Knowing all this is fine theoretically but how do i put it in code is what I am failing to understand...

Can anybody please post me a code example for me to understand this properly???

Thanks.

推荐答案

访问DoFactory.com或参阅 [ ^ ]示例.它必须有帮助.
Visit DoFactory.com or see this[^] example. It must help.


这里是一个.这里有很多关于CP的文章(与Factory模式有关)-进行搜索,您会发现一些不错的结果.
Here is one. There are plenty of articles (related to the Factory pattern) on CP here - do a search and you should find some good results.


这篇关于vb.net中带有接口类的工厂方法模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆