/Tag: Design Patterns

Singleton Pattern

Singleton Pattern The Singleton pattern allows you to take control of object creation by ensuring that only one instance of a class is instantiated. This can be useful when you want to restrict resource use, or you have a sensitive object whose data shouldn’t be accessed by multiple instances (a registery object for example). Imagine you have an object containing a large amount of reference data that you want to share across your application - you may want to ensure that you have only one instance of that object. Multiple instances of such a large object would unnecessarily increase the memory footprint of your application. Typically [...]

Observer Pattern

Observer Pattern The observer pattern allows objects to communicate and exchange information using notifications. An observer object registers with and receives notification from a subject object. Any number of observers can register with a subject and when an event occurs a notification is sent to each of the registered observers. The observer pattern allows loose coupling between the object triggering the event and the objects that respond to the event. The diagrams below should help explain this a little better. Figure 1.0 shows 2 observers registering with a subject.   Figure 1.1 shows the subject sending notifications to all  registered observers when an event [...]

Factory Pattern

Factory Pattern In this post I’ll be looking at the factory pattern. I’ll start off by looking at how the factory pattern is commonly used and then I’ll take a look at the strict GoF (Gang of Four) definition which is a little different and to be honest not as well known. The factory pattern is all about encapsulating object creation logic into a single object that can be used by other parts of your code. I’ll start off with an example that describes a scenario where the factory pattern could be put to work. Imagine we have an application that takes data from multiple sources [...]

Decorator Pattern

Decorator Pattern This is the first blog in a series that will focus on design patterns. I’ll be looking at some of the most popular design patterns in use today and  how they can  be used to improve the way you write code. I'll cover the key benefits of these patterns and use code examples to show you how they can be implemented. The decorator pattern attempts to address an important design principle, and that is to keep your code closed for modification but open for extension. The decorator pattern allows you to implement new logic with little or no change required to existing logic. [...]