Transcript of "Basic design pattern interview questions"
Articles from Jinal Desai .NETBasic Design Patterns Interview Questions2013-06-01 05:06:51 Jinal Desai1. What is Design Patterns?Ans. Design patterns are time tested reusable design solutions to recurringproblems 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: Elementsof 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 aregenerally 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 orgroup of related objects.Structural Patterns:Structural patterns provide a way to define relationshipbetween classes and objects.Behavioral Patterns:Behavioral patterns define ways of communication betweenclasses and objects.5. Can you give name of patterns fall in each category?Ans. Five patterns fall under creational pattern.Abstract FactoryFactoryFactory MethodPrototypeSingletonSeven patterns fall under structural pattern.AdapterBridgeCompositeDecoratorFaçadeFlyweightProxyEleven patterns fall under behavioral pattern.Chain of ResponsibilityCommandInterpreter
IteratorMediatorMementoObserverStateStrategyTemplate MethodVisitor6. 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 allowthe 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 compiletime.Instead the actual class of the object can be chosen at runtime. Object patternsmostly 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 compiletime. 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 therelationship 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 scopeis 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 basedon the relationship of objects(mostly object composition).10. Can you list down different class patterns and object patterns?Ans.Class PatternsAdapterFactory MethodInterpreterTemplate MethodObject PatternsAbstract FactoryBuilderBridgeChain of ResponsibilityCommandCompositionDecorator
FaçadeFlyweightIteratorMediatorMementoObserverPrototypeProxySingletonStateStrategyVisitorFollowing is reference table for distribution of design patterns into Class or ObjectPatterns and categories of patterns(Creational, Structural and Behavioral).PurposeCreational Structural BehavioralScopeClass Factory Method AdapterInterpreterTemplate MethodObjectAbstract FactoryBuilderPrototypeSingletonBridgeCompositeDecoratorFacadeFlyweightProxyChain of ResponsibilityCommandIteratorMediatorMementoFlyweightObserverStateStrategyVisitor11. Can you describe all five creational patterns in one line? (Interviewer willnot ask for all five, only one or two)Ans.Abstract Factory pattern is used to provide a client with set of “family” of objectscreated by the factory which is determined at runtime.Builder pattern is used to create complex objects with internal parts that must becreated in the same order or using a specific algorithm.Factory Method pattern is used to replace class constructors, abstracting theprocess of object generation in a way that the type of object instantiated can bedetermined at runtime.Prototype pattern is used to instantiate new object by copying all of the propertiesof 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? (Interviewerwill not ask for all seven, only one or two)
Ans.Adapter pattern is used to provide mechanism to link two incompatible types bywrapping the “adaptee” with a class that supports the interface required by theclient.Bridge pattern is used to separate the abstract element of a class from theimplementation details, providing the means to replace the implementation detailswithout modifying the abstraction.Composite pattern is used to create hierarchical, recursive tree structures ofrelated objects where any element of the structure may be accessed and utilized ina standard manner.Decorator pattern is used to extend or alter the functionality of objects at run-timeby wrapping them in an object of a decorator class.Façade pattern is used to define a simplified interface to a more complexsubsystem.Flyweight pattern is used to reduce the memory and resource usage for complexmodels containing many hundreds, thousands or hundreds of thousands of similarobjects.Proxy pattern is used to provide a placeholder object, which references anunderlying object.13. Can you describe all eleven behavioral patterns in one line? (Interviewerwill not ask for all eleven, only one or two)Ans.Chain of Responsibility pattern is used to process varied requests, each of whichmay be dealt with by a different handler.Command pattern is used to express a request, including the call to be made andall of its required parameters, in a command object.Interpreter pattern is used to define the grammar for instructions that form part of alanguage or notation, whilst allowing the grammar to be easily extended.Iterator pattern is used to provide a standard interface for traversing a collection ofitems in an aggregate object without the need to understand its underlying structure.Mediator pattern is used to reduce coupling between classes that communicatewith each other by sending messages via a mediator object.Memento pattern is used to capture the current state of an object and store it insuch a manner that it can be restored at a later time without breaking the rules ofencapsulation.Observer pattern is used to allow an object to publish changes to its state. Otherobjects subscribe to be immediately notified of any changes.State pattern is used to alter the behavior of an object at runtime as its internal statechanges.Strategy pattern is used to create an interchangeable family of algorithms fromwhich the required process is chosen at run-time.Template Method pattern is used to define the basic steps of an algorithm andallow the implementation of the individual steps to be changed.Visitor pattern is used to separate a relatively complex set of structured dataclasses from the functionality that may be performed upon the data that they hold.