Showing posts from June, 2012

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 and converts that data into a common data standard. We’d probably use a data adapter of some sorts to convert the incoming data to our common data standard. To begin with were going to receive data in CSV format so our code for creating a data adapter object might look something like this. DataAdapter csvAdapter = new CsvDataAdapter(); Imagine requirements change and we’re told that we also have to handle XML data from another sou…

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. This achieved by ensuring that new task specific logic is encapsulated by individual objects rather than being spread across your code. This encapsulation of task specific logic makes code much easier to maintain going forward. The decorator pattern allows you to use wrapper objects to extend existing code with new behaviour being delivered by augmenting new logic to existing code at runtime. The following example des…