通用技术 [java] loan pattern

bauul · October 04, 2017 · 688 hits

code

package com.carl.loanpattern;

import java.util.function.Consumer;

public class FluentMailer {

    private FluentMailer() {};

    public FluentMailer from(final String address) {
        System.out.println(address);
        return this;
    }

    public FluentMailer to(final String address) {
        System.out.println(address);
        return this;
    }

    public FluentMailer subject(final String subject) {
        System.out.println(subject);
        return this;
    }

    public FluentMailer body(final String msg) {
        System.out.println(msg);
        return this;
    }

    public static void send(final Consumer<FluentMailer> block) {
        final FluentMailer mailer = new FluentMailer();
        block.accept(mailer);
        System.out.println("Sending ...");
    }

    public static void main(String[] args) {
        FluentMailer.send(mailer -> 
            mailer.from("from@test.com")
                  .to("to@test.com")
                  .subject("subject line")
                  .body("BRs")
            );
    }
}

output

from@test.com
to@test.com
subject line
BRs
Sending ...
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up