Which template to use in this case (interface implementation)

I have a data set that can be created in several ways, one of them by parsing some text, the other way from CSV format, the third way by importing from the database.

This is currently the interface:

 public interface IMyDataMaker
 {
       public MyDataSet MakeData();
 }

and this is the text parser class:

 public class MyDataSetTextParser : IMyDataMaker
 {
       private readonly string textToParse;
       public MyDataSetTextParser(string text)
       {
            textToParse = text;
       }

       public MyDataSet MakeDate()
       {
            // parse the text and return the dataset
       }
 }

parser csv is closer to a text parser, this is a database class:

 public class DbMyDataSetMaker : IMyDataMaker
 {
       private readonly SqlConnection connection;
       public DbMyDataSetMaker(SqlConnection sqlConn)
       {
            connection = sqlConn;
       }

       public MyDataSet MakeDate()
       {
            // connect to the db and make the dataset
       }
 }

Is this template used correctly in this case?

+4
source share
4 answers

Yes, this is absolutely the right way to do this. Use the constructors to separate the connection information for each subtype and end up with a common “read” method, just like you.

+2
source

, . Strategy .

, .

+3

. - , - . /, , 38- -. IMyDataMaker, , 28- . "" .

+1

, , -. - .

.

public abstract class MyDataMaker
{
   public abstract MyDataSet MakeData();
}

. . , , ?

1 , , , . . .

2 , , , , . MyDataMaker. , , , , .

So which one is more appropriate when it is very subjective and requires complete clarity regarding the goals.

And yes, if you plan to allow the structure to join the side, then the abstract option is absent because the structure is not inherited, so the interface is the only option.

+1
source

All Articles