Tech Point Fundamentals

Friday, April 15, 2022

Design Patterns Interview Questions - Part 07

Design Patterns Interview Questions and Answers - Part 07


In Software Industry the Design Patterns and Principles are always followed. So this is the most common topic for the interview. In this article, we will see the most frequently asked 90+ Design Patterns and Principles Interview Questions with Answers

Please visit our YouTube Channel for Interviews and other videos by below link:

Please read the complete C# Interview Questions and Answers article series here.


This is the 7th part of this Design Patterns and Principles Interview Questions and Answers article series. Each part contains five Design Pattern Interview Questions. Please read all the Design Patterns and Principles Interview Questions list here.

I will highly recommend to please read the previous parts over here before continuing the current part:

Design Patterns Interview Questions - Part 07

Q49. What is GoF Pattern? or What is a Gang of Four?

GoF stands for Gang of Four. GoF is nothing but a group of four writers of a famous book that has defined different software design patterns.

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides published a book titled "Design Patterns - Elements of Reusable Object-Oriented Software" which initiated the concept of Design Patterns in Software development. These authors are collectively known as the Gang of Four (GoF). 

The GoF wrote the book in a C++ context but it still remains very relevant to Java or C# programming. The design patterns can be applied in any object-oriented language. 

There are 23 GoF Design Patterns which are categorized into three categories: 

  1. Creational Design Patterns: for the creation of objects
  2. Structural Design Patterns: that provide relationships between objects.
  3. Behavioral Design Patterns: to help define how objects interact.

Q50. What are the different types of design patterns available in the software industry?

Design patterns are the design-level solutions for recurring problems that we software engineers come across often. There are about 23 patterns currently which are divided into three categories:

1. Creational Design Pattern: 

These patterns are designed for class instantiation. They can be either class-creation patterns or object-creational patterns. Following are the different creational patterns:

  1. Singleton Pattern
  2. Factory Pattern
  3. Abstract Factory Pattern
  4. Builder Pattern
  5. Prototype Pattern
  6. Fluent Interface Pattern

2. Structural Pattern: 

These patterns are designed with regard to a class's structure and composition. The main goal of most of these patterns is to increase the functionality of the class(es) involved, without changing much of its composition.  Following are the different structural patterns:

  1. Adapter Pattern
  2. Bridge Pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Facade Pattern
  6. Proxy Pattern
  7. Flyweight Pattern

3. Behavioral Pattern: 

These patterns are designed depending on how one class communicates with others. Following are the different behavioral patterns:

  1. Chain of Responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Observer Pattern
  7. Strategy Pattern
  8. Template Method Pattern
  9. State Pattern
  10. Memento Pattern

Q51. What are the Creational Design Patterns (CDP)? What are the different Creational Design Patterns?

Creational Design Patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using a new operator. This gives the program more flexibility in deciding which objects need to be created for a given use case.

Creational patterns make the creation process more adaptable and dynamic. In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. They reduce complexities and instability by creating objects in a controlled manner.

Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented. These design patterns are used when a decision must be made at the time of instantiation of a class.

Creational Patterns can be classified into two subcategories again:

  1. Class-Creational Patterns: use inheritance effectively in the instantiation process
  2. Object-Creational Patterns: use delegation effectively to get the job done

Types of Creational Pattern:

  1. Singleton Pattern: Singleton Pattern ensures only one instance of an object is created throughout the application.
  2. Factory Pattern: It creates objects without specifying the exact class to be created.
  3. Abstract Factory Pattern: It allows the creation of objects without specifying their concrete type i.e creates families of related dependent objects.
  4. Builder Pattern: It is used to create complex objects. It constructs complex objects using a step-by-step approach.
  5. Prototype Pattern: It creates a new object from an existing object.
  6. Fluent Interface Pattern: It is  implemented by using method chaining to implement method cascading

Q52. What is Factory Design Pattern (FDP)? When to use the Factory Pattern?

The Factory Design Pattern or Factory Method Design Pattern is one of the most used design patterns. According to GoF, this pattern “defines an interface for creating an object, but lets subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses”.

Factory Pattern delegates the responsibility of initializing a class from the client to a particular factory class by creating a type of virtual constructor.  A superclass specifies all standard and generic behavior and then delegates the creation details to subclasses that are supplied by the client.

