my dog learned polymorphism
The moose likes OO, Patterns, UML and Refactoring and the fly likes Which Design Pattern and how to design using OOP this scenaria Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Which Design Pattern and how to design using OOP this scenaria" Watch "Which Design Pattern and how to design using OOP this scenaria" New topic

Which Design Pattern and how to design using OOP this scenaria

vinay basavanal

Joined: Dec 08, 2012
Posts: 8
I am having trouble designing a module, can anybody help me?

Because it will be hard to maintain this kind of module, I also think that this can test my skill of design pattern usage.


This is basically an agricultural project (web application). I need to design a module where some calculation takes place.

There are different crops involved like maize, tomato, okra etc. Each of these crops has different traits.

Each trait has a measurement scale which lies in integer like 200-1000. Now let's 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 recorded observations for 15 traits. (We'll use trait1-trait15 as examples, the actual name can be like plt_ht, yld, etc.)

I recorded 5 observations for each trait:

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 observations. I have to calculate either average or sum of the data entered for each trait.

Complexity / centre of the problem

So far it's simple but complexity comes when I have some different formulas for some of the traits.

Example: trait YLD has a formula based on which I have to calculate its value, which may also depend on some other traits. Each different crop can 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's not a special trait then I either average or sum it, based on db entry), but there is a lot of hard coding.
I would like to have suggestions on a better way of handling this.

My code needs to handle both simple and complex calculations.
Simple calculations are easy, I have take average of value entered for trait.
The problem comes when I have to do complex calculations, since each crop have different traits with their own formulas, so to calculate I have to check for crop and then for complex trait. So I have to hardcode the trait name of complex traits.
Can any tell me how I can design this using Java oops [?!?] so that I can make it generic?

I have about 10 different crops. Some calculations are specific to crops, so there will be lot of code like the if below:

From Desgin point and maintaininablility point which way should this be handled
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6323

My first suggestion is to look at the Strategy pattern. This will allow you to treat the algorithm you use for a particular trait as an object.

Next, I'll address one of my pet peeves. My first rule for writing maintainable code is to write readable code. Names like plt_ht, avg, and yld may reduce the amount of typing you do but it just makes your code SOOOO unreadable. There is absolutely no justification for making your variable names so cryptic like that. Spell names out and make them readable! The names plantHeight, average, and yield are so much more readable and with most modern IDEs today, code completion makes it just as easy to code without too many keystrokes. When you have cryptic, unreadable names, your code's maintainability is already reduced by an order of magnitude off the bat.

Next, you are passing way too many parameters into your methods. When you get to three or more parameters in a method, you should seriously start thinking about ways to reduce it. The more parameters you are passing to your methods, the harder it is to maintain the code when something changes.

Junilu - [How to Ask Questions] [How to Answer Questions]
vinay basavanal

Joined: Dec 08, 2012
Posts: 8
Points Taken,

Now can you please explain me how can use stratergy pattern, each crops will have 100 of traits let say each crop has 10 different tratis where calculations differ how can i handle this so lets say traits like plantHeight, yeild ,qa_HU , tlssg need different calculations then how will i do it something like ?

and in my main code where i do caluations should be like this

I agree. Here's the link:
subject: Which Design Pattern and how to design using OOP this scenaria
It's not a secret anymore!