Wednesday, 30 October 2013

Publish Subscriber vs Observer pattern

Publish Subscriber is related to the software applications running on different nodes and want to exchange data between them using Events and Notifications. Communication is done via web server that keeps track of all subscribers. When a user logs in his IP is registered to the web server this IP information is used route the events to the subscribers. But there must be handlers on the various subscribers those execute their logic. 

  • This how different applications hosted on different nodes communicate with one another.
  • This same event-handling mechanism is implemented for the objects with in the same application process.
  • This same event-handling mechanism is implemented for different processes executed under a process. This is how the inter process communication is implemented.

Friday, 25 October 2013

Decorator Design Pattern

  • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  • Problem

    You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class.

    Discussion

    Suppose you are working on a user interface toolkit and you wish to support adding borders and scroll bars to windows. You could define an inheritance hierarchy like …

    Rules of thumb

    • Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
    • Adapter changes an object’s interface, Decorator enhances an object’s responsibilities. Decorator is thus more transparent to the client. As a consequence, Decorator supports recursive composition, which isn’t possible with pure Adapters.
    • Composite and Decorator have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects.
    • A Decorator can be viewed as a degenerate Composite with only one component. However, a Decorator adds additional responsibilities - it isn’t intended for object aggregation.
    • Decorator is designed to let you add responsibilities to objects without subclassing. Composite’s focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert.
    • Composite could use Chain of Responsibility to let components access global properties through their parent. It could also use Decorator to override these properties on parts of the composition.
    • Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
    • Decorator lets you change the skin of an object. Strategy lets you change the guts.
    Decorator: Attaches responsibilities to objects at runtime. Decorators prove more flexible than inheritance, which attaches responsibilities to classes at compile time.

Proxy Design Pattern Notes

you may see similarities between the Decorator and Proxy design patterns. Both patterns use a proxy that forwards method calls to another object, known as the real subject. The difference is that, with the Proxy pattern, the relationship between a proxy and the real subject is typically set at compile time, whereas decorators can be recursively constructed at runtime. 

Proxy: Control access to an object with a proxy (also known as a surrogate or placeholder).
one use for the Proxy design pattern: accessing remote objects. Proxies also prove useful for creating expensive resources on demand, a virtual proxy, and for controlling access to objects, a protection proxy.

The proxy forwards that method call to the Web service, which often resides on a different machine than that of the client.

n situations where multiple copies of a complex object must exist, the proxy pattern can be adapted to incorporate the flyweight pattern in order to reduce the application's memory footprint. Typically, one instance of the complex object and multiple proxy objects are created, all of which contain a reference to the single original complex object. Any operations performed on the proxies are forwarded to the original object. Once all instances of the proxy are out of scope, the complex object's memory may be deallocated.
So it's quite a simple concept - to save on the amount of memory used, you might use a Proxy. Similarly, if you want to control access to an object, the pattern becomes useful.
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.
  1. virtual proxy creates expensive object on demand.
  1. protection proxy controls access to the original object.Protection proxies are useful when objects should have different access rights.
  1. smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
  1. Adding a thread-safe feature to an existing class without changing the existing class's code. 
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#mDPSt6h12coDuSLm.99

You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.


Rules of thumb


  • Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
  • Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
Provide a surrogate or placeholder for another object to control access to it. 


Related Patterns

  • Adapter Design Pattern - The adapter implements a different interface to the object it adapts where a proxy implements the same interface as its subject.
  • Decorator Design Pattern - A decorator implementation can be the same as the proxy however a decorator adds responsibilities to an object dynamically while a proxy controls access to it.


  • Java RMI as has been explained implements a remote proxy
  • Security Proxies that controls access to objects can be found in many object oriented languages including java, C#, C++.

  • The Proxy changes the behavior of the Service, but preserves its interface.
  • The Adapter changes the interface of the Service, but preserves it behavior.

  • The purpose of the Proxy is to add behavior. The purpose of the Facade is to simplify, which may actually involve removing behavior


Proxy vs. Adapter

  • The Proxy changes the behavior of the Service, but preserves its interface.
  • The Adapter changes the interface of the Service, but preserves it behavior.
  • A Client can use the Proxy or the Service Entity in the same way.
  • A Client designed to use the Adapter would not be able to use the Service Entity without it.
  • The Proxy can be cast to the interface of the Service. The Adapter can be cast to the interface the Client expects.

Adapter vs. Facade

  • The Adapter is used to preserve existing polymorphism. In other words, the interface you are adapting to probably already exists, and is probably already committed to.
  • The Facade provides a more idealized interface, but it can be developed incrementally as new needs of the subsystem are discovered.
  • Adapters are usually small, and therefore do not raise performance concerns, typically.
  • Facades tend to be large, and therefore it may be useful to make sure they are re-entrant (so as to avoid the need for multiple instances).
  • The "Adaptee" is probably fine as it is, you're using the Adapter because you've committed to a different interface.
  • The "Facadee" is probably not fine as it is, and you are communicating this to other developers by using the term "Facade".

Proxy vs. Facade

  • Proxies are optional, Facades typically are not.
  • The purpose of the Proxy is to add behavior. The purpose of the Facade is to simplify, which may actually involve removing behavior