Thursday, December 26, 2013

Design Pattern Basics



What is Design Pattern:

Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time. A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations.
There are majorly 3 categories of Design Patterns 


Design patterns origin ?

Although design patterns were probably used by programmers for many years before, design patterns were formally introduced in computer science in a book called “Design Patterns: Elements of Reusable Object-Oriented Software”. This book was published in 1994 and is still considered to be the bible of design patterns.
There are four people who wrote the book, and they are commonly referred to as the Gang of Four, or GoF (even Go4) for short – many times GoF is even used to refer to the book itself.

1) Creational Design Patterns
It deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.

Creational Design Patterns are composed of 2 points


a)      Encapsulate knowledge about which concrete classes use it.
b)      How Concrete classes are created and Combined



Below is list of Creational Design Patterns





No
Name
Basic Part
1)
Abstract Factory
which provides an interface for creating related or dependent objects without specifying the objects' concrete classes
2)
Factory
which allows a class to defer instantiation to subclasses
3)
Builder
Builder pattern builds a complex object using simple objects and using a step by step approach
4)
Prototype
It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
It refers to create duplicate object while keeping performance in mind
5)
Singleton
It a single class which is responsible to creates own object while making sure that only single object get created.
6
Object Pool
creational design pattern that uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand




2)  Structural Design Patterns 

There are design patterns that ease the design by identifying a simple way to realize relationships between entities.
Structural Patterns describe how objects and classes can be combined to form larger structures. The difference between class patterns and object patterns is that class patterns describe abstraction with the help of inheritance and how it can be used to provide more useful program interface. Object patterns, on other hand, describe how objects can be associated and composed to form larger, more complex structures.

Below is list of Structural Design Patterns

No
Name
Description  
1)
Adapter   
adapts' one interface for a class into one that a client expects.The Adapter design pattern lets you adapt what an object or class has to offer so that another object or class can make use of it.
2)
Bridge    
decouple an abstraction from its implementation so that the two can vary independently
3)
Composite  
a tree structure of objects where every object has the same interface . The intent of this pattern is to compose objects into tree structures to represent part-whole hierarchies.
4)
Decorator  
Decorator pattern allows to add new functionality an existing object without altering its structure. Wrapper Design Pattern
5)
Façade   
Make a complex system simpler by providing a unified or general interface, which is a higher layer to these subsystems.
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
6)
Flyweight
Use sharing to support large numbers of similar objects efficiently.
7)
Front Controller
The pattern relates to the design of Web applications. It provides a centralized entry point for handling requests.
8)
Proxy
Provide a surrogate or placeholder for another object to control access to it.
   
3)    Behavioral Design Patterns  

  Patterns that describe the ways objects and classes interact and divide
  responsibilities among themselves. A behavioral pattern abstracts an  action you want to take from the object or class that takes the action. By changing the object or class, you can change the algorithm used, the objects affected, or the behavior, while still retaining the same basic interface for client classes 

Behavioral object patterns use object composition rather than inheritance.  Behavioral patterns are those which are concerned with interactions between the objects. The interactions between the objects should be such that they are talking to each other and still are loosely   coupled. The loose coupling is the key to n-tier architectures.
Behavioral patterns are specifically concerned with identifying common interactions between objects and realizing these. This increases the flexibility by letting them talk to each other but still retaining the desired loose coupling between these objects.
  





No
Name
Description  
1)
Chain of responsibility   

Chain of responsibility is a design pattern where a sender sends a request to a chain of objects, where the objects in the chain decide themselves who to honor the request. If an object in the chain decides not to serve the request, it forwards the request to the next object in the chain.
2)
Command Pattern
A request is wrapped under a object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command.
Command objects encapsulate an action and its parameters
3
Interpreter Pattern

This pattern involves implementing a expression interface which tells to interpret a particular context. This pattern is used in SQL parsing, symbol processing engine etc.
Implement a specialized computer language to rapidly solve a specific set of problems
4
Iterator
This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.
5
Mediator
Mediator pattern is used to reduce communication complexity between multiple objects or classes
6
Memento
Memento pattern is used to reduce where we want to restore state of an object to a previous state.
7
Null Object
Designed to act as a default value of an object
8
Observer
Observer pattern is used when there is one to many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.
9
State Pattern
In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.
10
Strategy   
Algorithms can be selected on the fly
11
Template
An abstract class exposes defined way(s)/template(s) to execute its methods. Its subclasses can overrides the method implementations as per need basis but the invocation is to be in the same way as defined by an abstract class.
12
Visitor
As per the pattern, element object has to accept the visitor object so that visitor object handles the operation on the element object.
13
 Communications are handled by multiple layers, which form an encapsulation hierarchy
14
A task is scheduled to be performed at a particular interval or clock time
15
Optimize the implementation of a visitor that is allocated, used only once, and then deleted
16
Recombination business logic in a  Boolean fashion

 

 

Design Principles:

  


Basic Questions 

#)  Difference between Design patterns versus frameworks
Are design patterns the same thing as frameworks? The answer to that is no. Design patterns are more like general guidelines on how to solve specific programming problems, but they do not specify the detailed code that’s necessary to solve those problems.


#)  What are Design Principle

1.     Encapsulate what varies.

2.   Favor composition over inheritance

3.   Program to interface not to implementation

4.   Strive for loosely coupled designed between objects that interact

5.   Classes should be open for the extension, but close for the modification
 



jhfhjdfjhgdjhdfhgdfhjgh

No comments: