Friday, November 20, 2009

Design Patterns : The Singleton Pattern

Why Singleton?

Sometimes we want just a single instance of a class to exist in the system. For example, we want just one database manager instance. We need to have that one instance easily accessible and we want to ensure that additional instances of the class can not be created.

What is Singleton Pattern

The singleton is a useful design pattern that is used to restrict instantiation of a class to one object.  It ensures a class has only one instance and provide a global point of access to it.  In the singleton pattern at any given point only a single instance of the Singleton Class is active. All instantiation of the Singleton Class references the same instance of the Class. The pattern also provides a global point of access to the sole instance of the class.

The Sample Code
Class Singleton
{
//Class Members
private static $instance;

//The Constructor
protected function Singleton() {}

//Static method for creating the single instance of the Constructor
public static function getInstance()
{
// create a new instance if not already done
if( $instance == null )
$instance = new Singleton();

// return the instance of the Singleton Class
return $instance;
}
}
Benefits of Singleton Pattern:   
  • Instance control: It ensures that all objects access the single instance, by preventing other objects from instantiating their own copies of the Singleton object.
  • Flexibility: The class has the flexibility to change the instantiation process. Because it controls the instantiation.
  • As Global variables: Singletons are often preferred to global variables because -   
    • They don't pollute the global namespace with unnecessary variables.
    • They allow lazy allocation and initialization, where global variables in many languages will always consume resources.
Drawbacks of Singleton Pattern:   
  • It is usually not recommended to use global objects, since it is hard to test them and the developer has to make assumptions regarding the application and how they will be used.
  • Using the singleton pattern makes the classes tightly coupled instead of loosely coupled, meaning that you can only test a few classes together rather than each of the classes at a time, as you should.
When should you use the Singleton design pattern:

Here’s a singleton recommended check list:   
  • Singleton should be used in cases where only one object is needed to coordinate actions and/or events across the application.
  • The application will always use the object in the same way.
  • It doesn’t really matter to the client what’s going on in the rest of the application, meaning that the singleton object is independent.

If your requirement confirms all these points, then it’s a good candidate to use the singleton pattern.

Tuesday, November 10, 2009

OO Design Principles

Here are some important principles of Object Oriented Design
       
  • Identify the aspects of your application that vary and seperate them from what stays the same.
  •    
  • Take what varies and encapsulate it so it won't affect the rest of the code.
  •    
  • Program to an interface, not an implementation.
  •    
  • Favour composition over inheritance.
  •    
  • Strive for loosely coupled designs between objects that interact.
  •    
  • Classes should be open for extension but closed for modification.
  •    
  • Depend on abstractions, do not depend on concrete classes.
  •    
  • Talk only to your immediate friends.
  •    
  • A class should have only one reason to change.

Saturday, June 20, 2009

An Introduction to Design patterns

What are design pattrens

Design patterns are commonly defined as time-tested solutions to commonly occurring problems in software design. Design Patterns aren't invented they are discovered. They don't give you code, they give you general solutions to design problems. A design pattern is not a finished design that can be transformed directly into code. A design pattern provides a description for how to solve a problem that can be used in different situations.

Design patterns are grouped mainly into three categories:

Creational patterns:

Creational Patterns deals with object creation mechanisms, they try to create objects in a way that is suitable to a given situation. They are also used for dynamically selecting the class of created objects. Some examples of creational pattrens are
  • AbstractFactoryPattern
  • BuilderPattern
  • FactoryMethodPattern
  • SingletonPattern

Structural patterns:


Structural Patterns are those patterns that helps realizing relationships between entities by identifying simple ways for it, these patterns use various language mechanisms for assembling objects and code structuring. Some examples of structural pattrens are
  • AdapterPattern
  • CompositePattern
  • DecoratorPattern
  • FacadePattern
  • IteratorPattern

Behavioral patterns:


Behavioral Patterns are patterns that identify common communication patterns between objects and realize these patterns. They usually describe how a group of objects collaborate to perform some task that no single object can perform alone. Some examples of behavioral pattrens are
  • CommandPattern
  • IteratorPattern
  • ObserverPattern
  • StatePattern
  • StrategyPattern
  • TemplateMethodPattern
  • VisitorPattern

Benefits of Design patterns:

  • Design Patterns provide a way to solve issues related to software development using a proven solution.
  • Design patterns helps to prevent subtle issues that can cause major problems.
  • Design Patterns facilitates the development of highly cohesive modules with minimal coupling.
  • Design Patterns isolate the variability that may exist in the system requirements, making the overall system easier to understand and maintain.
  • Design Pattern describes the problem, the solution, when to apply the solution, and its consequences.
  • Design Patterns also gives implementation hints and examples.
  • Design Patterns improves code readability for coders and architects who are familiar with the patterns.
  • Design Patterns allow programs to share knowledge about their design.