File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Which pattern to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Which pattern to use?" Watch "Which pattern to use?" New topic
Author

Which pattern to use?

Erwin DES
Greenhorn

Joined: Feb 26, 2003
Posts: 18
Hi,
I am new to Java Patterns and I encounter following problem that might give me the opportunity to enter the world of patterns :
The subject is the calculation of indemnification for corporal injury in case of insurance matters.
There exists in my country different ways to calculate the financial part for indemnification and summarized it looks like this :
- Only a moral indemnification;
- A combination of moral and material indemnification;
- Capitalization
If I look closer to the second possibility for example, I have to take into account different parameters :
- I need to now the % of injury:
If <= 9 %, the indemnification becomes 867.65 .
If <= 19 % this amount becomes 1,239.5
itherwise will be 2,479.0 .
This is a momentary situation => it will change in time. Not only the % can change, also other percentages may arise and also the amount may change.
How can I create a java program that provides me the possibility to achieve maximum flexibility in maintenance? Is it also possible to keep one or more of these elements (% and/or amount) accessible from the outside of the program, so it can still be changed by the user (for instance when only these parameters change.) But what if there is another level of % that needs to be integrated? Does this mean that new code has to be created?
Thanks for any help,
EDS
Gopi Balaji
Ranch Hand

Joined: Jan 23, 2003
Posts: 84
I think it is an excellent situation wherein the adoption of a rule engine would do good. Check out a Javaranch's bartender's book - Jess In Action.
If you think a rule engine is too general, and are adventerous, you could come up with your own expression/rule language tailored for your domain.
-GB.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That's really a tough question - and it strongly depends on the details of current and future requirements what the best design would look like.
A rule engine could be a good idea - admittedly I don't know enough about it to comment about it. You might also want to look at patterns like Strategy and Chain Of Responsibility. Embedding a scripting language like Jython might also be an option for more flexibility (but also complexity).
In my experience, it's really hard to think up a good design upfront for such a problem. So what I would do is use a highly incremental approach.
That is, I would implement the first (most fundamental) rule in the simplest way I could think of. Then I would implement the second. Then I would refactor the code so that there wasn't any duplication and implementing a third rule similar to the second would have minimal impact on existing code. That way the code gets flexible for the most likely changes in the future.
I would accompany the production code with extensive suites of both detailed unit (JUnit) and high level acceptance (FitNesse) tests. The former would help me design the system so that it becomes highly decoupled, and the latter would prevent refactoring the system to break existing functionality.
Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Erwin DES
Greenhorn

Joined: Feb 26, 2003
Posts: 18
Thanks for your suggestion. But, I don't think I would like to use Jess. It seems I have to pay for the use in the company where I work...
And writing my own expression/rule language tailored for this special domain doesn't seem easy to me. Or, do you have some documentation/examples for this use that could inspire me?
Are there no other work arounds?

EDS
Erwin DES
Greenhorn

Joined: Feb 26, 2003
Posts: 18
Thanks for your response, Ilja.
I still have some questions. You said : "I would implement the first (most fundamental) rule in the simplest way I could think of. Then I would implement the second. Then I would refactor the code so that there wasn't any duplication and implementing a third rule similar to the second would have minimal impact on existing code. That way the code gets flexible for the most likely changes in the future."
Concrete, how do you see this for the current problem? In my example I will have in many cases the following structure :
If Invalidity <= 9 %, the indemnification becomes 867.65 .
If Invalidity <= 19 % this amount becomes 1,239.5
Otherwise will be 2,479.0
I limit my view to this part and want that following parts are accessible from outside the code :
- the % may differ;
- the number of If's may differ;
- and the amounts may differ.
What do you understand under "the first (most fundamental) rule"? The only thing I have until now is a method that accepts a variable for the % of invalidity. This % is then tested in the method before the right value is returned. But, the question is how to obtain maximum flexibility, so the user is able to change not only the %, but also the number of if-cases and even the values that correspond with each %. For me, % and value could be treated as parameters, accessible from outside the code. This can be done in an easy way by reading an external text-file. But, my real concern is, how to get the possibility that also the number of if-clauses can increment or decrement...
Can you help me on the good way to solve this problem and provide me with some supplemental documentation on your suggestions?
Thanks in advance,
EDS
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
OK, let's start *really* simple. The simplest thing I can think of is a single value (no "if" at all) coming from a file. We could use some simple xml format:

How would you make that work?
Erwin DES
Greenhorn

