在课堂上有公共方法而在界面中有什么意义? [英] What's the point to have public method in class but not in interface?

查看:111
本文介绍了在课堂上有公共方法而在界面中有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

public interface IMessageService
{
   void ProcessMessages(IEnumerable<Message> messages);
}

实现了界面:

public class MessageService : IMessageService
{
     public void ProcessMessages(IEnumerable<Message> messages)
     {
        // whatever
     }
}

然后意识到ProcessMessages应该分解一下来处理消息的不同部分:

Then realized that ProcessMessages should be broken down a little bit to handle different parts of the message:

public class MessageService : IMessageService
{
     public void ProcessMessages(IEnumerable<Message> messages)
     {
       foreach (var msg in messages)
       {
          ProcessCustomer(msg.Customer);
       }
     }

     private bool ProcessCustomer(Customer c)
     {

     }
}

接下来,想要单元测试 ProcessCustomer 这样做了 ProcessCustomer public 此时没有强制转换就无法访问该方法,那么重点是什么?。

Next, wanted to unit test ProcessCustomer so made ProcessCustomer public and at this point the method is not accessible without a cast, so what's the point?.

推荐答案

请考虑以下内容。你去披萨店,你想测试他们的意大利辣香肠比萨饼。你想测试香肠切片机的工作原理吗?不。你想在你的披萨上看到五片香肠香肠 - 这是披萨店的要求。实际上,无论是手动切割香肠还是用切片机切割都没关系。您应该只验证是否正确实现了所有要求

Consider following. You go to the pizza shop and you want to test their pepperoni pizza. Do you want to test how sausage slicer works? No. You want to see five pieces of pepperoni sausage on your pizza - that is requirements for pizza shop. Actually it even does not matter whether sausage was cut manually or with slicer. You should only verify that all requirements are implemented correctly.

与您的班级相同 - 它的要求是处理邮件。如何实现消息的处理并不重要 - 无论是否在内部调用一些辅助方法。所以,我的建议总是一样的 - 不要测试私有方法 - 它们不是必须存在的,它们的存在不应该被测试。

Same with your class - it's requirement is processing messages. It does not matter how processing of messages is implemented - whether it calls some helper method inside, or not. So, my advise is always same - do not test private methods - they are not required to exist, and their existence should not be tested.

这篇关于在课堂上有公共方法而在界面中有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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