1

Class heirarchy
The client uses them via the base class (java code) :

BaseClass baseObj1 = new DerivedClass("valueofreqdfeature");
//the required feature gets added to the map in the base class
Map<String, Object> features = Collections.singletonMap("requiredFeature1Name","requiredFeatureValue");
BaseClass newBaseObj = baseObj1.createNewConcreteFeature(features);

createNewConcreteFeature would get only the requiredFeature values from the map and return an instance. This seems like a method that would be static in the derived class but then the client can't create derived class instances with an existing object. Is there a more elegant way of writing this? or does this have some applicable pattern?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
shrewquest
  • 541
  • 1
  • 7
  • 22

2 Answers2

0

You can use Builder_pattern.

Declare only one class CustomerFeatures with all mandatory and optional parameters. Set the properties on need basis.

Have a look at this SE post :

Passing Properties to Factory method

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • the mandatory/optional features vary from one derived class to another so I don't see how that helps – shrewquest Jan 21 '16 at 05:01
  • Base class contains all parameters. Respective derived classes set required parameters. Even you don't need derived class unless you override behaviour of base class. – Ravindra babu Jan 21 '16 at 05:06
0

You are breaking Single Responsibility principle of SOLID. Features must do only one job. Other class like Builder or something should do other job - building your features. If you need to build feature based on other features, use something like this:

builder.SetValues("valueofreqdfeature");
IFeature someFeature = builder.CreateFeature();

builder.UseFeature(someFeature);
IFeature newFeature = builder.CreateFeature();

Going this way you don't need to change code in your billions of features if some changes in relationship between them is required. The only place you need to change will be your builder class. And also work with interfaces

Dzianis Yafimau
  • 2,034
  • 1
  • 27
  • 38