如何标注强制性的方法? [英] How to mark a method obligatory?

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

问题描述

假设你创建一个使用生成器模式的人一类的名称,假设生成器类包含的方法体()头()武器()当然建立(),你考虑的方法头()建立()强制性的这一类的用户。

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的是这个答案pssed这样的IM $ P $,他在的博客。它的这种巧搭上的全程治疗值得此处引用生成器模式。

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天全站免登陆