What is the use of Interceptor design pattern?

Interceptor design pattern is used for intercepting a request. Primary use of this pattern is in Security policy implementation.

We can use this pattern to intercept the requests by a client to a resource. At the interception we can check for authentication and authorization of client for the resource being accessed.

In Java it is used in javax.servlet.Filter interface.

This pattern is also used in Spring framework in HandlerInterceptor and MVC interceptor.

What are the Architectural patterns that you have used?

Architectural patterns are used to define the architecture of a Software system. Some of the patterns are as follows:

  • MVC: Model View Controller. This pattern is extensively used in the architecture of Spring framework.
  • Publish-subscribe: This pattern is the basis of messaging architecture. In this case messages are published to a Topic. And subscribers subscribe to the topic of their interests. Once the message is published to a topic in which a Subscriber has an interest, the message is consumed by the relevant subscriber.
  • Service Locator: This design pattern is used in a service like JNDI to locate the available services. It uses as central registry to maintain the list of services.
  • n-Tier: This is a generic design pattern to divide the architecture in multiple tiers. E.g. there is 3-tier architecture with Presentation layer, Application layer and Data access layer. It is also called multi-layer design pattern.
  • Data Access Object (DAO): This pattern is used in providing access to database objects. The underlying principle is that we can change the underlying database system, without changing the business logic. Since business logic talks to DAO object, there is no impact of changing Database system on business logic.
  • Inversion of Control (IoC): This is the core of Dependency Injection in Spring framework. We use this design pattern to increase the modularity of an application. We keep the objects loosely coupled with Dependency Injection.

What are the popular uses of Façade design pattern?

Some of the popular uses of Façade design pattern are as follows:

<li>A Façade provides convenient methods for common tasks that are used more often.</li>

<li>A Façade can make the software library more readable.</li>

<li>A Façade can reduce the external dependencies on the working of inner code.</li>

<li>A Façade can act as a single well-designed API by wrapping a collection of poorly designed APIs.</li>

<li>A Façade pattern can be used when a System is very complex and difficult to use. It can simplify the usage of complex system</li>

What is the difference between Builder design pattern and Factory design pattern?

Both Factory and Builder patterns are creational design patterns. They are similar in nature but Factory pattern is a simplified generic version of Builder pattern.

We use Factory pattern to create different concrete subtypes of an Object. The client of a Factory may not know the exact subtype. E.g. If we call createDrink() of a Factory, we may get Tea or Coffee drinks.

We can also use Builder pattern to create different concrete subtypes of an object. But in the Builder pattern the composition of the object can be more complex. E.g. If we call createDrink() for Builder, we can getCappuccino Coffee with Vanilla Cream and Sugar, or we can get Latte Coffee with Splenda and milk cream.

So a Builder can support creation of a large number of variants of an object. But a Factory can create a broader range of known subtypes of an object.

What is Memento design pattern?

Memento design pattern is used to implement rollback feature in an object. In a Memento pattern there are three objects:

  • Originator: This is the object that has an internal state.
  • Caretaker: This is the object that can change the state of Originator. But it wants to have control over rolling back the change.
  • Memento: This is the object that Caretaker gets from Originator, before making and change. If Caretaker wants to Rollback the change it gives Memento back to Originator. Originator can use Memento to restore its own state to the original state.
  • E.g. One good use of memento is in online Forms. If we want to show to user a form pre-populated with some data, we keep this copy in memento. Now user can update the form. But at any time when user wants to reset the form, we use memento to make the form in its original pre-populated state. If user wants to just save the form we save the form and update the memento. Now onwards any new changes to the form can be rolled back to the last saved Memento object.

What is an AntiPattern?

An AntiPattern is opposite of a Design Pattern. It is a common practice in an organization that is used to deal with a recurring problem but it has more bad consequences than good ones.

AntiPattern can be found in an Organization, Architecture or Software Engineering.

Some of the AntiPatterns in Software Engineering are:

  • Gold Plating: Keep on adding extra things on a working solution even though these extra things do not add any additional value.
  • Spaghetti Code: Program that are written in a very complex way and are hard to understand due to misuse of data structures.
  • Coding By Exception: Adding new code just to handle exception cases and corner case scenarios.
  • Copy Paste Programming: Just copying the same code multiple times rather than writing generic code that can be parameterized.

What is a Data Access Object (DAO) design pattern?

DAO design pattern is used in the data persistent layer of a Java application. It mainly uses OOPS principle of Encapsulation.

By using DAO pattern it makes the application loosely coupled and less dependent on actual database.

We can even implement some in-memory database like H2 with DAO to handle the unit-testing.

In short, DAO hides the underlying database implementation from the class that accesses the data via DAO object.

Recently we can combine DAO with Spring framework to inject any DB implementation.

Which is the design pattern used in Android applications?

Android applications predominantly use Model View Presenter design pattern.

<li><strong>Model</strong>: This is the domain model of the Android application. It contains the business logic and business rules. </li>

<li>

View: These are the UI components in your application. These are part of the view. Also any events on UI components are part of view module.

<li>

Presenter: This is the bridge between Model and View to control the communication. Presenter can query the model and return data to view to update it.

<li>

E.g. If we have a Model with large news article data, and view needs only headline, then presenter can query the data from model and only give headline to view. In this way view remains very light in this design pattern

.

How can we prevent users from creating more than one instance of singleton object by using clone() method?

First we should not implement the Cloneable interface by the object that is a Singleton.

Second, if we have to implement Cloneable interface then we can throw exception in clone() method.

This will ensure that no one can use clone() method or Cloneable interface to create more than one instance of Singleton object.

What is Builder design pattern?

Builder design pattern is a creational design pattern. We can use Builder pattern to create complex objects with multiple options.

E.g. when we have to create a Meal in a restaurant we can use Builder pattern. We can keep adding options like- Starter, Drink, Main Course, and Dessert etc. to create complete meal. When a user selects other options of Starter, Drink Main Course, Dessert another type of meal is created.

Main feature of Builder pattern is step-by-step building of a complex object with multiple options.