2

我正在为一家合金钢公司设计一个实验室系统,在这个系统中,我们必须对生产线上的每个产品应用某种类型的测试(例如分析测试、冲击测试……),我使用TestTemplate类来定义每个测试类型模板(例如分析测试模板)。每种测试类型都有一些参数,每个参数可以采用以下形式:

  • 范围值(最小值,最大值)
  • 单值(值)
  • 可选值(从值列表中选择的一个或多个值)

(到现在为止,将来可能会发现一些新的参数类型)。

同样对于每个产品,我们应该为每个测试类型模板定义一些标称值(换句话说,每个测试类型模板的期望值),例如对于 productA,分析测试的标称值可能如下:

  • 碳范围:(Min=0.23 Max=0.65)
  • Fe范围:(Min=1.25 Max=1.75)
  • 铜范围:(Min=0.87 Max=1.02)

对于 ProductB,标称值可以是:

  • 碳范围:(Min=0.43 Max=0.55)
  • Fe范围:(Min=1.15 Max=1.65)
  • 铜范围:(Min=0.57 Max=1.12)
  • pb range: (value = 0.12) /* 单值类型参数*/

这是我针对这个问题的设计 在此处输入图像描述

我设计的主要目标是实现测试模板的动态结构,是否有针对这种情况的设计模式或一些最佳实践,或者这种设计是否足够好?

4

1 回答 1

3

I congratulate you with your design. This design has a very clear distinction between class and instance and is not bad at all.

Your question is unclear. What do you think is wrong with your design or needs improvement? Sometimes asking the right question is what will give you the right answer.

One thing I would have improved upon is the TestParameter class. I'd probably have generalized that. In other words, I would have made a RangeTestParameter, SingleValueTestParameter and a SelectableTestParameter. I would've made that generalized TestParameter include a factory method too:

ITestParameterValue ITestParameter.createValue()

Implemented by SelectableParameter it would return a SelectableValue. That SelectableValue would have been supplied with a set of selectable/permitted values via its constructor.

This way you could address two points in your design that triggered questions I would have asked myself if this was my design:

  1. TestParameterType: Why design a type class when the type is available to you at compile-time? How are you going to ensure that your Code parameter contains a valid and functional value? Who is responsible for creating these types based upon these Codes?
  2. PermitedValue: Does TestParameter have a list of ParameterValues only if the TestParameter has a (Code) reference to the ParameterType Selectable? What if it doesn't? Who ensures it does? Who knows how to interpret a TestParameter in this particular way? Only SelectableValue? Only those who have read the if statement in your class diagram?

If you'd like to present your users with a list of possible parameters you can create a TestParameterFactory that links your parameter types to some additional info. Here is a simple Java version of such a factory:

public class TestParameterFactory {
    private final Map<String, ITestParameter.class> parameterTypes;

    public TestParameterFactory() {
        parameterTypes = new HashMap<String, ITestParameter.class>();
        parameterTypes.put("Range", RangeTestParameter.class);
        parameterTypes.put("Selectable", SelectableTestParameter.class);
        parameterTypes.put("Single value", SingleValueTestParameter.class);
    }

    public getParameterTypes() {
        return parameterTypes;
    }

    public ITestParameter createParameter(String name) {
        ITestParameter parameterType = parameterTypes.get(name);
        if (parameterType == null) 
            throw new IllegalArgumentException(name+ " is not a valid parameter type name");
        return parameterType.newInstance();
    }
}

I hope this helps. Good luck!

于 2013-09-20T22:11:32.487 回答