Tuesday, May 3, 2011

J2EE Design Patterns - Interview Questions

The following are some questions that you might encounter when you face an Interview for a position of a Senior Java/J2EE Developer. Design Patterns are very exciting and useful and most managers expect their developers to have a sound understanding of the most important patterns.

Also, as part of my series of chapters on the SCWCD Certification Series, there were a few articles posted on Design Patterns. You can use them to revise/understand these patterns better.

They are:

Introduction to Design Patterns
Elements of a Design Pattern
Value Object Pattern
DAO Pattern
Business Delegate Pattern
MVC Pattern
Front Controller Pattern


Questions:


What are design patterns?


A pattern is a proven (and recurring) solution to a problem in a context. Each pattern describes a problem which occurs time and again in our environment, and describes its solution to this problem in such a way that we can use this solution any number of times. In simple words, there are a lot of common problems which a lot of developers have faced over time. These common problems ideally should have a common solution too. It is this solution when documented and used over and over becomes a design pattern.

Can we always apply the same solution to different problems at hand?

No. Design patterns would study the different problems at hand. To these problems then it would suggest different design patterns to be used. However, the type of code to be written in that design pattern is solely the discretion of the Project Manager who is handling that project.

What should be the level of detail/abstraction which should be provided by a design pattern?

Design patterns should present a higher abstraction level though it might include details of the solution. However, these details are lower abstractions and are called strategies. There may be more than one way to apply these strategies in implementing the patterns. The details of implementation are usually upto the developer who is coding at the ground level.

What are the most common problems which one faces during the application design phase that are solved by design patterns?

Some are:

1. Identifying components, internal structures of the components, and relationships between components.
2. Determining component granularity and appropriate interactions
3. Defining component interfaces.

How does one decide which Design pattern to use in our application?

We need to follow these steps:

1. We need to understand the problem at hand. Break it down to finer grained problems. Each design pattern is meant to solve certain kinds of problems. This would narrow down our search for design patterns.
2. Read the problem statement again along with the solution which the design pattern will provide. This may instigate to change a few patterns that we are to use.
3. Now figure out the interrelations between different patterns. Also decide what all patterns will remain stable in the application and what all need to change (with respect to Change Requests received from the clients).


What is Refactoring?

Learning different design patterns is not sufficient to becoming a good designer. We have to understand these patterns and use them where they have more benefits. Using too many patterns (more than required) would be over-engineering and using less design patterns than required would be under-engineering. In both these scenarios we use refactoring. Refactoring is a change made to the internal structure of the software to make it easier to understand and cheaper to modify, without changing its observable behaviour.

What are Antipatterns?

Though the use of patterns fulfils our objectives in the applications; there are also several instances where several applications did not fulfill their goals. The architects of these applications too need to document these wrong decisions. This helps others by preventing them from repeating these mistakes in their applications. Such documented mistakes are called antipatterns.

As we do development in tiers, how do we divide patterns in tiers?

The Sun Java Center has classified the patterns in three tiers. These are:

Presentation tier patterns for web-component tier,
Business tier patterns for business logic (EJB) tier, and
Integration tier patterns for connection to the databases.

What are the Presentation Tier Patterns?

The presentation tier patterns are:

Intercepting filter, Front Controller, View Helper, Composite View, Service-to-Worker, and Dispatcher View.

What are the Business Tier Patterns?

The business tier patterns are:

Business delegate, Value Object, Session Façade, Composite Entity, Value Object Assembler, Value List Handler, and Service Locator.

What are the Integration Tier Patterns?

Integration tier patterns are:

Data Access Object (DAO) and Service Activator

What is Intercepting Filter pattern?

Provides a solution for pre-processing and post-processing a request. It allows us to declaratively apply filters for intercepting requests and responses. For ex. Servlet filters.

What is Front Controller pattern?

It manages and handles requests through a centralized code. This could either be through a servlet or a JSP (through a Java Bean). This Controller takes over the common processing which happens on the presentation tier. The front controller manages content retrieval, security, view management and retrieval.

What is View Helper pattern?

There generally are two parts to any application – the presentation and the business logics. The “View” is responsible for the output-view formatting whereas “Helper” component is responsible for the business logic. Helper components do content retrieval, validation and adaptation. Helper components generally use Business delegate pattern to access business classes.

What is Composite View pattern?

