无需多态/继承与大量分支语句,实现分类与算法匹配
Got it, let's break this down. You need a way to link product categories like Stool or Rug to their specific parameter-calculation functions (e.g., GetSizeForStool()) without relying on switch/if statements or inheritance/polymorphism. The key here is using first-class functions mapped to product type strings—this directly solves your "classification to algorithm matching" need without any branching logic.
How It Works
The core idea is to create a dictionary (or hash map) where each key is your product type string, and the corresponding value is the function you want to execute for that type. When you get an input product type, you just look up the function in the map and run it—no conditionals required.
Example Implementation (Python)
# Define your parameter calculation functions for each product type def get_size_for_stool(): # Add your actual stool size logic here return "Stool dimensions: 40cm x 40cm x 50cm" def get_size_for_rug(): # Add your actual rug size logic here return "Rug dimensions: 180cm x 240cm" # Create the mapping: product type string → corresponding function product_calculators = { "Stool": get_size_for_stool, "Rug": get_size_for_rug } # Usage: Get the product type from your input, then execute the mapped function selected_product = "Stool" product_size = product_calculators[selected_product]() print(product_size) # Output: Stool dimensions: 40cm x 40cm x 50cm
Example Implementation (C#)
If you're working in a statically typed language like C#, use delegates to achieve the same result:
// Define a delegate that matches the signature of your calculation functions public delegate string SizeCalculator(); // Individual calculation methods public static string GetSizeForStool() { return "Stool dimensions: 40cm x 40cm x 50cm"; } public static string GetSizeForRug() { return "Rug dimensions: 180cm x 240cm"; } // Create the type-to-function map private static readonly Dictionary<string, SizeCalculator> _productCalculators = new() { { "Stool", GetSizeForStool }, { "Rug", GetSizeForRug } }; // Usage in your system public static string GetProductParameters(string productType) { // No branches here—just look up and execute the function return _productCalculators[productType](); }
Why This Fits Your Requirements
- No branching statements: No
switch/ifanywhere in the core logic. - No polymorphism/inheritance: You're not creating base classes or implementing interfaces—just mapping strings directly to standalone functions.
- Extensible: Adding a new product type (e.g., "Table") only requires writing the new
GetSizeForTable()function and adding an entry to the map—no changes to existing code. - Clean matching: Directly ties your product classification system to the exact algorithm it needs.
Handling Edge Cases
If you need to handle product types that aren't in your map, you can add a default function without introducing branches. For example, in Python:
def get_default_size(): return "Unknown product type—no size available" product_size = product_calculators.get(selected_product, get_default_size)()
This still avoids conditionals and keeps your logic clean.
内容的提问来源于stack exchange,提问作者Isaiah Shiner

