Akshit Gupta
Akshit Gupta

Reputation: 77

Allow instantiate of specific variables of a class based on the variable value

I have a use case where I have to instantiate a given class with only specific variables based on the value of variable (let say in this example) typeName. For a variable String typeName, if its value is TYPE-1, only specific set of variables (a,b,c) should be allowed to be instantiate. Similarly if its value is TYPE-2, only the another set of variables (x,y,z) should be allowed to instantiate.

if(typeName == "TYPE1") 
 {
     CentralClass class = new CentralClass(a,b,c);  //initiating only variable a,b,c
 }
else
{  
     CentralClass class = new CentralClass(x,y,z);  //initiating only variable x,y,z
}

Class Structure

public class CentralClass {

 String typeName; //ALLOWED Values TYPE1, TYPE2
 String x;
 String y;
 String z;

 String a;
 String b;
 String c;
}

What would be the best way to do so via any design pattern etc.

Note: The structure of the class is open for change. We can have multiple classes(clubbing different variables), inner, static classes, or any design pattern enforced etc.

Upvotes: 0

Views: 601

Answers (1)

Tom Cools
Tom Cools

Reputation: 1158

Answer

The builder pattern is perfect for your usecase. It will allow you to create an object with only certain fields. It is better to use a builder than to pass in "null" into a constructor for the fields you do not have.

You can quickly add all the builder methods using Lombok.


Side-notes

  • Only having half of the values filled in with a class is usually a sign you should create separate classes.
  • When you say "typeName" can only have values "TYPE1" or "TYPE2", you should be using an enum instead of String. This anti-pattern is often called primitive obsession.

Solution Code

Builder pattern (without Lombok)

public class CentralClass {

    String typeName; //ALLOWED Values TYPE1, TYPE2
    String x;
    String y;
    String z;

    String a;
    String b;
    String c;

    CentralClass(String typeName, String x, String y, String z, String a, String b, String c) {
        this.typeName = typeName;
        this.x = x;
        this.y = y;
        this.z = z;
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public static CentralClassBuilder builder() {
        return new CentralClassBuilder();
    }

    public static class CentralClassBuilder {
        private String typeName;
        private String x;
        private String y;
        private String z;
        private String a;
        private String b;
        private String c;

        CentralClassBuilder() {
        }

        public CentralClassBuilder typeName(String typeName) {
            this.typeName = typeName;
            return this;
        }

        public CentralClassBuilder x(String x) {
            this.x = x;
            return this;
        }

        public CentralClassBuilder y(String y) {
            this.y = y;
            return this;
        }

        public CentralClassBuilder z(String z) {
            this.z = z;
            return this;
        }

        public CentralClassBuilder a(String a) {
            this.a = a;
            return this;
        }

        public CentralClassBuilder b(String b) {
            this.b = b;
            return this;
        }

        public CentralClassBuilder c(String c) {
            this.c = c;
            return this;
        }

        public CentralClass build() {
            return new CentralClass(typeName, x, y, z, a, b, c);
        }
    }
}

Builder pattern (with Lombok)

@Builder
public class CentralClass {

    String typeName; //ALLOWED Values TYPE1, TYPE2
    String x;
    String y;
    String z;

    String a;
    String b;
    String c;
}

Usage

if(variable == "TYPE1")
{
    CentralClass aClass = CentralClass.builder()
            .a("A")
            .b("B")
            .c("C")
            .build();
}
else
{
    CentralClass aClass = CentralClass.builder()
            .x("X")
            .y("Y")
            .z("Z")
            .build();
}

Read more:

https://dzone.com/articles/design-patterns-the-builder-pattern

Upvotes: 1

Related Questions