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
将仅从地图获取requiredFeature值并返回实例。这似乎是一个在派生类中是静态的方法,但客户端无法使用现有对象创建派生类实例。有更优雅的写作方式吗?或者这有一些适用的模式吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
您正在违反SOLID的单一责任原则。功能必须只做一个工作。其他类如Builder或其他类应该做其他工作 - 构建您的功能。如果您需要基于其他功能构建功能,请使用以下内容:
builder.SetValues("valueofreqdfeature");
IFeature someFeature = builder.CreateFeature();
builder.UseFeature(someFeature);
IFeature newFeature = builder.CreateFeature();
如果需要更改关系,您不需要更改数十亿个功能中的代码。您需要更改的唯一地方是您的构建器类。并且还使用接口