file - OOP 中处理不同文件类型的设计模式

标签 file oop design-patterns pattern-matching

我需要处理一大组文件,这些文件目前都已加载到列表上的内存中。

即:

List(FileClass) Files;

用途:

Reader.Files; //List of files of the type

文件类有一个属性来匹配每个 FileInfo 对象属性,即:名称、创建日期等。 还有一个类型为(LineNumber、Data)的行列表。

现在我需要创建一个逻辑来解释这些文件。它们都有不同的逻辑解释,并且将被加载到相应的业务对象上。

即:

Model model = new Model()

.emp => Process => Employee Class
.ord => Process => Order Class

model.AddObject(emp);
model.AddObject(ord);

我的问题是,解决此类问题的最佳设计模式是什么。

我能想到的是......像这样:

public ProcessFiles(List<Files> Files)
{

Model model = new Model()
var obj;

        foreach(file in Files)
        {
                    switch (File.GetExtension(file))
                    {
                        case "emp":
                           obj = BuildEmployee(file) //returns Employee class type
                            break;
                        case "ord":
                            obj = BuildOrder(file) //returns Order class type
                            break;
                    }

         model.AddObject(obj);

        }

}

有更好的方法来解决这个问题吗? 这个解决方案对我来说看起来是程序化的,是否有更好的面向对象方法?

干杯

更新:

我遇到了一些解决此问题的选项:

1)- 使用部分类来分离关注点。 我有一个数据模型,我不想混合文件处理、数据库使用等(单一职责)

数据模型:

public partial class Employee 
{ 
   public int EmployeeID; 
   public string FirstName; 
   public string LastName; 
   public decimal Salary; 
}

解释器/文件解析器:

该分部类定义了解析 .emp 文件的逻辑。

         // This portion of the partial class to separate Data Model 
          from File processing
         public partial class Employee 
         { 
            public void ProcessFile(string FileName)
            { 
               //Do processing
            } 

            ... 
         }

解释器对象

 public class Interpreter : IInterpreter
     {

     foreach(file in Files)
      {

       switch (fileExtension)
        {
          case .emp
             Employee obj= new Employee();
          case .ord
             Order obj = new Order(file);
        }


        obj.ProcessFile(File)
        Model.AddObject(emp)
      }

     }

2)-也许使用某种工厂模式... 输入是具有扩展名类型的文件。 这驱动了要创建的对象的类型(即:员工、订单等)以及解析该文件的逻辑。有什么想法吗?

最佳答案

嗯,您似乎想根据文件类型改变处理行为。行为和类型是关键字。任何行为模式符合您的要求吗?

或者对象创建是由输入文件类型驱动的?那么创作和打字就成为重要的关键词。

您可能想了解一下策略和工厂方法模式。

这是书中的一些内容Refactoring to Patterns :

The overuse of patterns tends to result from being patterns happy. We are patterns happy when we become so enamored of patterns that we simply must use them in our code. A patterns-happy programmer may work hard to use patterns on a system just to get the experience of implementing them or maybe to gain a reputation for writing really good, complex code.

A programmer named Jason Tiscione, writing on SlashDot (see http://developers.slashdot.org/comments.pl?sid=33602&cid=3636102), perfectly caricatured patterns-happy code with the following version of Hello World. ..... It is perhaps impossible to avoid being patterns happy on the road to learning patterns. In fact, most of us learn by making mistakes. I've been patterns happy on more than one occasion.

The true joy of patterns comes from using them wisely.

关于file - OOP 中处理不同文件类型的设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25008149/

相关文章:

c++ - 这种方法会涉及内存重新分配从而影响其效率吗?

python - sys.stdin 读什么?

c# - 将 DTO 传递给服务层

javascript - 在创建单例实例时传递选项的 TypeScript 模式

c# - 如何比较 2 个 .csv 文件并创建一个包含两个 csv 文件的部分的新 .csv?

php - 使用 PHP 替换以前的图像并向服务器添加新图像

c++ - 多重继承方便吗?

python - sys.exit(0) 是退出/终止 python 线程的有效方法吗?

php - 需要面向对象设计的建议 : a collection of items

java - 在 Java 中使用策略模式的电子邮件程序