Wednesday, 11 June 2014

Java Design Patterns Part - 3A

Design Pattern In Java

Spring-AOP                                                                                    Spring-Core

Behavioral : Thisdesign pattern explains about how objects will get interacted with other objects.
The commiunication between the objects is like that they should talk with each other but still they should be loosly coupled. Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns. A loosely-coupled class can be consumed and tested independently. Interfaces are powerful tool to use for loose coupling. So it is clear to say that loose coupling will help to avaoid dependencies.
  1. Chain of Responsibility Pattern : This design pattern belongs to Behavioral pattern. In software engineering the more stress we are giving on decoupling of objects. The main objective of this design pattern is when a sender sends a request to a chain of objects, where the objects in the chain decide themselves who to serve 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.
As per GOF : - "Gives more than one object an opportunity to handle a request by linking receiving objects together."
This design pattern allows a number of classes to attempt to handle a request, independently of any other object along the chain. Once the request is handled, it completes it’s journey through the chain. Extra handlers can be added or removed from chain in the middle or anywhere without modifying the logic inside any of concrete handler. In a chain of objects, the responsibility of deciding who to serve the request is left to the objects participating in the chains. This is very similar to ‘passing the question in a quiz scenario’. When the quiz master asks a question to a contestent & if he doesn’t know the answer, he passes the question to next contestent and so on. When one contestent answer to that question, the passing flow stops. Sometimes, the passing might reach the last contestent and still nobody gives the answer.
Points to remember while using Chain of Resposibility Pattern :
  • Sender is not aware of that which object in the chain will serve its request.
  • Every node in chain will have the responsibility to decide, if they are able to serve the request.
  • If node/chain decides to forward the request, then it should be capable of choosing the next node and forward the request to that.
  • There is a possibility where none of the node may serve the request.
Example : -
  • Real time example for this design pattern is Service desk support for any bank.
  • Chain of Resposibility Pattern references in JDK is

java.util.logging.Logger#log() : -
If the logger is currently enabled for the given message level then the given message is forwarded to all the registered output Handler objects available in the chain.

javax.servlet.Filter#doFilter() : -
The Servlet doFilter method of the Filter class is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

       2.  Command Design Pattern :- This design pattern falls under behavioral design pattern. In this design pattern, an object is used to represent and encapsulate all the complete information which is required to call a method after some time. The information which that object contains includes the method name, the object that owns the method and values for the method parameters. Particularly it defines a manner for controlling communication between classes or entities.The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object.
A command object has a receiver object and invokes a method of the receiver in a way that is specific to that receiver's class. This command object is separately passed to an invoker object, which invokes the command, and optionally does logging about the command execution. Any command object can be passed to the same invoker object. Both an invoker object and several command objects are held by a client object. The client contains the decision making about which commands to execute at which points. To execute a command, it passes the command object to the invoker object.

As per GOF :- “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undo able operations.”
There are some terms which are related to this design pattern, which are invoker, receiver, command & client.
  • Command is an interface which is having a execute() method. So it means that this interface declares a method for executing a specific action. This provides the base class for all objects
  • An invoker instructs the command to perform an action or the invoker object initiates the execution of commands. The invoker could be controlled by the client objects.
  • A client creates an instance of the command implementation and then associates/links it with the receiver.
  • A command implementation class instance creates a binding between the receiver and an action.
  • Receiver is the object that knows the actual steps to perform the action or we can say that Receiver objects contain the methods that are executed when one or more commands are invoked.
Command Pattern can be used for the following situations:
  • If we want to parameterize objects by an action to perform.
  • If we need to specify, queue, and execute requests at different times.
  • When a set of changes to data need to be encapsulated as a single action or transaction.
Example:-
A Command pattern is also known as an action pattern or transaction pattern.
  • The real world example for the command pattern is the idea of table order at a restaurant. Restaurent waiter takes the order, which is a command from the customer.This order is then queued for the kitchen staff, then waiter tells the chef that the a new order has placed, and the chef has complete information to cook the ordered dish.

Another Example:-
  • Struts controller uses the Command Design Pattern.
  • All implementations of javax.swing.Action
  • All implementations of java.lang.Runnable

       
       3.   Interpreter Design Pattern :- The interpreter pattern is used to define the grammar for instructions that form part of a language or notation, at the same time allowing the grammar to be easily extended. It means that design pattern is used to define a grammatical representation for a language and provides an interpreter to deal with this grammar. This design pattern is useful for simple languages where performance is not critical. It's used to manage algorithms, relationships and responsibilities between objects. 
