You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于参数为方法设置不同入口点以遵循DRY原则

基于参数控制方法执行入口,遵循DRY原则的实现方案

问题背景

我有一个SetDisplayCollection方法,无需每次运行全部代码,希望根据场景仅执行部分代码。为遵循DRY(Don't Repeat Yourself)原则,如何基于传入参数为该方法设置不同的执行入口?

原代码示例:

public void SetDisplayCollection(string input)
{
    //SWITCH:
    //Input 1 entry Point
    ChosenSite = SiteCollection.Where(x => x.Guid == chosenConcept.SiteGuid && x.Name == chosenConcept.SiteName).FirstOrDefault();
    //Input 2 entry point
    BuildingDisplayCollection = new ObservableCollection<BuildingConcept>(BuildingCollection.Where(x => x.RelatingName == ChosenSite.Name).ToList());
    ChosenBuilding = BuildingDisplayCollection.Where(x => x.Guid == chosenConcept.BuildingGuid && x.Name == chosenConcept.BuildingName).FirstOrDefault();
    //Input 3 entry point
    BuildingStoreyDisplayCollection = new ObservableCollection<BuildingStoreyConcept>(BuildingStoreyCollection.Where(x => x.RelatingName == ChosenBuilding.Name).ToList());
    ChosenBuildingStorey = BuildingStoreyDisplayCollection.Where(x => x.Name == chosenConcept.BuildingStoreyName).FirstOrDefault();
    //default no entry
}

期望的三种执行场景:

  • 运行全部代码
  • 运行后2/3代码(跳过开头部分)
  • 运行最后1/3代码(跳过前2/3部分)

解决方案:拆分逻辑+参数驱动执行

核心思路是把原方法里的三个独立逻辑块拆成单独的私有方法,通过传入的参数决定调用哪些方法,既避免重复代码,又能灵活控制执行流程。

步骤1:拆分独立逻辑块

把每个执行段封装成单一职责的私有方法,确保每个逻辑只写一次:

private void LoadChosenSite()
{
    ChosenSite = SiteCollection.Where(x => x.Guid == chosenConcept.SiteGuid && x.Name == chosenConcept.SiteName).FirstOrDefault();
}

private void LoadBuildingCollections()
{
    BuildingDisplayCollection = new ObservableCollection<BuildingConcept>(BuildingCollection.Where(x => x.RelatingName == ChosenSite.Name).ToList());
    ChosenBuilding = BuildingDisplayCollection.Where(x => x.Guid == chosenConcept.BuildingGuid && x.Name == chosenConcept.BuildingName).FirstOrDefault();
}

private void LoadBuildingStoreyCollections()
{
    BuildingStoreyDisplayCollection = new ObservableCollection<BuildingStoreyConcept>(BuildingStoreyCollection.Where(x => x.RelatingName == ChosenBuilding.Name).ToList());
    ChosenBuildingStorey = BuildingStoreyDisplayCollection.Where(x => x.Name == chosenConcept.BuildingStoreyName).FirstOrDefault();
}

步骤2:重构SetDisplayCollection方法,根据参数控制执行

用参数判断要执行的逻辑组合,示例先用字符串匹配,后续可以优化为枚举:

public void SetDisplayCollection(string input)
{
    switch(input)
    {
        case "Full": // 运行全部代码
            LoadChosenSite();
            LoadBuildingCollections();
            LoadBuildingStoreyCollections();
            break;
        case "BuildingOnly": // 运行后2/3代码
            LoadBuildingCollections();
            LoadBuildingStoreyCollections();
            break;
        case "StoreyOnly": // 运行最后1/3代码
            LoadBuildingStoreyCollections();
            break;
        default: // 默认不执行任何逻辑
            break;
    }
}

优化建议:用枚举替代字符串参数

为了避免字符串拼写错误,建议定义枚举规范输入参数:

public enum ExecutionScope
{
    Full,
    BuildingOnly,
    StoreyOnly,
    None
}

public void SetDisplayCollection(ExecutionScope scope)
{
    switch(scope)
    {
        case ExecutionScope.Full:
            LoadChosenSite();
            LoadBuildingCollections();
            LoadBuildingStoreyCollections();
            break;
        case ExecutionScope.BuildingOnly:
            LoadBuildingCollections();
            LoadBuildingStoreyCollections();
            break;
        case ExecutionScope.StoreyOnly:
            LoadBuildingStoreyCollections();
            break;
        default:
            break;
    }
}

为什么符合DRY原则?

每个业务逻辑块(加载站点、加载建筑、加载楼层)只实现一次,不同场景只是组合调用这些方法,没有重复代码。后续如果需要修改某段逻辑,只需要修改对应的私有方法即可,所有调用该逻辑的场景都会同步更新。

内容的提问来源于stack exchange,提问作者Ryan Daley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 13:55:16