Design Pattern : Part 2


Earlier in Design Pattern : part 1, We have seen how we can use creational and structural design patterns. In this blog we will cover behaviour design patterns. This design patterns are equally important compare to earlier two and that's the reason it is asked frequently in java interview. The important design patterns are Visitor, Strategy, Command and Observer. Personally I have faced these questions in interview- How we can perform new operation in class without any change in class? Which design pattern is used for undo and redo in text formatter? How to notify object property change to a number of classes? 


Chain of Responsibility:
   This is a way of passing a request between a chain of objects. In this pattern there is a single chain of passing a request with many handlers. Each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain. This pattern is used to process those requests that are varied.


Command:
   In this pattern we encapsulate a command request as an object. To call a method later we need some information like method name, its object and its values, to represent that information command pattern is used. It allows a request to be saved in a queue. The Command pattern has three main components: the Invoker, the Command, and the Receiver. 


Interpreter:
  This is a way to include language elements in a program. Interpreter pattern defines how the communication between the entities is controlled in a good manner and also defines the instructions grammar. The interpreter design pattern is useful for simple languages where performance is not critical.


Iterator :
  It helps us sequentially access the elements of a collection. This pattern is used to provide the way to access the aggregate object without any knowledge of Aggregate structure. The iterator object will maintain the state of the iteration, keeping track of the current item and having a way of identifying what elements are next to be iterated. This allows traversing of lists, trees and other structures in a standard manner.


Mediator :
  In this pattern we define simplified communication between classes. Mediator design pattern also deals with communication and defines an object to encapsulate that how the set of an object should interact. It promotes many-to-many relationships between interacting peers and thus defines simplifies communication between classes.


Memento :
  In this pattern we capture and restore an object's internal state. This pattern proves to be helpful in saving the internal state of the object in to external state and enabling us to restore the state later when needed means it easily restore an object to its previous state with minimal lines of coding. Memento pattern helps us to store a snapshot which can be reverted at any moment of time by the object. A word processor providing undo operation or Ctrl+Z is the example of this pattern.


Observer :
  In this we can find out a way of notifying change to a number of classes. This pattern allows us to plug objects into a framework at runtime, which allows for highly flexible, extensible, and reusable software. It is used when it is necessary to ensure that multiple components (called observers or subscribers) are kept in sync with a master set of data (the subject or publisher).



State:
  State Pattern allows an object to alter its behavior dynamically according to its internal state. For this we directly move the logic to property "State" which has method Handle() and call this method. 


Strategy:
  It is useful when we want to encapsulates an algorithm inside a classes. The strategy pattern is very useful when we are in a need of a dynamic formula or data. It is used to separate different types of algorithms that serve a similar purpose. It's a strategy of dealing with a code design with a purpose to decouple concrete code into separate classes, which promotes reusability. 



Template Method:
 The main intention of this This pattern helps us to improve code reusability. It lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. We cannot override the template method. Only the steps given in the skeleton of algorithm of template method need to be overridden in concrete classes.


Visitor :
With the help of visitor pattern the new operations are performed on an existing structure. The visitor pattern also specifies how iteration occurs over the object structure. It is a classic technique for recovering lost type information. 

  • We are covering here -'Java garbage collection interview questions' or 'Java memory interview questions' in d...
  • Java util.collections is one of the most important package in java and the important of this package can be understand by...
  • Java Concurrency interview question - In year 2004 when technology gurus said innovation in Java is gone down and Sun Microsystems [Now Or...
  • 'Java investment bank interview' generally contains 'Java Design Pattern' questions. If you want to be a professional Java ...
  • 10 comments:

    1. ->Its really helpful for me to understand where we i lost in my previous interview. Thanks.

      If anyone wants to Learn Java in Chennai go to the Besant Technologies which is No.1 Training Institute in Chennai

      ReplyDelete
    2. This comment has been removed by the author.

      ReplyDelete
    3. I would like more information about this, because it is very nice., Thanks for sharing.
      Signature:
      i like play happy wheels demo online and play happy wheels games full and friv4school , games for girls , games go girls

      ReplyDelete
    4. The article gives me a lot of interesting information. I still often share life experiences with friends. And I enjoyed the convenience and attractiveness of free messaging app
      whatsapp baixar , baixar whatsapp , baixar whatsapp gratis

      ReplyDelete
    5. vidyasthali is one of the best law college in India

      Vidyasthali Law College

      ReplyDelete

    6. vidyasthali is one of the best law college in India
      Vidyasthali Law College is a self-financing Institution affiliated to the University of Rajasthan

      law college
      law college in Jaipur
      Best law college in Jaipur
      Top College Of law In Jaipur
      Vidyasthali Law College in jaipur

      ReplyDelete