Friday, September 13, 2013

Proxy Design Pattern Example

Proxy Design Pattern: 

Proxy Design Pattern addresses a problem where access to real object needs to be controlled in a manner by which only authorized user can access the real object. Proxy pattern can also be applied to allow only limited access to real object.

Let’s take an example of banking system

Bank as a whole, is a very secure system, the access to which is restricted only to customers who holds an account with the bank. The access to the bank is controlled by bank personal such that if a bank customer wants to perform a transaction, then he will not be allowed to do so. The bank personal ensures that only customer who holds account with the bank is allowed to perform transaction.

The bank personal is in fact the proxy who controls access to the bank to the outside world. This proxy validates the authenticity of the customer before performing any transaction, on behalf of the bank.

If we have a class Bank which represents real Bank then we need to expose this Bank object to the client directly, so without proxy any non-bank client who has a valid account number can perform transaction with the Bank object which is not desirable.



Thursday, September 12, 2013

Facade Design Pattern Example

Facade design pattern: Facade design pattern provides structured way of designing a system by encapsulating sequence of operations required for business operation into a single interface.


In a complex system, performing a business operation would require interacting with different modules of the system, by calling method on each of the different module in a particular sequence. The facade design patterns abstracts all the interaction with different modules into a single simplified method. 

In simple words a single interface to perform multi-step process. So the client or the user has to only use this simplified method or interface to perform the business operation and the method/interface will internally take care of calling methods / steps on different modules. 

This way Facade Pattern defines a higher-level interface that makes the subsystem or sub modules easier to use.

How to use: Lets take an example of music player...


Visitor Design Pattern Example

Visitor Design Pattern: The visitor design pattern is a way of separating an some logic from an object structure on which it operates. Basically it allows you to add new operations on existing classes without modifying them, So visitor design pattern is a great way to provide a flexible design.

Note:  Visitor pattern has some flaws.
If we add a new class, the visitor class needs a new method. Furthermore, it is indeed likely that a new visiting method will need the definition of a new visitor patter, as well as a new accept method in every class of the hierarchy, visitor uses dual dispatching.

When to use: when you have a complex structure, i.e, a hierarchy, tree structure or something else that's not simply linear, you can apply the visitor to simplify the complex structure.

Lets understand it by one hierarchy problem example ...