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

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

问题描述

假设您使用构建器模式创建一个类名Person,并假设Builder类包含方法 body() head() arms()当然 build(),你会考虑方法 build()必须为此课程的用户。



我们想以某种方式标记这些方法,如果可能,使用注释。如果此类的用户尝试构建一个Person实例,但忘记调用这些方法之一,我们希望得到某种警告 - 无论是从java编译器,还是从Eclipse或Maven,我们用来构建我们的项目 - 他们中的任何一个。



有可能吗?你会建议哪种方式?

解决方案

这里是一个使用不同类型使一些部分成为强制性的例子(它也使得订单你调用的方法是必须的):

 包测试; 

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 头)主体( 正文)臂( 臂)的腿( 腿)建立()。。

String person2 = PersonBuilder.newInstance()。head(head)。body(body)。arm(arm)。

}

}

界面StepOne {

//强制
StepTwo头(String head) ;

接口StepTwo {
//强制
StepThree body(String body);
}

接口StepThree {
//强制
LastStep arm(String arm);
}

//此接口中的所有方法不是强制性的
接口LastStep {
LastStep leg(String leg);
String build();
}

}

class PersonBuilder实现StepOne,StepTwo,StepThree,LastStep {

String head;
字符串体;
字符串;
字符串;

static StepOne newInstance(){
返回新的PersonBuilder();
}

private PersonBuilder(){
}



public StepTwo head(String head){
this.head = head;
返回这个;
}

public LastStep arm(String arm){
this.arm = arm;
返回这个;
}

public StepThree body(String body){
this.body = body;
返回这个;
}

public LastStep leg(String leg){
this.leg = leg;
返回这个;
}

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



编辑



OP对这个答案印象深刻,他在一个 blog 。这是一个聪明的建筑师模式,在这里应该得到一个全面的对待。


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.

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;
    }
}


Edit

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