Wednesday, June 5, 2013

Basic Design Patterns Interview Questions

1. What is Design Patterns?
Ans.
 Design patterns are time tested reusable design solutions to recurring problems in application development.
2. What is Gang of Four in design patterns?
Ans.
 The Gang of Four are the four authors of the book, “Design Patterns: Elements of Reusable Object-Oriented Software”. The four authors were Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. The Gand of Four(GoF) terminology refers to the book or either author in design pattern. GoF patterns are generally considered foundation for all other patterns.
3. How many total patterns are there in GoF?
Ans.
 There are total 23 patterns in GoF.
4. What are the categories design patterns divided into?
Ans.
 There are total three categories into which design patterns divided.
Creational Pattern:Creational patterns provide ways to instantiate single objects or group of related objects.
Structural Patterns:Structural patterns provide a way to define relationship between classes and objects.
Behavioral Patterns:Behavioral patterns define ways of communication between classes and objects.
5. Can you give name of patterns fall in each category?
Ans. Five
 patterns fall under creational pattern.
Abstract Factory
Factory
Factory Method
Prototype
Singleton
Seven patterns fall under structural pattern.
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
Eleven patterns fall under behavioral pattern.
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor
6. What are object patterns?
Ans.
 Object patterns deals with object relationships that can be changed at runtime. It specify the relationships between objects. The purpose of object pattern is to allow the instances of different classes to be used in the same place in a pattern.
Object patterns avoid fixing the class that accomplishes a given task at compile time.
Instead the actual class of the object can be chosen at runtime. Object patterns mostly use object composition to establish relationships between objects.
7. What are class patterns?
Ans.
 Class patterns deals with class relationships that can be changed at compile time. It specify relationship between classes and their subclasses.
Thus, class patterns tend to use inheritance to establish relationships.
Unlike object patterns and object relationships, class patterns generally fix the relationship at compile time.
They are less flexible and dynamic and less suited to polymorphic approaches.
8. What is class scope in class patterns?
Ans.
 Class scope in class patterns is defined at design(compile) time. Class scope is built in the structure and relationship of classes.
9. What is object scope in object patterns?
Ans.
 Object scope in object patterns is defined at runtime. Object scope is based on the relationship of objects(mostly object composition).
10. Can you list down different class patterns and object patterns?
Ans.
Class Patterns

Adapter
Factory Method
Interpreter
Template Method
Object Patterns
Abstract Factory
Builder
Bridge
Chain of Responsibility
Command
Composition
Decorator
Façade
Flyweight
Iterator
Mediator
Memento
Observer
Prototype
Proxy
Singleton
State
Strategy
Visitor
Following is reference table for distribution of design patterns into Class or Object Patterns and categories of patterns(Creational, Structural and Behavioral).
Purpose
CreationalStructuralBehavioral
ScopeClassFactory MethodAdapterInterpreter
Template Method
ObjectAbstract Factory
Builder
Prototype
Singleton
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
Chain of Responsibility
Command
Iterator
Mediator
Memento
Flyweight
Observer
State
Strategy
Visitor
11. Can you describe all five creational patterns in one line? (Interviewer will not ask for all five, only one or two)
Ans.

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.
12. Can you describe all seven structural patterns in one line? (Interviewer will not ask for all seven, only one or two)
Ans.

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.
13. Can you describe all eleven behavioral patterns in one line? (Interviewer will not ask for all eleven, only one or two)
Ans.

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.

No comments:

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps