Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

more dimensional query

Thread view: 
Pi421 - 25 Nov 2005 13:31 GMT
Hi,
i have a problem with a query.
First i have stocks, depending on 4 attributes. Its like a tree, so
there are 16 possible querys. Like the stock hat batchs, and the one
patchs is reserverd for diffrent projekts and some of the materials in
the stoch habe diffrent qualitys....
So i want to programm a Method with 4 attributes. All combinations of
the attributes are possible. Like getStock(batchA, ProjektX, QualityZ,
conditionF).
My question: How do i store the stock and how do i programm the
getStock method. I think arrays are not the best way...
Does someone knows a good way, or i hope there is allready a solution
somewhere. Because i cant be the first guy having this problem.
Thanks
Joe Black
iamfractal@hotmail.com - 25 Nov 2005 14:59 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.