Class Design – Calling the Same Method on Different Classes from One Place

class-designinterfacesobject-oriented

Let me introduce my situation:

I have Java EE application and in one package, I want to have classes which will act primarily as cache for some data from database, for example:

  • class that will hold all articles for our website
  • class that will hold all categories
  • etc.

Every class should have some update() method, which will update data for that class from database and also some other methods for data manipulation specific for that data type.

Now, I would like to call update() method for all class instances (there will be exactly one class instance for every class) from one place.

What is the best design?

Best Answer

You should seriously consider using the observer pattern and, rather than calling an Update method which iterates through a list of objects that need an Update, you simply call an event to which all of your objects are subscribed for as long as they are valid to be updated?