Sunteți pe pagina 1din 18

9 Unusual Design Patterns Interview

Questions (with Answers)


Alex 👨🏼💻FullStack.Cafe Jul 17 '18 Updated on Sep 05, 2018 ・5 min read
#designpatterns #fullstack #interview #questions

In software engineering, a design pattern is a general


repeatable solution to a commonly occurring problem in
software design. A design pattern isn't a finished design that
can be transformed directly into code. It is a description or
template for how to solve a problem that can be used in many
different situations.

Q1: What is Design Patterns and why anyone


should use them?
Topic: Design Patterns
Difficulty: ⭐

Design patterns are a well-described solution to the most


commonly encountered problems which occur during software
development.

Design pattern represents the best practices evolved over a


period of time by experienced software developers. They
promote reusability which leads to a more robust and
maintainable code.

🔗Source: www.educba.com

Q2: What is Filter pattern?


Topic: Design Patterns
Difficulty: ⭐⭐

Filter pattern or Criteria pattern is a design pattern that


enables developers to filter a set of objects using different
criteria and chaining them in a decoupled way through logical
operations. This type of design pattern comes
under structural pattern as this pattern combines multiple
criteria to obtain single criteria.

Filter design pattern is useful where you want to add filters


dynamically or you are implementing multiple functionalities
and most of them require different filter criteria to filter
something. In that case instead of hard coding the filters inside
the functionalities, you can create filter criteria and re-use it
wherever required.
Consider:

List<Laptop> laptops = LaptopFactory.manufactureInBulk();

AndCriteria searchCriteria = new AndCriteria(

new HardDisk250GBFilter(),

new MacintoshFilter(),

new I5ProcessorFilter());

List<Laptop> filteredLaptops = searchCriteria.meets(laptops);

🔗Source: tutorialspoint.com

Q3: What is Strategy pattern?


Topic: Design Patterns
Difficulty: ⭐⭐

In Strategy pattern, a class behavior or its algorithm can be


changed at run time. This type of design pattern comes
under behavior pattern.

In Strategy pattern, we create objects which represent various


strategies and a context object whose behavior varies as per its
strategy object. The strategy object changes the executing
algorithm of the context object.
🔗Source: tutorialspoint.com

Q4: What is Observer pattern?


Topic: Design Patterns
Difficulty: ⭐⭐⭐

Observer pattern (also known as Publish-Subscribe Pattern) is


used when there is one-to-many relationship between objects
such as if one object is modified, its dependent objects are to be
notified automatically. Observer pattern falls
under behavioral pattern category.

An object with a one-to-many relationship with other objects


who are interested in its state is called the subject or publisher.
The observers are notified whenever the state of
the subject changes and can act accordingly. The subject can
have any number of dependent observers which it notifies, and
any number of observers can subscribe to the subject to receive
such notifications.

Observer pattern uses two actor classes:


 The Observer (os Subscriber) abstract class provides
an update()method which will be called by the subject to
notify it of the subject’s state change.
 The Subject (or Publisher) class is also an abstract class and
defines four primary
methods: attach(), detach(), setState(), and notify()

🔗Source: sitepoint.com

Q5: Why would I ever use a Chain of


Responsibility over a Decorator?
Topic: Design Patterns
Difficulty: ⭐⭐⭐⭐

The key difference is that a Decorator adds new behaviour that


in effect widens the original interface. It is similar to how
normal extension can add methods except the "subclass" is only
coupled by a reference which means that any "superclass" can
be used.

The Chain of Responsibility pattern can modify an existing


behaviour which is similar to overriding an existing method
using inheritance. You can choose to call super.xxx() to
continue up the "chain" or handle the message yourself.

🔗Source: stackoverflow.com

Q6: When would you use the Builder Pattern?


Why not just use a Factory Pattern?
Topic: Design Patterns
Difficulty: ⭐⭐⭐⭐

The builder pattern is a good choice when designing classes


whose constructors or static factories would have more than a
handful of parameters.

Consider a restaurant. The creation of "today's meal" is a


factory pattern, because you tell the kitchen "get me today's
meal" and the kitchen (factory) decides what object to generate,
based on hidden criteria.

The builder appears if you order a custom pizza. In this case,


the waiter tells the chef (builder) "I need a pizza; add cheese,
onions and bacon to it!" Thus, the builder exposes the attributes
the generated object should have, but hides how to set them.

🔗Source: stackoverflow.com

Q7: How is Bridge pattern is different from


Adapter pattern?
Topic: Design Patterns
Difficulty: ⭐⭐⭐⭐
The intent of the Adapter pattern is to make one or more classes'
interfaces look the same as that of a particular class.

The Bridge pattern is designed to separate a class's interface


from its implementation so you can vary or replace the
implementation without changing the client code.

🔗Source: tutorialspoint.com

Q8: What's the difference between the


Dependency Injection and Service Locator
patterns?
Topic: Design Patterns
Difficulty: ⭐⭐⭐⭐⭐

 With the ServiceLocator, the class is still responsible for


creating its dependencies. It just uses the service locator to
do it.
 Service locators hide dependencies - you can't tell by
looking at an object whether it hits a database or not (for
example) when it obtains connections from a locator.
 With DI, the class is given it's dependencies. It neither
knows, nor cares where they come from.

One important result of this is that the DI example is much


easier to unit test -- because you can pass it mock
implementations of its dependent objects. You could combine
the two -- and inject the service locator (or a factory), if you
wanted.

🔗Source: stackoverflow.com
Q9: Explain difference between the Facade,
Proxy, Adapter and Decorator design patterns?
Topic: Design Patterns
Difficulty: ⭐⭐⭐⭐⭐

 Adapter adapts a given class/object to a new interface. In


the case of the former, multiple inheritance is typically
employed. In the latter case, the object is wrapped by a
conforming adapter object and passed around. The
problem we are solving here is that of non-compatible
interfaces.
 Facade is more like a simple gateway to a complicated set
of functionality. You make a black-box for your clients to
worry less i.e. make interfaces simpler.
 Proxy provides the same interface as the proxied-for class
and typically does some housekeeping stuff on its own. (So
instead of making multiple copies of a heavy object X you
make copies of a lightweight proxy P which in turn
manages X and translates your calls as required.) You are
solving the problem of the client from having to manage a
heavy and/or complex object.
 Decorator is used to add more gunpowder to your objects
(note the term objects -- you typically decorate objects
dynamically at runtime). You do not hide/impair the
existing interfaces of the object but simply extend it at
runtime

🔗Source: stackoverflow.com
Powered by Translate

Singleton pattern - Quiz Explanation


The correct answers are indicated below, along with text that explains the
correct answers.
The general purpose of the Singleton pattern is to:
1.
Please select the best answer.

Ensure that no more than one instance of a class exists.


A.

Ensure that only one instance of a class exists at the same time.
B.

Separate objects in a single class from objects in another class.


C.

Control creation of objects in a single class or another class.


D.

The correct answer is A. The Singleton pattern ensures that no more than one
instance of the class exists ever.
It does not allow one instance of the Singleton to be destroyed and then another one
created. In general, a Singleton has nothing to do with separating objects in different
classes. A Singleton does control creation of objects, but not in a single or another
class.

The participants in a design pattern are:


2.
Please select the best answer.

The methods used in the pattern


A.

The other patterns that participate with the pattern


B.

The classes and objects used in the pattern


C.

The fields used in the pattern


D.

The correct answer is C.


A class pattern has mostly class participants. An object pattern has mostly object
participants. However, many patterns have both. Fields and methods only enter into
patterns as parts of the classes and objects they belong to.

The consequences of a design pattern are:


3.
Please select the best answer.

The pitfalls of using the particular pattern


A.

The results of choosing a pattern


B.

The time a program using the pattern takes to run


C.

The time it takes to design a program using the pattern


D.

The correct answer is B. Although the English word consequences has a


somewhat negative connotation, in the context of design patterns it includes all major
results choosing or using a pattern, both positive and negative. Of course for good
patterns that fit the problem they're being used to solve, the positive consequences
should outweigh the negative ones.

Which of the following is specifically in the realm of applicability of the Singleton?


4.
Please select the best answer.

The class has only a single member method.


A.

The class has only a single field.


B.

The class should have exactly one instance.


C.

The class cannot be subclassed.


D.

The correct answer is C.


Although many of the examples in this module used classes with only a single static
field, in more specific cases the class can have many different static and instance
fields and methods and still be a Singleton. The single in Singleton refers to the
number of instances of the class, not the number of fields or methods in the class. D
is incorrect because Singleton classes can be subclassed.
Questions and Answers
 1.
Defines an interface for creating an object, but let the subclasses decide
which class to instantiate. It let the instantiation differ to subclasses.

o A.

Factory Method

o B.

Abstract Factory

o C.

Builder

o D.

Prototype

 2.
Attach additional responsibilities to an object dynamically. It provides a
flexible alternative to subclassing for extending functionality.

o A.

Chain of responsibility

o B.

Adapter

o C.

Decorator

o D.

Composite

 3.
Define a family of algorithms, encapsulate each one, and make them
interchangeable. It lets the algorithm vary independently from clients that
use it.

o A.

Template method
o B.

Decorator

o C.

Strategy

o D.

Visitor

 4.
Define one too many dependencies between objects so that when one
object changes state, all its dependencies are notified and updated
automatically.

o A.

Chain of responsibility

o B.

Event Notification

o C.

Mediator

o D.

Observer

 5.
Encapsulate a request as an object, thereby letting you parametrize clients
with different requests, queue or log requests, and support undoable
operation.

o A.

Adapter

o B.

Command

o C.

Decorator
o D.

Composite

 6.
Ensure a class has only one instance, and provide a global access point to
it.

o A.

Single Class Factory

o B.

Proxy

o C.

Singleton

o D.

Flyweight

 7.
