Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Version control problem

Status
Not open for further replies.

LuckyStarr

Programmer
Sep 12, 2002
22
BR
Hi,

I have a system written in Java that shares the same code for three different versions. The version logic is made using IFs, like the example:

Code:
if(currentRelease == ManagedProducts.SW_540){
 ...
}
else if(currentRelease == ManagedProducts.SW_530){
 ...
}
else if(currentRelease == ManagedProducts.SW_520){
 ...
}

This code snippet is spread all over the system code. However, when a new version is released, I have a big problem: modify each single if to update the version constants. There are a lot of them, near 100 ifs. It's very error prone.

My question is: Is there a design pattern that I could use to improve the code, to ease the new version's pain?

Thanks.

---
There is no Knowledge that is not power
---
 
It strongly depends on what kind of code is inside the ifs, but an option could be instantiating a class by reflection depending on the name of the version.

Cheers.

Dian
 
Do a Google on the Factory design pattern. It allows you to put the code that compares version into it's own component so you only have to change it in one place. All the code that would be inside your "if" statements would be in their own components.
Code:
   ---------         ---------
  | Calling | ----> | Factory |
  | Module  |       |         |
   ---------         ---------
                        / \
                       /   \
                      /     \
                     /       \
                    v         v
                 --------    --------
                | v5.3.0 |  | v5.4.0 |
                |  code  |  |  code  |
                 --------    --------
This way, the calling module doesn't need to know the messy details, it just needs to know how to call the factory.

When it comes time to add a new version, you update the factory module (change it's config file, or make code changes) so that it knows about the new version.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thank you guys!

I think that's what I was looking for.

I will study this pattern and try to organize this mess.

---
There is no Knowledge that is not power
---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top