Thursday 24 April 2014

Observer Design Pattern

What???
  • Observer pattern comes under Behavioral Design Pattern ,As this patterns deals with How the objects and classes sends messages to each other....
  • In observer design pattern multiple observer objects registers with a subject for change notification. When the state of subject changes, it notifies the observers. Objects that listen or watch for change are called observers/listeners and the object that is being watched for is called subject.
  • Observer pattern is used when there is one to many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically.



Applicability

The observer pattern is used when:
  • The change of a state in one object must be reflected in another object without keeping the objects tight coupled.
  • The framework we are writing needs to be enhanced in future with new observers with minimal changes.
Some Classical Examples:

Model View Controller Pattern - The observer pattern is used in the model view controller (MVC) architectural pattern. In MVC the this pattern is used to decouple the model from the view. View represents the Observer and the model is the Observable object.

Event management - This is one of the domains where the Observer patterns is extensively used. Swing and .Net are extensively using the Observer pattern for implementing the events mechanism.

Example:

  • Lets assume that 3 or 4 users want to buy a product in one website called flipcart.
  • But currently that product is not available/out of stock state and It provide a option like NotifyMe When Product available.So These users will register into this site for notification.
  • After few days The Product is available in that website,so Perticular Notify method will send the notification to the all Registered users via email that the product they requested is availableNow and you can shop now.

    Note that in this example subject : Product Information,Observers:Users




Implimentation :


                     will update soon.......

Proxy DesignPattern

What Is PROXY?


  • Someone who takes the place of someone else is known as a proxy.
  • In Proxy pattern, a class represents functionality of another class. This type of design pattern comes under structural pattern.
  • Proxy design pattern allows you to create a wrapper class over real object. Wrapper class which is proxy, controls access to real object so in turn you can add extra functionalities to real object without changing real object's code.


  • Example:
  1. If a Person has account in a bank and he want to withdraw some 5000 from bank.If it is direct process ,He has to go to bank and fill the withdraw form and the employee check for the account balance and then process the withdraw request.
  2. Second Process:,the Person directly go to an ATM and enter pin and amount ,the machine checks the account details and send info to bank .and process the request.Here ATM is not the Real bank or real object to work on.
When?

Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer. Here are some situations when proxy pattern is applicable.

  1. remote proxy provides a local representative for an object in a different address space providing interface for remote resources such as web service or REST resources.
  2. virtual proxy creates expensive object on demand.
  3. protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.
  4. smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
  5. Adding a thread-safe feature to an existing class without changing the existing class's code.

How??


will update soon...


                 


Factory Design Pattern

What??

  • In this,we create object without exposing the creation logic to the client and refer to the newly created object using common interface 
  • At run time ,we get the object of the type based on the parameter we pass.
  • Actually we use this Factory design pattern in the situation like,If we have a super class and subclasses and based on the data provided we have to return the object of one of the subclasses.


will update soon.........