Design patterns are covered in 5 categories.
1. Interfaces
* Adapter: Convert programming interface of one
class into that of another. It is used whenever we want unrelated classes to
work together in a single program. There are two ways to do this.
By Inheritance
It is used when client specifies its requirements in an
interface and subclasses an existing class.
Listing 1
using System;
public class AdapterPattern
{
static void Main()
{
//Non adapted Animal
Animal nonAdapted = new Animal("Lion","Wild");
nonAdapted.Display(); //Adapted Animal
Animal adapted = new AdaptedAnimal("Dog","Pet");
adapted.Display(); Console.Read();
}
}
public class Animal
{
public string name;
public string type;
public Animal(string name, string type)
{
this.name = name;
this.type = type;
}
public virtual void Display()
{
Console.WriteLine("\nNon-Adapted Animal name is '{0}' and type is '{1}'",name, type);
}
}
//Adapter
public class AdaptedAnimal : Animal
{
public AdaptedAnimal(string name,string type) : base(name,type) {}
public override void Display()
{
//Adaptee
Console.WriteLine("\nAdapted Animal name is '{0}' and type is '{1}'",name, type);
//Adaptee //Any thing done here will be adaptee
}
}
The main purpose of the above listing is to map the
interface of one class onto another so that they can work together. It demonstrates
that whether an Animal can be Adapted or Non-Adapted.
By Object Composition
It creates an object adapter that forwards a client's calls
to an instance of the existing class. Use this when the client does not provide
the interface it requires.
* Facade: Provide a simple interface into a
collection of classes
* Composite: Define an interface that applies to
individual objects and groups of objects.
2. Responsibility
* Singleton: A class of which only a single
instance can exist.
* Observer: A way of notifying change to a number
of classes.
* Proxy: An object representing another object.
3. Construction
* Factory Method: Creates an instance of several
families of classes.
* Abstract Factory: Creates an instance of several
families of classes.
4. Operations
* State: Alter an object's behavior when its state
changes.
* Command: Encapsulates a command request as an
object.
5 Extensions
* Iterator: Sequentially accesses the elements of
a collection.
*Decorator: Adds responsibilities to objects
dynamically.