Friday, August 10, 2007

Purpose behind using Interfaces

• Implementing an interface allows a class to become more formal about the behavior it promises to provide
• Interfaces form a contract between the class and the outside world, and the contract is enforced at build time by the compiler
• If a class implements an interface it must implement all the methods the implemented interface defines
• All the methods and properties defined in Interface are by default public and abstract

Design Pattern > Behavioural > Observer

• As an Object’s state changes, it is often necessary for other objects to be notified when this happens
• In C#, Delegates and Events are based on Observer pattern


Design Pattern > Structural > Iterator

ITERATOR PATTERN
• Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying operations
• In .net System.Collection assembly classes support all the functionality of the iterator pattern through a call to GetEnumerator(), which returns a concrete iterator, the Ienumerator interface

Design Pattern > Structural > Proxy

PROXY PATTERN
• Proxy is a way to “Provide a surrogate or placeholder for another object to control access to it..”
• Proxy helps in deferring creation of objects till not needed
• Use of Proxy means using a lightweight placeholder in lieu of expensive object

Design Pattern > Structural > Facade

FACADE PATTERN
• Provides a unified interface to a set of interfaces in a subsystem
• Facade defines a higher level interface that makes the subsystem easier to use
• Facade knows which subsystem classes are responsible for a request
• Delegates client requests to appropriate subsystem objects
• In coding prespective, Facade class holds objects of all the subsystems under it

Design Pattern > Creational > Factory



• A Factory Pattern is one that returns an instance of one of several possible classes depending on the data provided to it
• Usually all the classes it returns should have a common base class and common methods, but implementations of the methods may be different
• Following is an UML representation of the Factory Method Pattern. Note that no instances of Class derived1 or derived2 are directly created
• Factory class’ doIt() method is used to get the objects, and object type is determined on the basis of information passed to getObject() method

Design Pattern > Creational > Singleton

SINGLETON
• Singleton design pattern is used when only one instance of an object is needed throughout the lifetime of an application
• The Singleton Class is instantiated at the time of first access and the same instance is used thereafter till the application quits
• Singleton Class can be used in various places where one would need a common repository of information that can be accessed from all objects in an application
• For example sometimes only a single database connection object or network connection object may be needed by the application