Design Pattern Interview Questions and Answers

Design pattern is a guideline to find a solution for commonly occurring problems. Now a day’s design pattern and software design questions are essential part of any programming interview, especially for mid to senior level programming jobs. Day by day it is becoming more popular. This article describes some important questions and answers in asp.net design pattern. Hope it will help you to build successful carrier.

What is a design pattern?
In software engineering, design pattern or simply pattern is a universal reusable solution for a commonly occurring problem in software design. It is not a finished task that can be converted into codes directly. It is a template or description for how to solve a problem that can be used in different circumstances/situations. In Object Oriented Programming (OOP) it describes the relationship and interaction between classes or objects.

Why should we use design patterns?
Design Patterns provides us lot of advantages. That’s why we should use them. Some common advantages are given bellow:

  • It provides solution to common problems which occur in software design
  • It provides common platform for developers means developer can implement these in any language
  • It provides a standard terminology and is specific to particular scenario

What about anti-patterns?
The selection of wrong design pattern can perform a negative effect on codes. That’s why it is required to choose appropriate design pattern. The selections of wrong desing pattern is defined as “anti-pattern”.

Are design patterns the same thing as frameworks?
No, both are different.  Design pattern provides only the guideline, not codes. But Framework deals with codes.

What are the types of design patterns?
Design Patterns can be organized in four pattern groups depending on the nature of the design problem they intend to solve

  1. Gang of Four (GoF) Patterns
  2. Enterprise Patterns
  3. SOA and Messaging Patterns
  4. Model-View Patterns

What is gang of four (gof) design patterns?
In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book named Elements of Reusable Object-Oriented Software that explains the concept of design pattern in software development. These four authors are known as Gang of Four (GOF).

Which pattern is the foundation of design pattern?
Gang of Four (GoF) design patterns is the foundation for all other design patterns.

How many patterns are in GoF design pattern?
23

What are the main categories of GoF pattern?
GoF design pattern can be categorized into 3 groups.

  1. Creational Patterns
    Creational Patterns deals mainly with creation of Classes & Objects.
  2. Structural Patterns
    Structural Patterns deals with Class & Object Composition.
  3. Behavioral Patterns
    Behavioral Patterns deals with Class & Object communication.

Give the list of GoF design pattern
The list is given bellow:

  • Creational Design Patterns
    • Factory Method
    • Abstract Factory
    • Builder
    • Prototype
    • Singleton
  • Structural Design Patterns
    • Adapter
    • Bridge
    • Composite
    • Decorator
    • Façade
    • Flyweight
    • Proxy
  • Behavioral Design Patterns
    • Chain of Responsibility
    • Command
    • Interpreter
    • Iterator
    • Mediator
    • Memento
    • Observer
    • State
    • Strategy
    • Visitor
    • Template Method

What is Factory method pattern?
The factory method pattern creates objects at run-time based on demand. This pattern is the most used design pattern in modern programming languages like Java and C#. For example consider an ice-cream factory which produces different types of ice-creams. If we went to the ice-cram factory and order for chocolate ice-cream, factory method will produce chocolate ice-cream and will provide us.

When to use Factory method pattern?
The factory method pattern is used when:
-The creation of object is done when it is required
-Flexibility is important
-Subclasses decide what objects should be created

What is Abstract factory pattern?
The abstract Factory patterns acts as like super-factory which creates other factories. That’s why this pattern is also called Factory of factories. This pattern is used to create a set of related objects or dependent objects. For creating objects this pattern internally use Factory method design pattern. Also this pattern may use Builder design pattern or prototype design pattern. But this completely depends on choice.

When to use abstract factory pattern?
The abstract factory pattern is used when:
-Need to create a set of related objects or dependent objects.

What is the difference between factory and abstract factory patterns?
The factory method is a single method and an abstract factory is an object.
Factory method produces one product. Abstract factory produce families of products.
For example consider Mr. XX needs a door for his house. He approaches a carpenter. He gives the measurement of the door. After some days the carpenter provides the door. In this case, the carpenter is a factory of doors. Here specifications are the inputs for the factory and the door is the output or product of the factory.
Consider the same situation. Mr. XX goes to a carpenter or door shops. All of them are door factories. Based on the situation he can approach what kind of factory he needs. This is like abstract factory.

What is prototype pattern?
The prototype pattern creates a new object from the existing instance of the object. This pattern is used to create a duplicate object or clone of the current object to enhance performance.

When to use prototype pattern?
The prototype pattern is used when the creation of object is costly or complex, a limited number of state combinations exist in an object.

What is Singleton Pattern?
The one of the simplest design patterns is Singleton pattern. This pattern ensures that a class has only one instance in the whole application and provides a global point of access to it.

When to use singleton pattern?
In a project when we want only one instance of a particular class will be created and all the modules of the entire project will use this instance. For example consider the class country which will loads the entire country list. We would like to share a single instance of this class by not hitting the database again and again. This will increase the performance of the application.

How to implement singleton pattern in .NET?
We can implement the singleton pattern in .net by following the three steps:
Create a class with static members
Define a private constructor to the class.
Provide a static method to get access to the singleton object.
For example consider the following C# codes:

public class mySingleton
{
    private static mySingleton instance;

    private mySingleton() { }

    public static mySingleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new mySingleton();
            }
            return instance;
        }
    }
}

What is MVC pattern?
The Model View Controller (MVC) is a design pattern used in software engineering. This patten separates the structure and behavior of the applications into three component/parts: model, view and controller.

  1. Model
    Model is the data access layer. This can be direct data access, web services, etc
  2. View
    View is the presentation layer of the application. This the users interface.
  3. Controller
    Controller is the business logic layer for the application. It controls Models & View.

Which pattern is widely used in programming?
The MVC pattern is widely used in software development with many programming language like: java, C#, C, C++, PHP etc.

Leave a Reply

Your email address will not be published. Required fields are marked *