Define the skeleton of an algorithm in an operation, deferring some steps to
subclasses. It lets subclasses redefine certain steps of an algorithm without
changing the algorithm structure.
Discuss

o A.

Chain of responsibility

o B.

Template method

o C.

Interpretor

o D.

Prototype

 8.
Provide a unified interface to a set of interfaces in a subsystem. It defines a
higher level interface that makes the subsystem easier to use.

o A.

Facade

o B.

Mediator

o C.

Adapter

o D.

Strategy

 9.
Provides a way to access the elements of an aggregate object sequentially
without exposing its underlying representation.

o A.

Iterator

o B.

Visitor

o C.

Composite

o D.

Command

 10.
Which of the following is correct about the Singleton design pattern.

o A.

This type of design pattern comes under creational pattern.

o B.

This pattern involves a single class which is responsible to create an object while
making sure that only single object gets created.
o C.

Singleton class provides a way to access its only object which can be accessed
directly without need to instantiate the object of the class

o D.

All of the above.

1.Defines an interface for creating an object, but let the


subclasses decide which class to instantiate. It let the
instantiation differ to subclasses.
Factory Method
Abstract Factory
Builder
Prototype
2.Attach additional responsibilities to an object
dynamically. It provides a flexible alternative to subclassing
for extending functionality.
Chain of responsibility
Adapter
Decorator
Composite
Define a family of algorithms, encapsulate each one,
and make them interchangeable. It lets the algorithm vary
independently from clients that use it.
Template method
Decorator
Strategy
Visitor
Define one too many dependencies between objects so
that when one object changes state, all its dependencies are
notified and updated automatically.
Chain of responsibility
Event Notification
Mediator
Observer
Encapsulate a request as an object, thereby letting you
parametrize clients with different requests, queue or log
requests, and support undoable operation.
Adapter
Command
Decorator
Composite
Ensure a class has only one instance, and provide a global access
point to it.

Single Class Factory

Proxy

Singleton

Flyweight
Define the skeleton of an algorithm in an operation, deferring
some steps to subclasses. It lets subclasses redefine certain
steps of an algorithm without changing the algorithm
structure.
Chain of responsibility
Template method
Interpretor
Prototype

Provide a unified interface to a set of interfaces in a


subsystem. It defines a higher level interface that makes the
subsystem easier to use.
Facade
Mediator
Adapter
Strategy
Provides a way to access the elements of an aggregate
object sequentially without exposing its underlying
representation.
Iterator
Visitor
Composite
Command
v
Difficulty: Easy72% got this correct
Incorrect Discuss this Question
1) Attach additional responsibilities to an object dynamically. It provides a flexible
alternative to subclassing for extending functionality.

A. Chain of responsibility
B. Adapter
C. Decorator
D. Composite
Difficulty: Easy91% got this correct
Incorrect Discuss this Question
2) Ensure a class has only one instance, and provide a global access point to it.

A. Single Class Factory


B. Proxy
C. Singleton
D. Flyweight
Difficulty: Easy78% got this correct
Incorrect Discuss this Question
3) Provide a unified interface to a set of interfaces in a subsystem. It defines a higher level
interface that makes the subsystem easier to use.

A. Facade
B. Mediator
C. Adapter
D. Strategy

Related Qu
Correct Difficulty: Easy64% got this correct
1) Defines an interface for creating an object, but let the subclasses decide which class to
instantiate. It let the instantiation differ to subclasses.

A. Factory Method
B. Abstract Factory
C. Builder
D. Prototype
Correct Difficulty: Easy74% got this correct
2) Define a family of algorithms, encapsulate each one, and make them interchangeable. It
lets the algorithm vary independently from clients that use it.

A. Template method
B. Decorator
C. Strategy
D. Visitor
Correct Difficulty: Easy78% got this correct
3) Define one too many dependencies between objects so that when one object changes state, all
its dependencies are notified and updated automatically.
A. Chain of responsibility
B. Event Notification
C. Mediator
D. Observer
Difficulty:
Correct Easy75%
got this
correct
4) Encapsulate a request as an object, thereby letting you parametrize clients with different
requests, queue or log requests, and support undoable operation.

A. Adapter
B. Command
C. Decorator
D. Composite
Correct Difficulty: Easy70% got this correct
5) Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. It
lets subclasses redefine certain steps of an algorithm without changing the algorithm
structure.

A. Chain of responsibility
B. Template method
C. Interpretor
D. Prototype
Correct Difficulty: Easy72% got this correct
6) Provides a way to access the elements of an aggregate object sequentially without exposing
its underlying representation.

A. Iterator
B. Visitor
C. Composite
D. Command
Correct Difficulty: Easy63% got this correct
7) Which of the following is correct about the Singleton design pattern.

A. This type of design pattern comes under creational pattern.


B. This pattern involves a single class which is responsible to create an object while
making sure that only single object gets created.
C. Singleton class provides a way to access its only object which can be accessed directly
without need to instantiate the object of the class
D. All of the above.

S-ar putea să vă placă și