Few terms which relate to this design pattern :
  • Context, Client, Abstract Expression, TerminalExpression and NonTerminalExpressions.
  • Context :- This contains information which is global to the interpreter.
  • Client :- This build the Abstract Syntax tree or AST is passed through the client.
  • AbstractExpression :- This provides an interface for executing an operation.
  • TerminalExpression :- This implements the interpret interface associated with any terminal expressions in the defined grammer. This will not contain other expression, the leaf node in a tree
  • NonTerminalExpressions :- This will contain other expression, non-leaf node in a tree.

An AST( Abstract Syntax tree) is composed of both TerminalExpressions and NonTerminalExpressions.

As per GOF :- “Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.”
  • Real Time example:-
Java compiler which interprets the java source code into byte code and which is understandable by JVM is the best example for this design pattern. Google Translator is another known example for this pattern where the input can be in any language and we can get the output interpreted in another language.

Examples related to Java :-
  • SQL parsing is using this design pattern.
  • All subclasses of java.text.Format
  • All subclasses of javax.el.ELResolver
  • java.util.Pattern
  • java.text.Normalizer 


            4.    Iterator design pattern: - This is the commonly used design pattern in Java. This pattern is used to manage algorithms, relationships and responsibilities between objects. This pattern allows to traverse through a collection of objects without exposing the underlying collection. As this pattern helps to hide the actual implementation of traversal. Iterator pattern is widely used in Java Collection Framework where Iterator interface provides methods for traversing through a collection objects.

As per GOF :- Provides a way to access the elements of an aggregate object without exposing its underlying represenation.”
  • Iterator example in Real world :- Mp3 player control is very good example of this design pattern.
  • Iterator example in Java :- As we know that this is the widely used design pattern in Java collection framework.
  • All implementations of java.util.Iterator (thus among others also java.util.Scanner!).
  • All implementations of java.util.Enumeration
                5.     Mediator design pattern :- In order to have a good object oriented design framework, we have to create multiple objects and classes and these classes interacting with each other. Mediator pattern is used to reduce communication complexity between multiple objects or classes. It provides loose coupling by providing a mediator class which normally handles all the communications between different classes and also supports easy maintainability of the code.

As per GOF:-Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.

a) Mediator example in Real world :- Air traffic controller (ATC) is a mediator between multiple flights. It helps in communication between flights and co-oridates/controls landing & take-off. Two flights need not interact directly and there should not be any dependency between them. This dependency is solved by the mediator ATC.

b) Mediator example in Java :-
  • java.lang.reflect.Method#invoke()
  • java.util.Timer (all scheduleXXX() methods)
  • java.util.concurrent.Executor#execute()
  • java.util.concurrent.ExecutorService (the invokeXXX() and submit() methods)
  • java.util.concurrent.ScheduledExecutorService (all scheduleXXX() methods)
             6.    Memento design pattern :-Memento means anything serving as reminder. This pattern helps to store an object's state so that this state can be restored at a later point. The saved state data in the memento object is not accessible from outside of the object to be saved and restored.
In this pattern, an Originator class represents the object whose state we would like to save. A Memento class represents an object to store the state of the Originator. The Memento class is typically a private inner class of the Originator. As a result, the Originator has access to the fields of the memento, but outside classes do not have access to these fields. This means that state information can be transferred between the Memento and the Originator within the Originator class, but outside classes do not have access to the state data stored in the Memento.

The memento pattern also utilizes a Caretaker class. This is the object that is responsible for storing and restoring the Originator's state via a Memento object. Since the Memento is a private inner class, the Memento class type is not visible to the Caretaker. As a result, the Memento object needs to be stored as an Object within the Caretaker.

As per GOF :-Captures and externalizes an object's internal state so that it can be restored later, all without violating encapsulation.

a) Memento design pattern in Real world :- We have seen option in cellphone i.e. Restore phone data with factory settings
Other example :- Undo or backspace or ctrl+z is one of the most used operation in an editor. Memento design pattern is used to implement the undo operation.

b) Memento design pattern in Java :-
  • java.util.Date (the setter methods do that, Date is internally represented by a long value)
  • All implementations of java.io.Serializable
  • All implementations of javax.faces.component.StateHolder
Uses of Memento design pattern :- The Memento pattern is useful when we need to provide an undo mechanism in our applications, when the internal state of an object may need to be restored at a later point of time. Using serialization along with this pattern, it's easy to preserve the object state and bring it back later on.
                                                                                                      Rest of the behavioral design pattern will be in next part


Web-Services                                             Spring-MVC                                         Interview-Questions

No comments:

Post a Comment