如何将方法标记为强制性? [英] How to mark a method obligatory?

查看:31
本文介绍了如何将方法标记为强制性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您使用构建器模式创建了一个名为 Person 的类,并假设构建器类包含方法 body()head()arms() 当然还有 build() 并且你认为方法 head()build() 对这个类的用户来说是必须的.

Suppose you create a class names Person using the builder pattern, and suppose the Builder class contains methods body(), head(), arms() and of course build() and you consider methods head() and build() obligatory for the user of this class.

如果可能的话,我们想以某种方式将这些方法标记为强制性的.如果这个类的用户试图构建一个 Person 实例但忘记调用这些方法中的任何一个,我们希望得到某种警告 - 来自 java 编译器,或者来自 Eclipse 或 Maven,我们用来构建我们的项目 - 他们中的任何一个都可以.

We would like to somehow mark these methods obligatory, if possible using annotations. If a user of this class tries to build a Person instance but forgot to call either of these methods, we would like to get some kind of warning - either from the java compiler, or maybe from Eclipse or Maven, which we use to build our projects - any of them would do.

可以吗?你会建议哪种方式?

Is it possible to do? Which way would you suggest?

推荐答案

以下是使用不同类型使某些部分成为强制性的示例(这也使您调用方法的顺序成为强制性):

Here is an example with using different types to make some parts mandatory (it also makes the order you call the methods mandatory):

package test;

import test.StepOne.StepThree;
import test.StepOne.StepTwo;
import test.StepOne.LastStep;

public class TestBuilder {

    public static void main(String[] args) {

        String person1 = PersonBuilder.newInstance().head("head").body("body").arm("arm").leg("leg").build();

        String person2 = PersonBuilder.newInstance().head("head").body("body").arm("arm").build();

    }

}

interface StepOne {

    // mandatory
    StepTwo head(String head);

    interface StepTwo {
        // mandatory
        StepThree body(String body);
    }

    interface StepThree {
        // mandatory
        LastStep arm(String arm);
    }

    // all methods in this interface are not mandatory
    interface LastStep {
        LastStep leg(String leg);
        String build();
    }

}

class PersonBuilder implements StepOne, StepTwo, StepThree, LastStep {

    String head;
    String body;
    String arm;
    String leg;

    static StepOne newInstance() {
        return new PersonBuilder();
    }

    private PersonBuilder() {
    }



    public StepTwo head(String head) {
        this.head = head;
        return this;
    }

    public LastStep arm(String arm) {
        this.arm = arm;
        return this;
    }

    public StepThree body(String body) {
        this.body = body;
        return this;
    }

    public LastStep leg(String leg) {
        this.leg = leg;
        return this;
    }

    public String build() {
        return head + body + arm + leg;
    }
}


编辑

OP 对这个答案印象深刻,以至于他将其完整地写在了 博客.这是对构建器模式的如此巧妙的理解,值得在这里引用完整的处理方法.

The OP was so impressed with this answer that he wrote it up fully in a blog. It's such a clever take on the builder pattern that a full treatment deserves to be referenced here.

这篇关于如何将方法标记为强制性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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