In my last 2 post i tried to give an idea about singleton and factory design pattern. Toady i would like to share the idea regarding “The observer pattern”. It gives you another way to avoid tight coupling between components. This pattern is simple: One object makes itself observable by adding a method that allows another object, the observer, to register itself. When the observable object changes, it sends a message to the registered observers. What those observers do with that information isn’t relevant or important to the observable object. The result is a way for objects to talk with each other without necessarily understanding why.
A simple example is a list of users in a system. The following code shows a user list that sends out a message when users are added. This list is watched by a logging observer that puts out a message when a user is added.