Saturday, December 3, 2011

design patterns interview 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 over and over again in our environment, and describes its solution to this problem in such a way that we can use this solution a lots of times



  • What is 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.



  • How do we divide patterns in tiers?


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


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



  • The presentation tier patterns are:


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


  • The business tier patterns are:


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


  • Integration tier patterns are:


Data Access Object (DAO) and Service Activator.


Reference:
http://www.ms.oyangudi.com/blog/design-patterns/design-patterns-interview-questions-2/




  • What is a software design pattern?


A design pattern is a solution to a general software problem within a particular context.

Context : A recurring set of situations where the pattern applies.
Problem : A system of forces (goals and constraints) that occur repeatedly in this context.
Solution : A description of communicating objects and classes (collaboration) that can be applied to resolve those forces.


Reference:
http://www.javabeat.net/articles/106-design-patterns-interview-questions-1.html

http://userpages.umbc.edu/~tarr/dp/spr04/cs446.html
http://www.allapplabs.com/j2ee_design_patterns/j2ee_design_patterns_business_delegate.htm
http://www.corej2eepatterns.com/Patterns2ndEd/TransferObject.htm
http://www.precisejava.com/javaperf/j2ee/Patterns.htm#Patterns105




  • Give an example where you prefer abstract class over interface ?

1. In Java you can only extend one class but implement multiple interface. So if you extend a class you lost your chance of extending another class.
2. Interface are used to represent adjective or behavior e.g. Runnable, Clonable, Serializable etc, so if you use an abstract class to represent behavior your class can not be Runnable and Clonable at same time because you can not extend two class in Java but if you use interface your class can have multiple behavior at same time.
3. On time critical application prefer abstract class is slightly faster than interface


You are writing classes to provide Market Data and you know that you can switch to different vendors overtime like Reuters, wombat and may be even to direct exchange feed , how do you design your Market Data system.

a MarketData interface which will have methods required by client e.g. getBid(), getPrice(), getLevel() etc and MarketData should be composed with a MarketDataProvider by using dependency injection. So when you change your MarketData provider Client won't get affected because they access method form MarketData interface or class




What is main benefit of using factory pattern ? Where do you use it?
Factory pattern’s main benefit is increased level of encapsulation while creating objects. If you use Factory to create object you can later replace original implementation of Products or classes with more advanced and high performance implementation without any change on client layer.





Can you name few design patterns used in standard JDK library?
Decorator design pattern which is used in various Java IO classes,
Singleton pattern which is used in Runtime , Calendar and various other classes,
Factory pattern which is used along with various Immutable classes likes Boolean e.g. Boolean.valueOf and
Observer pattern which is used in Swing and many event listener frameworks.

http://javarevisited.blogspot.com/2012/06/20-design-pattern-and-software-design.html#ixzz2B5eNpnQN



  • What is the difference between the bridge pattern and the strategy pattern?


The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

The Bridge pattern is a structural pattern (HOW DO YOU BUILD A SOFTWARE COMPONENT?).
The Strategy pattern is a dynamic pattern (HOW DO YOU WANT TO RUN A BEHAVIOUR IN SOFTWARE?).

Strategy: you have more ways for doing an operation; with strategy you can choice the algorithm at run-time and you can modify a single Strategy without a lot of side-effects at compile-time;
Bridge decouples the abstraction from implementation so that the two can vary independently.

http://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern



What is the basic difference between Factory and Abstract Factory Patterns?
With the Factory pattern, you produce implementations (Apple, Banana, Cherry, etc.) of a particular interface -- say, IFruit.

With the Abstract Factory pattern, you produce implementations of a particular Factory interface -- e.g., IFruitFactory. Each of those knows how to create different kinds of fruit.


When to Use the Abstract Factory Pattern Use the Abstract Factory pattern when clients must be decoupled from product classes. Especially useful for program configuration and modification The Abstract Factory pattern can also enforce constraints about which classes must be used with others. It may be a lot of work to make new concrete factories.

Abstract Factory Example1: This specification for the disks to prepare different types of pasta in a pasta maker is the Abstract Factory, and each specific disk is a Factory. all Factories (pasta maker disks) inherit their properties from the abstract Factory. Each individual disk contains the information of how to create the pasta, and the pasta maker does not.

Abstract Factory Example2: The Stamping Equipment corresponds to the Abstract Factory, as it is an interface for operations that create abstract product objects. The dies correspond to the Concrete Factory, as they create a concrete product. Each part category (Hood, Door, etc.) corresponds to the abstract product. Specific parts (i.e., driver side door for 99 camry) corresponds to the concrete products


The Abstract Factory Pattern

    Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
    The Abstract Factory pattern is very similar to the Factory Method pattern. One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.
    Actually, the delegated object frequently uses factory methods to perform the instantiation!

Factory pattern

    Factory patterns are examples of creational patterns

    Creational patterns abstract the object instantiation process. They hide how objects are created and help make the overall system independent of how its objects are created and composed.

    Class creational patterns focus on the use of inheritance to decide the object to be instantiated Factory Method

    Object creational patterns focus on the delegation of the instantiation to another object Abstract Factory

 http://stackoverflow.com/questions/1001767/what-is-the-basic-difference-between-factory-and-abstract-factory-patterns


  • all creational patterns in one line?
Abstract Factory pattern is used to provide a client with set of “family” of objects created by the factory which is determined at runtime.
Builder pattern is used to create complex objects with internal parts that must be created in the same order or using a specific algorithm.
Factory Method pattern is used to replace class constructors, abstracting the process of object generation in a way that the type of object instantiated can be determined at runtime.
Prototype pattern is used to instantiate new object by copying all of the properties of an existing object, creating an independent clone.
Singleton pattern ensures single object of a particular class.


all structural patterns in one line?
Adapter pattern is used to provide mechanism to link two incompatible types by wrapping the “adaptee” with a class that supports the interface required by the client.
Bridge pattern is used to separate the abstract element of a class from the implementation details, providing the means to replace the implementation details without modifying the abstraction.
Composite pattern is used to create hierarchical, recursive tree structures of related objects where any element of the structure may be accessed and utilized in a standard manner.
Decorator pattern is used to extend or alter the functionality of objects at run-time by wrapping them in an object of a decorator class.
Façade pattern is used to define a simplified interface to a more complex subsystem.
Flyweight pattern is used to reduce the memory and resource usage for complex models containing many hundreds, thousands or hundreds of thousands of similar objects.
Proxy pattern is used to provide a placeholder object, which references an underlying object.


all behavioral patterns in one line?
Chain of Responsibility pattern is used to process varied requests, each of which may be dealt with by a different handler.
Command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object.
Interpreter pattern is used to define the grammar for instructions that form part of a language or notation, whilst allowing the grammar to be easily extended.
Iterator pattern is used to provide a standard interface for traversing a collection of items in an aggregate object without the need to understand its underlying structure.
Mediator pattern is used to reduce coupling between classes that communicate with each other by sending messages via a mediator object.
Memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.
Observer pattern is used to allow an object to publish changes to its state. Other objects subscribe to be immediately notified of any changes.
State pattern is used to alter the behavior of an object at runtime as its internal state changes.
Strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.
Template Method pattern is used to define the basic steps of an algorithm and allow the implementation of the individual steps to be changed.
Visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.

http://jinaldesai.net/basic-design-patterns-interview-questions/

No comments:

Post a Comment