Design a Algorithm/ Module Design Pattern

• December 5th, 2012, 05:30 AM
vinaysb
Design a Algorithm/ Module Design Pattern
I am having problem designing a module.Can anybody help me
Requirement:
This is basically an agricultural project (web application).I need to design module where some calcuation takes place.
There are differnt crops invovled like maize,tomato,okra etc.Now each of these crops have different traits.
Each trait has a mesurament scale which lies in integer like 200-1000.now let say i have planted the crop and
done measurement noted down the traits.Now i want to do some sort of measurement.Some measurements are simple and some are
complex.
Lets take an example of crop maize.I have recored observations for 15 trait.(trait1.. trait15 are just alias the acutal name can be like plt_ht,yld)
trait1 trait2 trait3 trait4 trait5 trait6..... trait15
i have recorded 5 observations for each traits
trait1 trait2 trait3 trait5 trait6..... trait15
01,02,03,04 01,02,03,04 01,02,03,04
User logs into system and selects his crops and enters data for these observation i either have to calculate average of those data entered for each or sum of those.
This is simple but complexity comes when for some of the tratis i have some different formulas. Like lets take an example let say a trait YLD has a formula based on which i have to caluate its value which may also depends on some other traits .This way all differnet crops have different traits.All this i am able to do whenever user selects crop i will check for those specific traits and do calculations if it is not special trait then i eaither avg it or sum it based on db entery but there is lot of hard coding .I want to have some suggestion from you people if u can think of an better way of handling this.
• December 7th, 2012, 02:33 AM
nuzzle
Re: Design a Algorithm/ Module Design Pattern
Quote:

Originally Posted by vinaysb
complexity comes when for some of the tratis i have some different formulas.

In such cases the Strategy design pattern is often used.

To understand how it works take sorting as an example. Instead of writing a different sort module for each and every kind of item you want to sort you write one general sort module and then for each specific item you supply a strategy for how exactly this kind of item is to be sorted. In the sort case the strategy simply tells which of two items is the bigger. That's all the sort algorithm needs to know about items to sort them.

In the same way you could separate and encapsulate what's specific about each trait from what's general for all traits. The trait specific strategies are then passed in to otherwise trait independent algorithms/code. In practice this can be done in different ways depending on your coding style and programming language. In C++ 11 a nice way is to use lambdas.
• December 7th, 2012, 07:21 AM
vinaysb
Re: Design a Algorithm/ Module Design Pattern
Thank you ,
any equivalent in java ;)
• December 7th, 2012, 08:17 AM
nuzzle
Re: Design a Algorithm/ Module Design Pattern
Quote:

Originally Posted by vinaysb
any equivalent in java

Sure, Collections.sort() could serve as a blueprint for how to implement the Strategy pattern in Java. Here's an example,

http://www.vogella.com/blog/2009/08/...ons-sort-java/

This sort() method will sort any kind of item as long as a proper strategy object implementing the Comparator interface is passed in. In this case the strategy object is called MyIntComparable and determines the relative order between two Integer objects. If you want to sort something else or Integers in some other way you pass in another strategy object.

That shows the principle of the Strategy pattern and how it may be used but you'll have to adapt it to your situation of course. And if it doesn't fit consider other patterns or combinations of patterns. As it happens one of the best introductory book uses Java as model language. It's Head First Design Patterns by E & E Freeman.
• December 17th, 2012, 12:56 AM
vinaysb
Re: Design a Algorithm/ Module Design Pattern
There are crops and each crop have traits , traits are actually a mesuremet scale to decide growth of a seed of a particular crop. This module is to for planters to observe growth of seeds sowed of certain crops and take down n no of observation for each trait and upload in csv format.Once they enter data i have to either avg out the values or sum the values or sometimes there are more complex function that i have to apply it may differe for each trait .This is the whole module about.Just to give an idea about how they will enter data

Code:

```Hyubrid(seed) trait1 trait2 trait3 trait5 trait6..... trait15 Hybrid1 01 02 03 04 01 HYbrid2 04 06 08 04 01 HYbrid2 04 06 08 04 01 HYbrid2 04 06 08 04 01 HYbrid2 04 06 08 04 01```
Once they enter data in this format i have to give result something like this.

Here avg colum does not necessaryly mean avg it can be sum or any formula based resutl.Hybrid is the seed for which they record the observation. I have shown avg column only for two tratis it is actually for all the traits.

Code:

```Hyubrid(seed) trait1 Avg trait2 avg trait3 trait5 trait6..... trait15 Hybrid1 01 01 02 04 03 04 01 HYbrid2 04 04 06 10 08 04 01 HYbrid2 04 04 06 12 08 04 01 HYbrid2 04 04 06 14 08 04 01 HYbrid2 04 04 06 12 08 04 01```
Hope this clarifies atleat a but

The data are not correctly indented but there is no way i can format it.