简体   繁体   English

如何重用稍有不同的代码

[英]How to reuse a code which is slightly different

I'm having some difficulties about how to correctly design and implement two methods. 关于如何正确设计和实现两种方法,我遇到了一些困难。

I have 2 methods, A and B. Method A does 2 things and method B does only 1 thing. 我有2种方法,A和B。方法A做2件事情,方法B只做1件事情。 One of the things that method A does is the same as what method B does, so it is very reasonable to call method B from within a method A. 方法A所做的事情之一与方法B所做的事情相同,因此从方法A内调用方法B是非常合理的。

Now the problem is that both methods need to send exactly one email to a user. 现在的问题是,这两种方法都需要向用户发送一封电子邮件。 When I invoke A, I want to receive 1 email and when I invoke B, I also want to get 1 email. 当我调用A时,我希望收到1封电子邮件,而当我调用B时,我也希望收到1封电子邮件。 This means that if I call method B inside A, I will now get 2 mails while doing 1 action (= invoking A). 这意味着,如果我在A内调用方法B,现在我将在执行1个操作(=调用A)的同时收到2封邮件。 To make it even more tempting to simply include B in A, the set up procedure is truly the same for both methods and so instead of redoing the set up in the B, I could simply call method B with these set up data from A. But I have no setup data to provide when calling the B directly and so in this case I'd need to do the setup anyway. 为了使在B中简单包含B更具诱人性,这两种方法的设置过程实际上都是相同的,因此除了重做B中的设置之外,我还可以简单地使用A中的这些设置数据来调用方法B。但是,当直接调用B时,我没有提供设置数据,因此在这种情况下,我仍然需要进行设置。

What is the best approach to solve such a problem? 解决此类问题的最佳方法是什么? Should I: 我是不是该:

  1. Add a parameter to method B in which I will say whether it should send the email or not and another parameters for the setup data 在方法B中添加一个参数,在该方法中,我将说出是否应该发送电子邮件以及设置数据的另一个参数
  2. Keep the two methods separated, because method B is not doing exactly the same thing in the two contexts 将两个方法分开,因为方法B在两个上下文中做的事情并不完全相同
  3. ... other suggestions? ...其他建议?

PS. PS。 I'm not sure if stackoverflow is actually suitable for such a question, let me know if there's a better stackexchange platform for this. 我不确定stackoverflow是否真的适合这样的问题,请让我知道是否有更好的stackexchange平台。

Thanks for any ideas 谢谢你的任何想法

I don't know if this is truly on-topic either. 我也不知道这是否真的是话题。 That said... 那个...

It seems to me that "sending email" is one of the things that is included in the method B work that method A needs done. 在我看来,“发送电子邮件”是方法A需要完成的方法B工作中包含的内容之一。 As such, why not just implement it in method B? 因此,为什么不仅仅在方法B中实现它呢? Then method A gets it for free when it calls method B. 然后方法A在调用方法B时免费获得它。

If the exact contents of the email are different depending on whether method A was called or not, then sure…you can add a parameter to method B to customize the email in some way. 如果电子邮件的确切内容因是否调用方法A而有所不同,请确定…您可以向方法B添加参数以某种方式自定义电子邮件。

Finally, you're pretty vague on the details. 最后,您在细节上含糊不清。 It's not really clear just how much code you're saving by calling method B from method A. If it's significant, then I'm a strong proponent of code sharing like this. 通过从方法A调用方法B可以节省多少代码还不太清楚。如果有意义,那么我强烈支持这样的代码共享。 But if we're just talking a single statement, well...that seems less worth bothering with; 但是,如果我们只说一条语句,那……似乎就不那么值得打扰了; maybe just putting that same single statement in each method is better. 也许只是将相同的单个语句放入每个方法中会更好。

Sorry for the vague answer. 抱歉,答案含糊。 GIGO. GIGO。 :) :)

You could use the return of the methods for the content of our email: 您可以将方法的返回用于我们的电子邮件内容:

private String funcA() {
    // foo
    return "Mail from funcA";
}

private String funcB() {
    funcA();      // ignoring the return value here, we later return our on
    // foo
    return "Mail from funcB";   // our own return
}

Then you send the email outside of these functions (JUST EXAMPLE CODE): 然后,您在以下功能之外发送电子邮件(仅示例代码):

private void mainFunc() {
    String mail = condition ? funcA() : funcB();

    sendMail(mail);
}

I have two ideas: 我有两个想法:

  • create C method that represents common functionality of A and B, and invoke C in A and B with different parameters 创建表示A和B的通用功能的C方法,并使用不同的参数调用A和B中的C
  • invoke B inside A, but extract mailing functionality alone to separate method C 在A内部调用B,但仅提取邮件功能以分离方法C

To clarify the first idea: 为了阐明第一个想法:

methodA() {
        firstAction();
        secondAction(Parameter parameterFromA);
        }

methodB() {
        secondAction(Parameter parameterFromB);
        }

I would suggest you split out the functionality a little further. 我建议您进一步分离功能。

Currently you have two methods that might be described something like this: 当前,您可以使用两种方法来描述:

void MethodA()
{
    DoThing1();
    DoThing2();
    SendEmail();
}

void MethodB()
{
    DoThing1();
    SendEmail();
}

So one fairly simple answer is to extract the actual functional bits out of the two methods into methods of their own and leave behind a shell similar to the above. 因此,一个相当简单的答案是将两种方法中的实际功能位提取为它们自己的方法,并留下类似于上述内容的外壳。 Each of the DoThingX methods can return whatever it is you need for building the email, etc. 每个DoThingX方法都可以返回构建电子邮件等所需的内容。

Of course if the DoThingX methods are really small - a couple of lines or so, for instance - then it might not make sense to break them out this way. 当然,如果DoThingX方法真的很小(例如, DoThingX两行),那么以这种方式将它们分开可能没有意义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM