samedi 23 octobre 2021

How to implement conditional visibility of methods in a Builder design pattern?

How to change the visibility of methods in a Builder design pattern ?

For example I have this Builder :

public class Builder {

    public Builder a() {
        //
        return this;
    }

    public Builder b() {
        //
        return this;
    }

}

User can use the API and do this :

new Builder().a().b();
new Builder().a();
new Builder().b();
new Builder().b().a();

I want to allow him to access method b() only if a() has been called :

new Builder().a().b();
new Builder().a();

A simple example could be a SQL request Builder. You shouldn't be allowed to call when() before select().

How to do so?

Aucun commentaire:

Enregistrer un commentaire