简体   繁体   English

“外观方法”(称为本类的公共方法)是否被视为不良设计?

[英]Is 'facade method' (calling public methods of own class) considered bad design?

Is it considered bad design to have a 'facade method' (I can't think of a better term). 使用“外观方法”是否被认为是不好的设计(我想不到一个更好的术语)。 Example: 例:

public class MyService() {
    public void doThis() {
        // do something
    }

    public int doThat() {
       // do something
    }

    public boolean isThisTrue(String str) {
       // do something
    }

    // determine something by calling methods within the same class
    public boolean shouldSomethingBeDone() {
        int result = 0;            

        if (isThisTrue("foobar")) {
           doThis();
           result = doThat();
        }

        return result > 0; 
    }
}

I'm trying to avoid having too much logic in one of my Controller classes that is calling a service. 我试图避免在调用服务的Controller类之一中包含过多逻辑。 The only way I could see getting around this was by created a method such as the one above. 我能看到的唯一解决方法是创建一种上述方法。

The dilemma arose when I tried to create a unit test and mock the calls within the shouldSomethingBeDone() method. 当我尝试创建一个单元测试并在shouldSomethingBeDone()方法中模拟调用时,出现了两难选择。 Since all of the calls are to methods within the same class it's difficult to mock this. 由于所有调用都针对同一个类中的方法,因此很难对此进行模拟。

Thanks. 谢谢。

Have you seen the Tell Don't Ask principle? 您是否看到了“不要说话”原则?

Methods like isThisTrue ask questions and push the logic for answering them all over the place. 诸如isThisTrue方法isThisTrue提出问题,并在整个地方推动回答问题的逻辑。 Instead, prefer to just tell the object what to do and let it worry about it. 取而代之的是,只想告诉对象该做什么,然后让它担心。

This suggests that instead of shouldSomethingBeDone you should rename the method to tell the object what to do instead (difficult without real names, but let's say doSomething is a better name). 这表明,而不是shouldSomethingBeDone您应该重命名的方法来告诉对象做什么,而不是(很难不实名,但让我们说doSomething是一个更好的名字)。

In the highly abstracted version that you have given, it is hard to say whether this is an example of good design or bad design. 在您提供的高度抽象的版本中,很难说这是好设计还是坏设计的例子。 However, I don't think there is general principle that says it is bad. 但是,我认为没有一般性原则认为不好。

The fact that something might be difficult to test (exhaustively) doesn't make it bad design ... or vice versa . 某些东西可能很难(详尽地)测试的事实并不能使它成为糟糕的设计…… 反之亦然

We could critique the names of those methods, but I don't think that addresses your question, with respect to either design or testability. 我们可以批评这些方法的名称,但我认为这不能解决您的设计或可测试性问题。

Structuring your code using methods is generally good design. 使用方法构造代码通常是好的设计。 Of course you shouldn't overdo like creating lots of one-line methods. 当然,您不应过分喜欢创建许多单行方法。

If you have a method, which shouldn't be called by another class, just don't use public and use private. 如果您有一个不应被另一个类调用的方法,则不要使用public和private。

Another advantage: Using Inheritance might be easier, because you might not need to override whole shouldSomethingBeDone() , but just override smaller methods. 另一个优点:使用继承可能更容易,因为您可能不需要重写整个shouldSomethingBeDone() ,而只是重写较小的方法。

Yeah, mocking is difficult. 是的,嘲笑很困难。 I would test shouldSomethingBeDone() Method as a whole. 我将从整体上测试shouldSomethingBeDone()方法。 Or just use Google to find out how to mock a method in the same class you are testing. 或者只是使用Google找出如何在您正在测试的同一类中模拟方法。

暂无
暂无

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

相关问题 在同一个类中调用其他方法的方法:设计不好? - Methods calling other methods in the same class: bad design? 这会被认为是不良的RESTFul设计吗? - Will this be considered as bad RESTFul design? 从重写方法中调用(非公共)方法 - Calling (non public) methods from overriden method 接口的这种设计会被认为是不好的吗? - Would this design of interface be considered bad? 是否有两个版本的相同方法只有不同的签名(方法名称和'throws'属性)被认为是糟糕的设计? - Is having two versions of the same method which only differ in their signature (method name and 'throws' attribute) considered bad design? 为什么javac认为在泛型类上使用泛型返回调用方法是不安全的? - Why calling method with generic return on a generic class is considered unsafe by javac? OOP:在同一个类中调用公共方法 - OOP: Calling a public method within the same class 使用“instanceof”运算符是否被认为是糟糕的设计? - Is This Use of the “instanceof” Operator Considered Bad Design? 为什么类调用自己的@Transactional 方法需要自注入,而@Bean 方法不需要自注入来确保创建单个实例? - Why self-inject is required for class calling own @Transactional method, but is not required for @Bean methods to ensure single instance creation? Junit最佳实践:公共方法调用多个私有方法 - Junit Best Practice: Public method calling multiple private methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM