Posts

Showing posts from November, 2011

Design Patterns Series 4 - Factory Pattern

Previous post explored decorator pattern  that take care of flows in standard OOP especially when it comes to inheritance, also this post will explore another pattern that serve the same concept. Factory Pattern : the factory design pattern lets you improve the new  operator by giving you a lot more flexibility when you create new object. When you use this pattern, you use your own code to create new objects, you don't use just the new  operator. To know how we can improve the new   operator consider you want to create a connection to MSSQL database, you will create it using the following line  Connection conn = new SQLConnection(); and when we want to connect to Oracle database we will use the following connection Connection conn = new OracleConnection(); but we want to make the default datebase is MySql so we need the next connection Connection conn = new MySqlConnection(); of course we will use one connection so we will choose the right connection according to dat

Design Patterns Series 3 - Decorator Pattern

In previous post  we explored Strategy Pattern, today we will explore another design pattern, that take care of flows in standard OOP especially  when it comes to inheritance. Decorator pattern : The decorator pattern is perfect for the opening scenario, it's all about extending the functionality of given class. After you have written a class you can add decorators(additional classes) to extend that class, that mean you will not have to keep modifying the original class's code over and over again. It allows you to write your code and avoid modification, while still extending that code if needed. So as much as possible design your core code so that  it doesn't have to be modified a lot, but may extended  as needed. If you want to write a code to produce a car with description as " this is a new car ", so the class car will be like the following public class Car { public Car() { } public String Description() { return "that is a new

Design Patterns Series 2 - Strategy Pattern

In the  Design Patterns Series 1 - Introduction  post we introduce the design patterns and why we need it. In this post we will explore the first design pattern in this series. Strategy Pattern: this pattern shows you how to extend the concept of OOP from "i s-a "    into " has-a " concept. we can take mammals as example, All mammals have legs and can walk but some mammals have two legs like human and other has four as horses but each one has its walking technique. From the programming view we can consider the  walk  method of mammal object print the sentence " I can walk"   public class Mammal { public Mammal() {} public void Walk() { Console.WriteLine("I can walk"); } } But these dose not distinguish between human and horse, to do human's  walk  method should print " I can walk using my two legs "   and horses  walk  method should print " I can walk using my fou

Design Patterns Series 1 - Introduction

In these series I will learn the design patterns with you. There are about 23 patterns (Factory, Decorator, Strategy, ... etc..). I will start with a small introduction about design patterns, then I will explain one or at most two patterns in each post with sample code in C#. What is the design pattern? In a simple words, design pattern is a tested solution to a standard programming problem. In other words, design patterns are solutions to programming problems that automatically implement good design techniques. Why do we need to learn design patterns?   When you faces a programming problem, instead of trying to solve it by your techniques. you should remember you have not to re-invent the wheel. this problem had been faced before and experts solved it and put general and tested solution for it. So if you know the design patterns you will use the appropriate pattern to solve your problem. Design patterns are intended to help you handle change as you have to adapt your code to