Imagine having a large-scale Ajax application with dozens of objects. In this application, you would like to add an object that handles all your Ajax updates. We will call this object the AjaxUpdater. All the objects in your web application will communicate through this object if they need to make an XHR at any time. Not only will this object handle all the requests, it will also handle delegating the responses to specified callback methods. In other words, when the response has been received, the AjaxUpdater will notify the requesting object if a callback method was specified during the request. This situation would require the use of the Singleton pattern because we would need a consistent object reference for the dozens of objects in our application that might be making XHRs. This pattern will also help separate this important functionality into a specific object for more flexibility, and help keep the web application decoupled for easy updates and future additions to the code. It might seem odd at first to separate your code into so many different objects, but as your application grows it will keep your code extremely easy to update and save you a lot of time in the long run. |