如何处理时间耦合? [英] How to deal with temporal coupling?

查看:590
本文介绍了如何处理时间耦合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此而苦苦挣扎:

我的课程有一些具有时间耦合的方法.也就是说,必须先调用某些方法 MethodA 来初始化" MethodB 需要正常工作的数据.

My classes have some methods that have temporal coupling. This is, some method MethodA has to be invoked first to "initialize" the data that MethodB needs to work properly.

我通常通过将有问题的依赖项作为参数传递给"MethodB" ,来明确时间耦合,如以下代码片段:

I usually make the temporal coupling explicit by passing the offending dependency to "MethodB" as argument, like in this snippet:

private class SomeClass
{
    private string field;
    private int count;

    public SomeClass()
    {
        MethodA();
        MethodB(field);
    }

    private void MethodA()
    {
        field = "Something";
    }

    private void MethodB(string str)
    {
        count = str.Length;
    }
}

尽管它使事情变得很明确,但我还是觉得自己做错了.我最终遇到了根本不使用字段的方法(静态方法!),因此该类的内聚性似乎开始降低.

Although it makes things explicit I feel I'm doing something wrong. I end up having method that don't use fields at all (static methods!), so the class starts to seem less cohesive.

这是最好的方法吗? (通过传递参数失去凝聚力)

Is this the best way to do it? (losing cohesion by passing arguments)

关于建议在构造器中使用field作为参数或使用Builder模式以避免无效状态的一些答案:我不能这样做,因为在我的情况下,我正在构建解析器 . MethodA读取输入并根据其设置状态(从文件中读取字符),然后调用MethodB.必须以正确的顺序调用它们.那是真正的问题:一个应该在另一个之前被调用.

Regarding some answers that suggest using field as a parameter in the constructor or using the Builder Pattern to avoid invalid states: I cannot do that, because in my case I'm building a Parser. MethodA reads the input and sets the state depending on it (reading characters from a file) and then, MethodB is invoked. They have to be invoked in the correct order. That is the real problem: one should be invoked before the other.

推荐答案

如果您遵循Anemic Domain Model,则可以中断您的课程并将其设置为2个较小的课程.您会意识到不良的设计,因为您当前的课程违反了 SRP ,简而言之,它有2个责任:1用于处理输入过程,1用于处理输入结果.

If you follow Anemic Domain Model, you can break your class and make it 2 smaller classes. You become aware of bad design because your current class violates SRP, in short it has 2 responsibility: 1 for handle the input process, 1 for process the input result.

将其分解,以使ClassA处理输入并返回结果,然后ClassB将以ClassA的结果作为参数,然后对其进行处理.例如:

Break it down so that ClassA will handle the input and returning result, then ClassB will take the result from ClassA as parameter, then process it. ex:

public class ClassA
{
    public string MethodA()
    {
        // read the input
        return "Something"; // or return the input
    }
}

public class ClassB
{
    private int count;
    public void MethodB(string str)
    {
        count = str.Length;
    }
}

如果您发现这两个类的使用都很麻烦,请使用另一个聚合服务.例如:

If you find the use of both class is bothersome, use another aggregate service for that. ex:

public class ClassC
{
    public ClassA ClassA = new ClassA();
    public ClassB ClassB = new ClassB();
    public void Execute(){
        string result = ClassA.MethodA();
        ClassB.MethodB(result);
    }
}

这篇关于如何处理时间耦合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