Introduction
This article describes design patterns for software development. Basically there are the following three categories of design patterns:
- Creational
- Behavioral
- Structural
What is a Design pattern?
Christopher Alexander says, "Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.". Even though Alexander was talking about patterns in buildings and towns, what he says is true about object-oriented design patterns. Our solutions are expressed in terms of objects and interfaces instead of walls and doors, but at the core of both kinds of patterns is a solution to a problem in a context.
Let's proceed with getting some idea of all these patterns.
Creational Pattern
Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented. A class creational pattern uses inheritance to vary the class that's instantiated, whereas an object creational pattern will delegate instantiation to another object.
Creational patterns become important as systems evolve to depend more on object composition than class inheritance. As that happens, emphasis shifts away from hard-coding a fixed set of behaviors toward defining a smaller set of fundamental behaviors that can be composed into any number of more complex ones. Thus creating objects with specific behaviors requires more than simply instantiating a class.
Behavioral Pattern
Behavioral patterns are concerned with algorithms and the assignment of responsibilities among objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication among them. These patterns characterize complex control flow that's difficult to follow at run-time. They shift your focus away from the flow of control to let you concentrate just on the way objects are interconnected.
Structural Pattern
Structural patterns are concerned with how classes and objects are composed to form larger structures. Structural class patterns use inheritance to compose interfaces or implementations. As a simple example, consider how multiple inheritance combine two or more classes into one. The result is a class that combines the properties of its parent classes. This pattern is particularly useful for making independently developed class libraries work together.
Let's have a look at a creational pattern.
A few popular examples of creational design patterns are:
- Singleton design pattern
- Factory design pattern
- Prototype design pattern
Singleton Design Pattern
A singleton pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance and provides a global point of access to it.
A singleton pattern is used when there must be exactly one instance of a class and it must be accessible to clients from a well-known access point.
When the sole instance should be extensible by subclassing and clients should be able to use an extended instance without modifying their code.
Let's have a look at the following code snippet.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- using System.Data.SqlClient;
- using System.Data;
- using System.Diagnostics;
-
- namespace Sample
- {
-
-
-
- public class SingleTone
- {
-
- private SingleTone()
- {
-
- }
-
- private static SingleTone Single;
-
- public Int32 Statcount { get; set; }
-
-
- public static SingleTone ReturnObject()
- {
- if
- (Single != null)
- {
- return
- Single;
- }
- else
- {
- Single = new SingleTone();
- return
- Single;
- }
-
-
- }
-
- }
-
- class Program
- {
-
- static void
- Main(string[] args)
- {
- SingleTone s = SingleTone.ReturnObject();
- s.Statcount = 200;
-
- SingleTone s1 = SingleTone.ReturnObject();
- s1.Statcount = 300;
-
- Console.WriteLine(s.Statcount);
- Console.ReadLine();
-
- }
-
- }
-
- }
Summary
In this article we have learned about design patterns in C# and a basic example of the Singleton Design Pattern. In my future articles we will see some more examples of design patterns.