Is there a design pattern for creating a prototype with only some values differing?

141 Views Asked by At

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?

2

There are 2 best solutions below

2
Ravindra babu On

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

0
Dzianis Yafimau On

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