mercredi 26 octobre 2022

Builder Design Pattern - How to make field only accessible to one of its subtype

I have created builder for client type A and B. Client TypeA has two fields(see below), while client TypeB has two additional fields. How can I make sure field C and D are only accessible to client TypeB, i.e. client TypeA should not take field C and D when creating.

  client.builder()
    .withClientType(ClientType.TypeA)
    .withFieldA(fieldA)   
    .withFieldB(fieldB)
    .build();
  client.builder()
    .withClientType(ClientType.TypeB)
    .withFieldA(fieldA)   
    .withFieldB(fieldB)
    .withFieldC(fieldC)
    .withFieldD(fieldD)
    .build();

What's the correct usage builder pattern in this case?

Aucun commentaire:

Enregistrer un commentaire