Interesting.
What does your Stock interface look like?
This is important because how your Stock interface services requests
will decide whether Stock can delegate requests to its four attributes,
or whether it must demonstrate behvariour based on some combined
influence of the attributes.
The former is preferred, in many respects, as its possibly simpler.
If you Stock interface looks like this:
public interface Stock {
String getProjectName();
Condition getCondition();
}
Then you can just create a ConcreteStock object, store the attributes,
and delegate various request as follows:
class ConcreteStock {
Batch batch;
Project project;
Quality quality;
Condition condition;
Stock(Batch batch, Project project, Quality quality,
Condition condition) {
this.batch = batch;
this.project = project;
this.quality = quality;
this.condition = condition;
}
String getProjectName() {
return project.getName();
}
Condition getCondition() {
return condition;
}
}
If, however, your ConcreteStock must behave according to a combintation
of attributes, then the question becomes: are all your attributes
single-valued, in that they are a series of individual values, rather
than value ranges?
If this is the case, then you could create a compound class,
containing the four attribute values (much like ConcreteStock above)
but declare an equals() and hashCode() based on all four values, e.g.
(though note that this is not a valid hashCode() method),
public int hashCode() {
return batch.hashCode() * project.hashCode() * quality.hashCode() *
condition.hashCode();
}
This will then allow you to create a Map of pre-defined Stocks, and map
them to factories to create the actual Stocks you want. (This presumes
that you have different Stock implementations for the various attibute
values, of course.)
CompondAttribute compound = new CompondAttribute(batch124,
qualityGood,
project6,
conditionAlways);
Map attributesToFactories = new HashMap();
attributesToFactories.put(Batch124QualGoodProj6ConAlwaysFactory.getInstance(),
compound);
You could, of course, have a mixture of this and the previous approach.
Finally, you could have the brute-force approach below. Ugly and
costly, though.
.ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
-----------------------------------------
Brute force approach:
class Main {
Stock getStock(Batch batch, Project project, Quality quality,
Condition condition) {
swtich (batch) {
case Batch.BATCH_A:
return getBatchAStock(Project project, Quality quality,
Condition condition);
case Batch.BATCH_B:
return getBatchBStock(Project project, Quality quality,
Condition condition);
}
}
Stock getBatchAStock(Project project, Quality quality,
Condition condition) {
swtich (project) {
case Project.PROJECT_X:
return getBatchAProjectXStock(Quality quality,
Condition condition);
case Project.PROJECT_Y:
return getBatchAProjectYStock(Quality quality,
Condition condition);
}
}
Stock getBatchAProjectXStock(Quality quality, Condition condition)
{
swtich (quality) {
case Quality.QUALITY_Z:
return getBatchAProjectXQualityZStock(Condition condition);
}
}
Stock getBatchAProjectXQualityZStock(Condition condition) {
swtich (condition) {
case Condition.CONDITION_F:
return new getBatchAProjectXQualityZConditionFStock();
}
}
}
Pi421 - 28 Nov 2005 09:02 GMT
Hei thanks a lot!!!!
This is a good solution of my problem. Because my attributes are
individual, i use the compound class.
Really good answer!
Thanks again.
Regards