c# - 通常识别 C# 中的模式

标签 c# .net design-patterns c#-4.0

我有两个类(class)

public class ClassA
{
 public int ID { get; set; }
 public string Countries {get;set;}
 public string City { get;set; }
}

public class ClassB
{
 public int ID { get; set; }
 public string Countries {get;set;}
 public string Village{ get;set; }
}

这两个类在另一个类中

public class ComponentClass
{
   public List<ClassA> classAObj { get; set; }
   public List<ClassB> classBObj { get; set; }
}

ComponentClass 的数据来自第三方,其中 ClassAClassB 的数据在结构上相似。 “City” 在 ClassA 中的数据将以逗号分隔值 “Manchester,Sydney” 等与 Village 以及以逗号分隔值类似。

现在我在业务层构建一个自定义对象,我在其中迭代 ComponentClass 的每个属性并提取信息。

Main()
{
  ComponentClass[] c = //Data from 3rd party;
  foreach(var data in c)
  {
    Parent p = new Parent();


    if(data.classAObj.count > 0)
    {
      Star s = new Star();
      s.Area = "infinite";
      s.Color = "red";
      List<string> sep = data.City.Split(',').Select(string.Parse).ToList();    
      foreach(var b in sep)
      {
       TinyStar t = new TinyStar();
       t.smallD = b;
       s.Values.Add(t);
       }
      p.Curves.Add(s);
     }


    if(data.classBObj.count > 0)
    {
      Star s2 = new Star();
      s2.Area = "infinite";
      s2.Color = "red";
      List<string> sep = data.Village.Split(',').Select(string.Parse).ToList();    
      foreach(var b in sep)
      {
       TinyStar t = new TinyStar();
       t.smallD = b;
       s2.Values.Add(t);
       }
      p.Curves.Add(s);
     }

   }
}

在上面的代码中,除了属性名称“City”和“Village”之外,两个 if 语句执行完全相同的操作。我想通过使用我在理论上只知道的任何设计模式(可能是策略模式)或任何其他模式来简化这一过程。

这是我尝试过的:

public abstract class Base
{
  public int ID { get; set; }
  public string Countries {get;set;}
}

public class ClassA : Base
{

 public string City { get;set; }
}


public class ClassB : Base
{
 public string Village{ get;set; }
}    

我想让它成为一个通用的工厂方法,它将为我进行循环并构建对象以避免代码重复

public void CommonMethod(Base)
{
  // How do I differentiate the properties for looping 
}

最佳答案

如果目标是减少代码重复,可以将两个语句重构为一个 Action ,如下所示。

foreach(var data in c)
{
    Parent p = new Parent();

    Action<string> iAction = iString =>
    {
        Star s = new Star();
        s.Area = "infinite";
        s.Color = "red";
        List<string> sep = iString.Split(',').Select(string.Parse).ToList();

        foreach(var b in sep)
        {
            TinyStar t = new TinyStar();
            t.smallD = b;
            s.Values.Add(t);
        }
        p.Curves.Add(s);
    }

    if(data.classAObj.count > 0)
    {
        iAction(data.City);
    }

    if(data.classBObj.count > 0)
    {
        iAction(data.Village);
    }
}

关于c# - 通常识别 C# 中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50404067/

相关文章:

c# - ApiController 返回 504 - 服务器未返回此请求的完整响应。服务器返回 0 字节

c# - 使用 Environment.Is64BitProcess 从 c# 应用程序动态调用 32 位或 64 位 DLL

c# - 如何动态不呈现脚本标签?

.net - Entity Framework 4.1 - 非键列之间的关系

c# - 动态 C# 对象的设计模式

c# - 将 lambda 参数传递给 include 语句

.net - 使用 .NET 中的 RegEx 规范化 Windows 路径或 URI

.net - 如何在 Visual Studio 2008 中从现有 SQLite 数据库创建 Entity Framework 模型?

oop - 装饰器设计模式问题?

java - 策略设计模式中获取对象的类名