Showing posts with label Design Pattern Interview Questions & Answers. Show all posts
Showing posts with label Design Pattern Interview Questions & Answers. Show all posts

July 03, 2017

J2EE Patterns

J2EE Design Patterns
  • MVC Pattern
  • Business Delegate Pattern
  • Composite Entity Pattern
  • Data Access Object Pattern
  • Front Controller Pattern
  • Intercepting Filter Pattern
  • Service Locator Pattern
  • Transfer Object Pattern
MVC Pattern
MVC is abbreviation of Model-View-Controller. Models are basically POJO's, which are used as blueprints/models for all of the objects that will be used in the application. Views represent the presentational aspect of the data and information located in the models. And the Controllers controls both of these.

Controller serve as a connection between the Model and View. It instantiate, update and delete models, populate them with information, and then send the data to the views to present to the end-user.

Front Controller Pattern
Upon sending a request, the Front Controller is the first controller it reaches. Based on the request, it decides which controller is the most adequate to handle it, after which it passes the request to the chosen controller. The Front Controller is most often used in Web Applications in the form of a Dispatcher Servlet.

Business Delegate Pattern
This pattern is used to decouple the presentation layer from the business layer. It is basically use to reduce communication or remote lookup functionality to business tier code in presentation tier code. In business tier we have following entities.

  • Business Service: Business Service interface. Concrete classes implement this business service to provide actual business implementation logic.
  • LookUp Service: Lookup service object is responsible to get relative business implementation and provide business object access to business delegate object.
  • Business Delegate: A single entry point class for client entities to provide access to Business Service methods.
  • Client: Presentation tier code may be JSP, servlet or UI java code.



Composite Entity Pattern
A Composite entity is an EJB entity bean which represents a graph of objects. When a composite entity is updated, internally dependent objects beans get updated automatically as being managed by EJB entity bean. Following are the participants in Composite Entity Bean.
  • Dependent Object: Dependent object is an object which depends on coarse grained object for its persistence lifecycle.
  • Coarse-Grained Object: This object contains dependent objects. It has its own life cycle and also manages life cycle of dependent objects.
  • Composite Entity: It is primary entity bean. It can be coarse grained or can contain a coarse grained object to be used for persistence purpose.
  • Strategies: Strategies represents how to implement a Composite Entity.



Service Locator Pattern
A pattern often seen in Web Applications, the Service Locator pattern is used to decouple the Service Consumers and the concrete classes like DAO implementations.

The pattern looks for the adequate service, saves it in cache storage to reduce the number of requests and therefore the strain on the server and provides the application with their instances.

-K Himaanshu Shuklaa..

July 01, 2016

Serialization of Singleton class..

Serialization is a process by which you can save or transfer the state of an object by converting it to a byte stream. During deserialization, the object is re-created.

June 19, 2016

Object Oriented Design Principles in Java

The Object-Oriented Design Principles are the core of Object Oriented Programming, with their help we can create a clean and modular design, which would be easy to test, debug, and maintain.

We can say the design principles are generalized pieces of advice or proven good coding practices that can be used as rules of thumb while making design choices.

April 26, 2016

Part 3: Java Design Pattern Interview Questions & Answers (Factory and Abstract Factory)

In Part 1 of Java Design Pattern Interview Questions & Answers, we discussed the basics. In Part 2, we implemented the Singleton design pattern. In Part 3, we will understand Factory and Abstract Factory design patterns. 

What is a static factory or factory method Design Pattern?

It is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. In the Factory pattern, we create an object without exposing the creation logic to the client and refer to a newly created object using a common interface.

Part 2: Java Design Pattern Interview Questions & Answers (All About Singleton Design Pattern)

In Part 1 of Java Design Pattern Interview Questions & Answers, we discussed the basics. Now in Part 2, we will discuss the Singleton design pattern in detail. 

What is the Singleton pattern?

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

This pattern involves a single class that is responsible to create an object while making sure that only a single object gets created. This class provides a way to access its only object which can be accessed directly without the need to instantiate the object of the class.

There are many classes in JDK which is implemented using Singleton pattern like : Java.lang.Runtime with getRuntime() method, Java.awt.Toolkit with getDefaultToolkit() and Java.awt.Desktop with getDesktop() etc.

Part 2: Scenario Based Java Design Pattern Interview Questions & Answers

Scenario 1

  • A company named XYZ Retail is in the business of selling Books, CD's and Cosmetics. 
  • Books are sales tax exempt and CDs and Cosmetics have a sales tax of 10%. 
  • CD's can be imported and attracts an import tax of 5%. 
  • Write a simple shopping basket program, which will calculate extended price (qty * (unitprice + tax)) inclusive of tax for each item in the basket?

Java Solution
We will first create the TaxCalculator interface and its implementation TaxCalculatorImpl.



Goods class is abstract as it implements the common behavior of Book, CDs, and Cosmetics. Also it composes (i.e composition) 'TaxCalculator' to perform the tax calculation based on if the good is imported and if sales tax is applicable.

Now, we will create Book, CDs, and Cosmetics classes that extends the shared behavior (inherits the shared behavior of Goods). The isTaxable & isImported are specific to the product. We can add new products in the future with specific taxable & importable behaviors.



-K Himaanshu Shuklaa

Part 1: Java Design Pattern Interview Questions & Answers

What are Design Patterns?
Design patterns are tried and tested ways to solve particular design issues by various programmers in the world. We can say design patterns are an extension of code reuse.

Design patterns versus frameworks
Are design patterns the same thing as frameworks? The answer to that is NO. Design patterns are more like general guidelines on how to solve specific programming problems, but they do not specify the detailed code that’s necessary to solve those problems.

What are the benefits of using design patterns?
  • Reusable in multiple projects.
  • Design patterns make our code easy to understand and debug.
  • They provide an industry standard approach to solve a recurring problem.
  • Design patterns also provide transparency to the design of an application.