Joined: Feb 26, 2003
Posts: 18
I could use following code :
a) public class Indemnification {
Double maxIndemnification = new Double(0);

/** Creates a new instance of Indemnification */
public Indemnification(Double maxIndemnification) {
this.maxIndemnification = maxIndemnification;
}

public Double getMaxIndemnification(){
return maxIndemnification;
}


} // class for the indemnification
b) The parser-class
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class MySAXParser extends DefaultHandler {
static final String PARSER = "org.apache.xerces.parsers.SAXParser";
Double maxIndemnification;
List indemnificationList;

StringBuffer b = new StringBuffer(); // collects text

public static void main(String[] args) {
// main receives the name of an XML file
if (args.length == 0) {
MySAXParser mp = new MySAXParser();
try {
mp.processFile("c:\\XMLTest\\indem.xml");
mp.listItems();
} catch (Exception e) {e.printStackTrace();};
}


}

public void processFile(String file) throws Exception {
// Parse an XML file
indemnificationList = new LinkedList();
XMLReader parser = XMLReaderFactory.createXMLReader(PARSER);
parser.setContentHandler(this);
parser.parse(file);
}

public void startElement(String uri, String localName, String qname,
Attributes attributes) {
System.out.println("startElement:" + qname);
b.setLength(0); // empty character buffer
if (qname.equals("indemnifications")) {

}
}

public void endElement(String uri, String localName, String qname) {
System.out.println("endElement:" + qname+":" + b.toString());
if (qname.equals("maximum")) {
double maxIndem = Double.parseDouble(b.toString());
maxIndemnification = new Double(maxIndem);
}

b.setLength(0); // empty character buffer

}
public void characters(char[] chars, int start, int length) {
// collect the characters
System.out.println("characters:" + new String(chars, start, length));
b.append(chars, start, length);
}

public void listItems() {
for (Iterator i = indemnificationList.iterator(); i.hasNext() {
Indemnification in = (Indemnification)i.next();
System.out.println(in.getMaxIndemnification().toString());
}
}

} // class to parse the xml-file

PS.: the code works, but I receive a java.lang.NumberFormatException ...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That doesn't look bad - I would only question the use of a SAX parser instead of something more elegant like DOM, DOM4J or the like.
The NumberFormatException is probably coming from the "," - try removing it from the file.
Also (if you didn't already do so), write yourself a tiny little script which feeds your code with the test file and asserts that you get the correct result. We will want to ran that test quite often and the easier it is, the better.
When we have that working, we need to decide about extending the format of the configuration file. How do we want to represent the "if"s?
Erwin DES
Greenhorn

Joined: Feb 26, 2003
Posts: 18
- It is not clear to me what you mean with a little script that feeds the code with the test file. Actually, I run the code in Sun One IDE.
- How do we want to represent the "if"s? : each if defines in fact a range. I could extend the xml-file with supplemental <range> </range>-tags I think.
But, even when I have the ability to change the xml-file at run-time (by adding some new ranges) I do not see clearly how to provide the same flexibility to the GIU. The user must have the ability to change the parameters in the code. So, my GUI for instance has provided 2 parameters (one for the case the range is <= 19% and one for the other cases).
If I add to the xml-file another range, this must also be reflected in the GUI.
I let you guide me into the real secrets for professional programming here.
EDS
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Erwin De Stercke:
- It is not clear to me what you mean with a little script that feeds the code with the test file. Actually, I run the code in Sun One IDE.

What we want is a way to run all our tests by pressing only one button and getting a message about success or failure as result. For testing to effective, it has to be automated as much as possible.
For this you could build something custom made, or use a framework like FitNesse or JUnit.

- How do we want to represent the "if"s? : each if defines in fact a range. I could extend the xml-file with supplemental <range> </range>-tags I think.
But, even when I have the ability to change the xml-file at run-time (by adding some new ranges) I do not see clearly how to provide the same flexibility to the GIU. The user must have the ability to change the parameters in the code. So, my GUI for instance has provided 2 parameters (one for the case the range is <= 19% and one for the other cases).
If I add to the xml-file another range, this must also be reflected in the GUI.

I guess I don't fully understand this requirement yet. What does the user need to do in the GUI? Change the parameters? Select one from a list? Or what else? Can you tell us a little story about what the user wants to do?

I let you guide me into the real secrets for professional programming here.


I am sorry to say that I won't be available to continue our discussion until monday, as I will be on a short vacation. Not that I really regret it...
Hopefully, some of the other experienced Ranchers will help out.
Erwin DES
Greenhorn

Joined: Feb 26, 2003
Posts: 18
I guess I don't fully understand this requirement yet. What does the user need to do in the GUI? Change the parameters? Select one from a list? Or what else? Can you tell us a little story about what the user wants to do?

The GUI has to permit the user to change some parameters used in the program.
This is a quite easy part I already wrote (but only using a simple flat-file for storing the paramter-data in case of XML). With parameters, I mean certain values that could change for a specific physical damage.
More difficult is that the program has also to be flexible enough work with %-ranges that might be added etc.
I try to explain with an example :
- When the % for Permanent Invalidity <= 10% => amount equals 700;
- When the % " " " <= 29% => amount equals 1000;
- Otherwise amount eqauls 1500.
This is the actual situation, but in a year this range of criteria could change. This possible change seems a problem to me in the case that also a new line is asked to be added. Suppose after <= 29 , a new row should be added like : When the % for permanent invalidity <= 45% => amount equals .... => thn I have 4 lines to test...
It is this kind of problem that troubles me. I want to avoid to change everytime again the code. If the code is written, it might be usefull if I could only add a special class for example. Is this possible in this case?
Or do you see another solution?
Thanks for any help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which pattern to use?
 
Similar Threads
Reading from multiple input sources and storing into multiple data sources
Design Patterns & J2EE
Good and Evil
Reading from multiple input-sources and storing into multiple data-sources
Challange to write a program