1

I have a complex class generated from a WSDL file. This class contains several other classes which in turn contain also other classes. There are more than 5 levels overall.

In order to initialize an instance I need to create several objects; the initialization code for creating an object of this class is around 100 lines.

Is there a good pattern to use for cases such as this? I created a wrapper around each class, and a wrapper around those wrappers. Is this is how it is done?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Adam
  • 515
  • 1
  • 5
  • 15
  • 7
    Are you sure this is the best design? Smells incredibly fishy. – RaminS Jun 02 '16 at 20:27
  • 1
    Without knowing the specific case, I can only say that whenever creating an instance is complicated, you should use either the factory or the builder pattern – fps Jun 02 '16 at 20:33
  • Thanks for reply, I will have a look on these patterns. Unfortunately I can not post the code due to intellectual rights. But, it is as follows . In order to initialize the parent class I want to initialize a list of classes first. Example. let us assume I need to create an object of a person class, I need first to initialize an object of the name class which is set as first name and last name and title, and then I need to create an object of age class which have a code such as adult and age value and so on. This is enforced by the java proxy class. – Adam Jun 02 '16 at 21:10

1 Answers1

1

Is there a good pattern to use for cases such as this?

Yes, it's called builder pattern. I prefer to use fluent interface. Another example you can find here.

Community
  • 1
  • 1
Alex G.
  • 909
  • 1
  • 9
  • 16