The Factory Method makes the design more customizable. To achieve this, we rely on a factory that provides us with the objects, hiding the actual implementation details. The created objects are accessed using a common interface.

Factory Methods are usually called within Template Methods. Factory Method is a creation through inheritance. The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.

The factory method should be used when the implementation of an interface or an abstract class is expected to change more frequently. The factory method can also be considered when the initialization process is relatively simple, and the constructor only requires a handful of parameters

Q53. Why do we use a factory class to instantiate a class when we can use a new operator directly?

Of course, we can use the "new" operator to create the instance of a class, but this causes tight coupling and it is something hard coded. The new operator cannot be used in a scenario when we want to create an object at run time based on the requirement.

Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

The Factory Method pattern suggests that you replace direct object construction calls (using the new operator) with calls to a special factory method. 

However the objects are still created via the new operator, but it’s being called from within the factory method. Objects returned by a factory method are often referred to as products.

When to use Factory Pattern:

The Factory Method pattern is useful when you need to abstract the creation of an object away from its actual implementation. 

  1. Use the Factory Method when you don’t know beforehand the exact types and dependencies of the objects your code should work with. So use a factory when a class cannot anticipate the type of objects it needs to create beforehand.
  2. Use the Factory Method when you want to provide users of your library or framework with a way to extend its internal components. So use factory when a class requires its subclasses to specify the objects it creates.
  3. Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time. So use factory when you want to localize the logic to instantiate a complex object.

Advantage of Factory Pattern:

  1. Factory Pattern avoids tight coupling between the creator and the concrete products.
  2. It supports the Single Responsibility Principle. You can move the product creation code into one place in the program, making the code easier to support.
  3. It follows the Open/Closed Principle. You can introduce new types of products into the program without breaking the existing client code.

Q54. What is Abstract Factory Design Pattern (AFDP)? When to use the Abstract Factory Creational Design Pattern?

Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes. The first thing the Abstract Factory pattern suggests is to explicitly declare interfaces for each distinct product (type) of the product family. Then you can make all variants of products follow those interfaces.

The Abstract Factory defines a Factory Method per product. Each Factory Method encapsulates the new operator and the concrete, platform-specific, product classes. Each "platform" is then modeled with a Factory derived class.  Abstract Factory returns the product immediately.

The Abstract Factory interface declares a set of creation methods that the client code can use to produce different types of products. The abstract factory interface declares a set of methods that return different abstract products. These products are called a family and are related by a high-level theme or concept.

Use the Abstract Factory when your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products—they might be unknown beforehand or you simply want to allow for future extensibility.

Q55. What is the difference between Factory Pattern and Abstract Factory Design Pattern?

The methods of an Abstract Factory are implemented as Factory Methods. Both the Abstract Factory Pattern and the Factory Method Pattern decouple the client system from the actual implementation classes through the abstract types and factories.

Factory Method design pattern could be used to create objects related to a single-family while Abstract Factory specializes in creating families of related objects. So the abstract Factory Design Pattern is used to create families of related or dependent objects

The Factory Method creates objects through inheritance whereas the Abstract Factory creates objects through composition.

Q56. What is Builder Design Pattern (BDP)? When to use the Builder Creational Design Pattern?

Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

The Builder Design Pattern is another creational pattern designed to deal with the construction of comparatively complex objects.

When the complexity of creating object increases, the Builder pattern can separate out the instantiation process by using another object (a builder) to construct the object. This builder can then be used to create many other similar representations using a simple step-by-step approach.

The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate objects called builders. The pattern organizes object construction into a set of steps. 

To create an object, you execute a series of these steps on a builder object. The important part is that you don’t need to call all of the steps. You can call only those steps that are necessary for producing a particular configuration of an object.

Using the Builder pattern makes sense only when your products are quite complex and require extensive configuration. Use the Builder pattern to get rid of a “telescopic constructor”.

To Be Continued Part-08...

Recommended Articles

Thanks for visiting this page. Please follow and join us on LinkedInFacebookTelegramQuoraYouTubeTwitterPinterestTumbler, and VK for regular updates.


External Reference:

  1. Wikipedia
  2. RefacturingGuru
  3. SourceMaking

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.