O'Reilly Forums: I Am Trying To Figure Out What Pattern Applies To This Problem - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

I Am Trying To Figure Out What Pattern Applies To This Problem

#1 User is offline   mikener 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 01-July 08

Posted 12 February 2010 - 01:44 PM

I need to be able to conditionally execute a method. I prefer not having a bunch of IF statements throughout my code for several reasons, the most notable is that at a certain point in the future the method will no longer be used.

I am not sure of the best way to do this or what pattern(s) I should choose to accomplish this task.

The application I refer to is an application that is going to replace a legacy system. The legacy code will be turned off and no longer used at some point. Once that point of time comes, I don't want to have to go back and change any code (if at all possible).

The following is a fictious conceptual example in psuedo of what I mean:

NewSystemEmployee.Save(Employee e)

if (Legacy System Is Running)
LegacySystemEmployee.Save(Employee e)

The method NewSystemEmployee.Save always needs to execute. I only want to execute LegacySystemEmployee.Save as long as the Legacy system is running. Once the Legacy system is shutdown, I no longer want to execute LegacySystemEmployee.Save

Once the legacy system goes away, I don't know how I can do this without

a. Creating an IF statement before I call LegacySystemEmployee.Save OR
b. Removing every call to LegacySystemEmployee.Save method OR
c. Changing LegacySystemEmployee.Save method so that it is a stub and nothing more

I also have a requirement that the NewSystemEmployee class does not refer in any way to the LegacySystemEmployee class.

Any suggestions?

Thanks so much

#2 User is offline   Mr Yossu 

  • New Member
  • Pip
  • Group: Members
  • Posts: 9
  • Joined: 09-November 10

Posted 09 November 2010 - 08:44 AM

I'm no expert, but I would have thought that a Strategy would have done this for you. I think the point of that pattern (related to how you might use it) is to allow you to choose what algorithm to use for a problem.

For example, I have seen Strategy discussed regarding sorting arrays. You can have a few different sorting algorithms encapsulated in their own classes, and the Strategy allows you to choose one without an "if" statement everywhere you might need to sort. This sounds a lot like what you want to do.

I guess you could have a single configuration setting somewhere that specified if the legacy system is still running, and every instance of the Strategy would check that and return either the legacy class or the new one depending. When you kill off the legacy system, you can leave the Strategy there in case you ever need it.

Hope this helps.

#3 User is offline   Sayan Mukherjee 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 15-June 11

Posted 21 June 2011 - 08:15 AM

I agree with Mr Yossu.
Hi mikener,

I am saying nothing new compared to what Mr Yossu said - I am just attaching a skeleton application to show the implementation of the idea.

I have used the Factory Method to create the employee system objects each of which has the save() method. The run time choice for which save() method to run is taken care of by the Strategy pattern. This is implemented via the interface IEmpSystem andthe classes CombinedEmpSystem and NewEmpSystem.

The client code (the main() method in this case) has the only dependency - the state of the system. It is passed on the command line to the application as a string - "C" for Combined (i.e. both Legacy and New) and "N" for just New.

You can replace this command line arg by a config file where you store the indicator "C" as long as the Legacy is running. Once the Legacy is retired, update the config file to store "N" and restart the application.



Attached File(s)


Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users