3

I have a class with (say) 50 fields. I only use a few of them per deployment of the program per user need. Is there a way to make the constructor generic yet specific to the deployment?

e.g

public class Employee{

    private String id       = "default";
    private String empcat   = "default";
    private String empfam   = "default";
    private String phychar  = "default";
    private String othchar  = "default";
    private String shoesty  = "default";
    private Double shoesiz  = 0.0;
    private String shoesty  = "default";
    private Double shirsiz  = 0.0;
    private String shirsty  = "default";
    .........50 other fields..
}

"User/Customer 1" - only wants to use the program for shoe and thus instantiates the object with :
Employee obemp = new Employee("John", 11.5, Dockers); (i.e. id, shoesiz and shoesty)

User/Customer 2 - only wants to use the program for shirt and thus instantiates the object with :
Employee obemp = new Employee("John", 42, ABC); (i.e. id, shirsiz and shirsty)

User/Customer 3 - only wants to use the program for family and thus instantiates the object with :
Employee obemp = new Employee("John", "Smith"); (i.e. id, empfam)

The order of the fields during the object creation can be different - depending on the usage in the model.

6
  • OK, I see your question now. Can you give an example of what "generic yet specific" means? I'm not sure what you want. Look at the Builder Pattern Commented May 24, 2015 at 20:51
  • 1
    A better approach here would be Builder pattern, like Employee e = Employee.ofName("John").ofSurname("Smith").build() Commented May 24, 2015 at 20:53
  • Not to mention that this is object oriented programming. Shoe is one kind of object, shirt another, and so on. Commented May 24, 2015 at 20:55
  • Can I suggest that 50 fields in a single class might be too many - consider splitting it into smaller, more focussed classes, which you compose into an Employee. Commented May 24, 2015 at 20:55
  • why you not use an array like public Employee(array) and and the user can pass the parameters he wants the other to be null or empty and that's it.... (i know it's not so easy but it is a solution) Commented May 24, 2015 at 21:28

2 Answers 2

2

First of all, I'd suggest breaking your main class down into smaller pieces that manage data which typically goes together (Shoe information, Shirt information, Family information, etc.).

Secondly, I'd suggest you provide customers with a builder pattern to make it easy for them to construct an object with just the pieces that they're likely to need. That way, they can do something like this:

Employee emp = new EmployeeBuilder("John")
    .withShirtInfo(shirsiz, shirsty)
    .build();
Sign up to request clarification or add additional context in comments.

Comments

1

There is no generic way in core java to do this. But you may use some design pattern like - builder pattern.

You may also create an Employee with some minimum criteria like - id. We can assume each Employee have an id. So create an Employee with the id using the Employee(String id) constructor -

public class Employee{

     //all properties

   public Employee(String id){
      this.id = id;
   }

   //setter methods
}  

Suppose you have create an Employee like this -

Employee employee = new Employee("eng134");   

After that you can set only required property to employee object using the setter methods -

employee.setShoesiz(9);
employee.setShirsiz(26);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.