This pattern is used for creating aggregate presentations (views) from atomic sub-components. This architecture enables says piecing together of elementary view components which makes the presentation flexible by allowing personalization and customization.

What is Service to Worker pattern?

This is used in larger applications wherein one class is used to process the requests while the other is used to process the view part. This differentiation is done for maintainability.

What is Dispatcher View pattern?

This is similar to Service to Worker pattern except that it is used for smaller applications. In this one class is used for both request and view processing.

What is Business Delegate pattern?

This pattern is used to reduce the coupling between the presentation and business-logic tier. It provides a proxy to the façade from where one could call the business classes or DAO class. This pattern can be used with Service Locator pattern for improving performance.

What is Value Object pattern?

Value Object is a serializable object which would contain lot of atomic values. These are normal java classes which may have different constructors (to fill in the value of different data) and getter methods to get access to these data. VOs are used as a course grained call which gets lots of data in one go (this reduces remote overhead). The VO is made serializable for it to be transferred between different tiers within a single remote method invocation

What is Session Façade pattern?

This pattern hides the complexity of business components and centralizes the workflow. It provides course-grained interfaces to the clients which reduces the remote method overhead. This pattern fits well with declarative transactions and security management.

What is Value Object Assembler pattern?

This pattern allows for composing a Value Object from different sources which could be EJBs, DAOs or Java objects.

What is Value List Handler pattern?

This pattern provides a sound solution for query execution and results processing.

What is Service Locator pattern?

It provides a solution for looking-up, creating and locating services and encapsulating their complexity. It provides a single point of control and it also improves performance.

What is Data Access Object pattern?

It provides a flexible and transparent access to the data, abstracts the data sources and hides the complexity of Data persistence layer. This pattern provides for loose coupling between business and data persistence layer.

What is EJB Command pattern?

Session Façade and EJB Command patterns are competitor patterns. It wraps business logic in command beans, decouples the client and business logic tier, and reduces the number of remote method invocations.

What is Version Number pattern?

This pattern is used for transaction and persistence and provides a solution for maintaining consistency and protects against concurrency. Every time a data is fetched from the database, it comes out with a version number which is saved in the database. Once any update is requested on the same row of the database, this version is checked. If the version is same, the update is allowed else not.

What all patterns are used to improve performance and scalability of the application?

Value Object, Session Façade, Business Delegate and Service Locator.

What design patterns could be used to manage security?

Single Access Point, Check point and Role patterns



If you have any more questions on Design Patterns that you have faced during your interviews and wish to add them to this collection - pls drop a note to anandvijayakumar007@gmail.com and I shall be glad to add them to this list.

10 comments:

  1. Thanks for sharing this valuable interview questions.

    Interview Questions

    ReplyDelete
    Replies
    1. Indeed valuable question, specially for experience Java programmer. By the way I have also put together list of 20 design pattern interview questions from my experience, you guys may found it useful.

      Delete
  2. hi, i liked it,but where is the composite entity and service activator???

    ReplyDelete
  3. hi,i liked it...
    but where is the composite entity and service activator?

    ReplyDelete
  4. hi,i liked it...
    but where is the composite entity and service activator?

    ReplyDelete
  5. Excellent post mate, design pattern is indeed favorite topic of interviewees and it get more tough when they asked to solve or apply design pattern in a particular problem.your questions are good for quick review before appearing.by the way I have also blogged some questions as Top 20 Core Java Interview questions and answers let me know how do you find it.

    Thanks
    Javin

    ReplyDelete
  6. Nice collection Anand and Thanks for your comment on my post Decorator design pattern in Java with Example . I see you also got a nice questions.

    ReplyDelete
  7. There is one thing I would like to point out...

    The type of code to be written should be at the hand of the discretion of the Architect or Lead Developer, the Project Manager should never be involved in making design descion.

    ReplyDelete
    Replies
    1. Amit - I agree on it partially but, if the PM is a techie guy, his inputs can be useful in design decisions

      For ex: I have over 8 years of Java dev exp now and if I become a PM next year and my team is taking a design decision, my inputs could be valuable. Isnt it?

      If the guy giving the advise is a pure manager with no technical background, then whatever you said is 100% true.

      Anand

      Delete
  8. hi Anand,
    I would like to add that related to design patterns is a term called anti patterns. I had covered the same in my blog post on introduction to design patterns

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers