简体   繁体   English

使用稍微不同的方法签名标准化子类

[英]Standardize child classes with slightly different method signatures

Iam refactoring a project but finding difficulties.我正在重构一个项目,但发现困难。

The goal is to remove some switch cases from the code that are based on the ServiceType (see in code).目标是从基于ServiceType的代码中删除一些 switch case(参见代码)。

Some context一些上下文

ServiceA , ServiceB , etc. are all classes pretty similar that describe different services. ServiceAServiceB等都是描述不同服务的非常相似的类。 I would like to:我想:

  • let them implement an interface (let's say IService )让他们实现一个接口(假设IService
  • make a Factory that returns the right concrete ( ServiceXX : IService )创建一个返回正确混凝土的工厂( ServiceXX : IService

so that in the end I will be able to reduce the amount of switch cases.以便最终我能够减少开关盒的数量。

Here a switch that I would like to remove:这里有一个我想删除的开关:

foreach (Service s in PartnerServices)
{
    int min = 0;
    int max = 0;
                
    switch (s.Type)
    {
        case "ServiceA":
            min = ServiceA.GetMinPrice();
            max = ServiceA.GetMaxPrice();
            break;
    
        case "ServiceB":
            min = ServiceB.GetMinPrice();
            max = ServiceB.GetMaxPrice();
            break;
                    
        case "ServiceC":
            min = ServiceC.GetMinPrice();
            max = ServiceC.GetMaxPrice();
            break;

        case "ServiceD":
            min = ServiceD.GetMinPrice(StateID);
            max = ServiceD.GetMaxPrice(StateID);
            break;  
                    
        case "ServiceE":
            min = ServiceE.GetMinPrice(StateID, CustomerID);
            max = ServiceE.GetMaxPrice(StateID, CustomerID);
            break;                       
    }
    ...
}

The problem Iam facing is that the majority of GetMinPrice and GetMaxPrice implementations need 0 parameters, while a few needs parameters.我面临的问题是大多数GetMinPriceGetMaxPrice实现需要 0 个参数,而少数需要参数。

This is due to the fact that some services need external info to calculate the price (for example ServiceE , needs to know the StateID and the CustomerID cause the price can be customized for that two keys).这是因为某些服务需要外部信息来计算价格(例如ServiceE ,需要知道StateIDCustomerID因为可以为这两个键自定义价格)。

So while the classes belong to the same "root" the method signature is different.因此,虽然类属于同一个“根”,但方法签名是不同的。

How is this usually managed?这通常是如何管理的? Do you have any suggestion?你有什么建议吗?

Please consider that I cannot insert these parameters as properties of the relevant Service class because these are external infos and do not belong there.请注意,我无法将这些参数作为相关服务类的属性插入,因为这些是外部信息,不属于那里。

Thank you very much in advance.非常感谢您提前。

I might have had an idea that got me closer to the solution... What if i create a new class that will contain all the potential options needed to calculate the price and then pass it to the methods?我可能有一个让我更接近解决方案的想法......如果我创建一个新类,该类将包含计算价格所需的所有潜在选项,然后将其传递给方法呢?

Here a sample of the final code that explain what i mean:这是解释我的意思的最终代码示例:

public class PriceCalculationOptions
{
     public int StateID { get; set; }
     public int CustomerID { get; set; }
}

public interface IService
{
     int GetMinPrice(PriceCalculationOptions options);
     int GetMaxPrice(PriceCalculationOptions options);
}

public class ServiceA/B/C/D/E : IService 
{
     public int GetMinPrice(PriceCalculationOptions options)
     {
        //custom implementation using options
     }
     
     public int GetMaxPrice(PriceCalculationOptions options)
     {
        //custom implementation using options
     }

}

public class ServiceFactory
{
    public static IService CreateService(string ServiceType)
    {
        switch (ServiceType)
        {
            case "ServiceA":
                return new ServiceA();
                break;
        
            case "ServiceB":
                return new ServiceB();
                break;
                        
            ...                      
        }
    }
}

And so the initial foreach which contained the switch case becomes:因此包含 switch case 的初始 foreach 变为:

foreach (Service s in PartnerServices)
{
    int min = 0;
    int max = 0;
                
    IService service = ServiceFactory.CreateService(s.Type);
    PriceCalculationOptions priceCalculationOptions = new PriceCalculationOptions()
    {
        StateID=1,
        CustomerID = 20
    };
    
    min = service.GetMinPrice(priceCalculationOptions);
    max = service.GetMaxPrice(priceCalculationOptions);
                
    ...
}

What do you guys think?你们有什么感想? Do you see any downside?你看有什么缺点吗? To me it seems that it might work!在我看来它可能会起作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM