简体   繁体   English

如何更改方法中传递的Lambda表达式的值

[英]How to change value of passed Lambda expression inside method

I have a class EmailServiceImpl which has a method sendMail(Cumsumer<EmailTemplate> template) . 我有一个类EmailServiceImpl ,它具有方法sendMail(Cumsumer<EmailTemplate> template)

EmailTemplate has 4 fileds EmailTemplate有4 EmailTemplate

  • to
  • from
  • body 身体
  • subject 学科

I am setting three fields of EmailTemplate using Lambda expression and sending it to sendMail method, like below : 我使用Lambda表达式设置EmailTemplate三个字段,并将其发送到sendMail方法,如下所示:

emailService.sendMail(mailer->
                mailer
                    .to("")
                    .subject("")
                    .body("")
                );

But I want to add it's 4th filed (from) inside sendMail method like below : 但是我想将其添加到sendMail方法中的第4个字段中(如下所示):

public String sendMail(Consumer<EmailTemplate> template) {
        template.from = "XYZ"; // cannot do this

    } 

I cannot access any field of EmailTemplate using this lambda (template). 我无法使用此Lambda(模板)访问EmailTemplate任何字段。

I have following queries : 我有以下查询:

  1. Is it possible to change values of passed lambda inside a method(like above)? 是否可以在方法内部更改传递的lambda的值(如上)?
  2. Is it good idea to do like that? 这样做是个好主意吗?
  3. Any better approach to do this using lambda? 有更好的方法使用lambda吗?

Is it possible to change values of passed lambda inside a method(like above)? 是否可以在方法内部更改传递的lambda的值(如上)?

No. The sendMail() method accepts a consumer of emailTemplate. 不能。sendMail()方法接受emailTemplate的使用者。 Which is NOT an emailTemplate itself. 这本身不是emailTemplate。 The consumer is a function that accepts an emailTemplate and does something with it. 使用者是一个接受emailTemplate并对其进行处理的函数。 If you want to set the fourth field, you will need to do that inside of the lambda. 如果要设置第四个字段,则需要在lambda内部执行该操作。 The sendEmail() method is essentially receiving a class that can process an emailTemplate. sendEmail()方法本质上是接收可以处理emailTemplate的类。 It is not receiving an emailTemplate instance/state at all. 它根本没有收到emailTemplate实例/状态。

Is it good idea to do like that? 这样做是个好主意吗?

It depends on how you wish to design your sendMail() method and what responsibility do you want it to have. 这取决于您希望如何设计sendMail()方法以及您希望它承担什么责任。 By the looks of it (and to keep it simple), the sendEmail() method should receive an emailTemplate object (and not a consumer). 从外观上(并保持简单),sendEmail()方法应该接收一个emailTemplate对象(而不是使用者)。 If you receive only the consumer, the only thing you can do it invoke/apply it on an emailTemplate object (which you currently are not sending to the sendEmail() method) 如果仅接收使用者,则您可以执行的唯一操作是将其调用/应用到emailTemplate对象(您当前未将其发送到sendEmail()方法)

Any better approach to do this using lambda? 有更好的方法使用lambda吗?

Lambdas define behavior/method-bodies. Lambda定义行为/方法体。 The act on an object that is supplied to them at a later time. 对稍后提供给他们的对象执行的操作。 In this example, I don't see the need for lambdas at all 在此示例中,我完全看不到需要lambda

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

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