[重构技巧]Java->更符合心智模型的DSL重构

1,019

这是我参与11月更文挑战的第25天,活动详情查看:2021最后一次更文挑战

DSL

Domain-specific language: 一种专注于某一领域,仅针对部分表达方式的计算机编程语言。

它有几个特点:

  • 方法链 Method Chaining
  • 功能序列 Functional Sequence
  • 嵌套函数 Nested Functions 嵌套函数
  • Lambda表达式/闭包 Lambda Expressions/Closures

概念有点抽象,先看代码吧

假设你想发一些邮件,你需要一个类能够方便的设置收信人、发信人、标题、内容。

一个传统的java api(具体业务代码都省略了):

public class Mailer {
    public void from(String fromAddress) {
    }

    public void to(String toAddress) {
    }

    public void subject(String theSubject) {
    }

    public void message(String body) {
    }

    public void send() {
    }
}

测试要这样写:

public static void main(String[] args) {
    Mailer mailer = new Mailer();
    mailer.from("build@example.com");
    mailer.to("example@example.com");
    mailer.subject("build notification");
    mailer.message("some details about build status");
    mailer.send();
}

我们可以做些重构,使这个api更流畅,更像DSL。

package dsl.example;

public class Mailer {
    public Mailer from(String fromAddress) {
        return this;
    }

    public Mailer to(String toAddress) {
        return this;
    }

    public Mailer subject(String theSubject) {
        return this;
    }

    public Mailer message(String body) {
        return this;
    }

    public void send() {
    }
}

测试:

这样看起来好多了,但是如果能消除new就更好了。因为用户的兴趣在于发送邮件,而不是在创建对象。

public static void main(String[] args) {
    new Mailer()
        .from("build@example.com")
        .to("example@example.com")
        .subject("build notification")
        .message("some details about build status")
        .send();
}

测试:

public static void main(String[] args) {
    Mailer.mail()
        .from("build@example.com")
        .to("example@example.com")
        .subject("build notification")
        .message("some details about build status")
        .send();
}

可以做一下静态导入

public static void main(String[] args) {
    import static dsl.example.Mailer.mail;mail()  
        .from("build@example.com")  
        .to("example@example.com")  
        .subject("build notification")  
        .message("some details about build status")  
        .send();
}

这样,一个DSL的语句就完成了。一般来说,使用Java编写的DSL不会造就一门业务用户可以上手的语言,而会是一种业务用户也会觉得易读的语言,同时,从程序员的角度,它也会是一种阅读和编写都很直接的语言。

小结

创建DSL最好的方法是,首先将所需的API原型化,然后在基础语言的约束下将它实现。DSL的实现将会牵涉到连续不断的测试来肯定我们的开发确实瞄准了正确的方向。该“原型-测试”方法正是测试驱动开发模式(TDD-Test-Driven Development)所提倡的。

其实JDK8提供的很多api已经有很多内部DSL的语义,比如Stream流的find、count等操作都是一种DSL的语义表达,本文只是简单的说明了如何构造DSL,有机会计划找一个实际的业务代码用DSL的方式重构,敬请期待。