Posts

Showing posts from 2011

Design Patterns Series 9 - Adapter Pattern

In this post and coming posts, you will get inside scoop on patterns and OOP. Patterns have great deal to say about OOP and about how to improve your object-oriented experience.

Sometimes objects doesn't fit together as they should, a class may have changed, or an object turns out to be too difficult to work with. We will introduce you a rescue by converting two design pattern,Adapter Pattern and Facade Pattern.

The Adapter Pattern:

It lets you adapt what an object or class has to offer so that another object or class can make use of it. It lets you adapt what an object or class exposes to what another object or class expects.

Adapter pattern lets you fix the interface between objects and classes without to modify the objects or classes directly.

It lets you convert the interface of a class into another interface the clients expects. Adapter pattern lets classes work togthter that couldn't otherwise because of incompatible interfaces.

Although the official definition of the a…

Design Patterns Series 8 - Flyweight Pattern

This post will talk about another pattern is all about restricting object creation like Singleton Pattern, but this time it gives the rest of your code the feeling of multiple objects.

Flyweight Pattern :
It is called flyweight because instead of having to work with many massive, individuals objects, you reduce them to a smaller set of more generic objects, that can be configured at runtime to look like more plentiful, massive objects. Each massive object consumes system resources, by extracting what is generic from those massive objects and relying on runtime configuration to mimic those massive objects, you save the system resources.

You take all the specialized contents out of the massive objects to create flyweight objects. When you do that, you end up with more-generic objects, and you can reduce the number you need - possibly down to just one - which can be configured as need at run time to mimic the larger set of more massive objects.

The Flyweight pattern must use sharing to s…

Design Patterns Series 7 - Singleton Pattern

In the previous post we explained chain of responsibility pattern, this post we will be about taking control of the number of objects you have floating around in your code.There are two pattern, Singleton Pattern and Flyweight Pattern.

Singleton Pattern:
It is all about making sure that you can instantiate only one object of a particular class, if you don't use a pattern like this one,the new operator just keeps on creating more and more objects of the same class. Using singleton pattern make sure you only have one object, no matter how many times someone's code tries to create more objects.

You use it when you want to either restrict resource use, or when you have a sensitive objects whose data shouldn't be accessed by multiple instances. Also you can use it when you want to restrict the number of objects created because you want to share the data in those objects and you don't want to create multiple objects, which might confuse access to that data.

Creating a single…

Design Patterns Series 6 - Chain of Responsibility Pattern

In previous post we discovered one of design patterns that keep your object  know when something has happened and passing a notification to an observer, Observer Pattern, this post will explore another one.

Chain Of Responsibility Pattern
like observer pattern it lets you notify objects of change, but this time, the objects are connected in a chain, in series. The notification goes from one object to the next until an object is found that can handle the notification.

This pattern is all about connecting objects in a chain of notification; as a notification travels down the chain, it is handled by the first object that is set up to deal with the particular notification.

This pattern should "Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it."

You use this pattern when not all your observers are created equal. In other words…

Design Patterns Series 5 - Observer Pattern

We talked about Factory pattern in previous post ,in this post we will discover one of design patterns that keep your object  know when something has happened and passing a notification to an observer. Actually there is two design pattern do this job, Observer Pattern and Chain of Responsibilities pattern.

Observer Pattern :
It lets several observer objects be notified when a subject object is changed in some way, each observer registers with the subject and when a change occurs the subject notifies them all in parallel (yes at the same time).

The observer design pattern should "define a one-to-many dependency between objects so that when one object changes state, all its dependants are notified and updated automatically ".

You should consider the observer design pattern when you have an object that can cause events to occur - events that you want other objects to know about. Instead of writing everything in one monolithic class, consider breaking control out into a set of o…

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 database type by writi…

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 car";

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 "is-ainto "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 four legs". so we need to two walk meth…

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 new and unfo…

simple text formatter as jquery plugin

from about two months I created an account on the biggest questions & answers web site in the world at least in my view, stackoverflow of course any programmer met it in his search. I asked my questions and answered some other questions but the interesting thing was the formatting of questions and answers. They uses Markdown for that purpose.

I am not javascript expert but I tried to build my simple formatter to use it in my work, and with jquery I create my first jquery plugin, MiroDown is the name of it (narcissism I know).

Now I will provide MiroDown code and how to use it in your web page, let's start.

first thing your page should contains <textarea> to write and <div> to preview.
<textarea id="mytext" cols="70" rows="10"></textarea> <div id="preview"></div> but before going into more code I want to explore some sample of my formatter
*text*             ==>            text **text**           ==>…

Sort any Collection by a certain property using Insertion Sort Algorithm in VB.Net

This post is a just a Vb.Net Version of the code explained in this POST
<Extension()> _ Public Sub SortCollection(Of T)(ByRef items As List(Of T), ByVal propertyName As String, Optional ByVal sortDirection As String = "asc") Dim tmpItem As Object Dim value As New Object Dim value2 As New Object Dim j As Integer Dim insertItem As Boolean Select Case sortDirection.ToLower Case "asc" Try If items.Count > 1 Then Dim itemType As String = items(0).GetType().GetProperty(propertyName).GetValue(items(0), Nothing).GetType().ToString() For i As Integer = 1 To items.Count - 1 j = i - 1 insertItem = False tmpItem = items(i) value = items(i).GetType().GetProperty(propertyName).GetValue(items(i), Nothing) Do Until insertItem va…

Sort any Collection by a certain property using Insertion Sort Algorithm in C#

Sorting is a big topic to talk about , but in IT industry you need it many time every day. My leader asked me to sort a huge collection of very complicated items.My first try take about 2 mins this is catastrophe but I kept try and try till I found a nice sorting algorithm (of course for my case) which made the sorting time became about 2 secs. Insertion Sort algorithm you can find more about it here so I will give you a sample implementation for it using C#. you can find VB.Net version here.


first, I made it for a specific datatype and a specific property which I will order by but here it is the generic version using reflection and I used this keyword to call it as extension method of my collection..
public static void SortCollection<T>(this List<T> items, String propertyName, String sortDirection = "asc") { Object tmpItem; Object value = new Object(); Object value2 = new Object(); int j; Boolean insertItem; switch (sortDirection.ToLower(…