Design Patterns Interview Question and Answers
What is Design Pattern?
DP is nothing but solution for the particular problem in specific context.
2) What are major types of DP ?
Creation Pattern
Structured Pattern
Behavior Pattern
3) What are pattern under the Creation Pattern Umberlla?
1) Builder Pattern
2) ProtoType Pattern
3) Singleton Pattern
4) Factory Pattern
4) What are patterns under the Structural Pattern
5) What are the patterns are under the Behavior Patterns
1) Chain of Responsiblity
2) State
3) Command
4) Strategy
5) Observer
6) Visitor
7) Command
8) Template
9) Iterator
10) Momentum
Builder Pattern :
Its mainly used to separate the Object construction from Object representation
Ex: In our world customer sign in the Mobile phone (Post Paid/Pre Paid) with different Combo Plan /packages
Construction is Mobile Plan signin and representation of the mobile plan is as follows
Construction is
Mobileplan plan =new Postpaid()..
Representation is
MobilePlan plan = new Postpaid(line,sms,mms) ;
Singleton Pattern :
Allows to create the Single instance in the JVM level .
It mainly used in Logging/Cache/Data Source/Thread Pooling and etc
Ways to Implement the Singleton :
1) Make the constructor with Private along with Synchronized block/statement
2) Use Enum
3) Use annotation (JDK 1.5)
6) How to implement the Singleton in Cluster level ?
We can create the Singleton instance in cluster level in following ways
1) Application Service API from respective Apps Server (JBOSS/Weblogic)
2) JGroups use to create the Singleton in Cluster level
3) Terracotta/oracle coherence tool to implement the Singleton in cluster
7) What is difference between Singleton Pattern and Singleton Bean?
Singtleton Pattern , create the single instance in JVM level
Singleton Bean ,Create the single instance bean in Spring Container level
8) What is proxy Pattern ?
Object representation by another object
Ex:
In Real world, Customer purchase do the online shopping and payment through credit card/debit card indirectly it will deducted the amount from your bank account ie instead of using bank account directly will use the CC/DC for shopping . Here CC/DC acts as Proxy for Bank Account
In our world , Consumer not able to access our Business Service directly instead they are consume those services through Proxy services.
9) What is difference between Adapter and Mediator Pattern ?
Mediator :
Avoid the tightly coupling between the object or System
Ex: ESB
ESB act as Mediator in SOA world , Consumer consume the services through ESB instead of accessing directly to Back end Services.
Adapter :
Its used to transform one format to another format
Ex: CE
In our world we are using Consumer Adapter used to convert the message to one format to another format.
10) What is flyweight pattern ?
Its mainly used to create the fine grained instance in the application level .
Ex: Connection Pooling , How to open /release the connection effectively
11) What is advantages of Facade Pattern ?
Its mainly used to access entire application
Advantages
1) Reduce the network call
2) Abstract entire system implementation
3) Define the Txn Boundry
Ex: In our we have different ways to perform the payment ie CC/Enets/Paypal/Mobile wallet all are treat as payment sub system umbrella. if customer initiate the payment it will call the facade for the payment module instead of calling individual system separately.
12) Decorator Pattern :
Adding additional responsibility dynamically .
https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm
Ex: In our world ,Create the purchase order with discount for some plan .Adding discount dynamically.
13) State Pattern :
Changes the state of object Dynamically .
Ex: In the real world , Fan with Regulator , whenever we change the regulator fans speed will change.
14) Visitor Pattern:
Mainly for Open for Extension and closed for Modification principle
Ex: API from any framework , we can extend their capabilities not able to modify the original.
15) What is difference between Command and Chain of Responsibility Pattern?
CCR :
Manage algorithms, relationships and responsibilities between objects
Ex:
Processing Refund , it will send to first CSR--> Reviewer --> Approver
Command Pattern :
It allows the requester of a particular action to be decoupled from the object that performs the action
enable loose coupling between Invoker and Receiver
Ex: Order the mobile phone
Customer Initiate--> Eligiblity --> Create order in BSS--> Create order in OSS--> Provisioning --> Status updates in BSS--> Billing account creation --> Delivery
Command VS Strategy
Command Pattern : enable loose coupling between Invoker and Receiver
Strategy :
Multiple implementations (algorithms) for a given feature and you want to change the algorithm at runtime depending on parameter type.
Ex: Create Customer
Normal Customer and Premium Customer
15 ) Come with Proper Design Pattern for Hotel Management
Step 1 : List of User Stories
1) Customer Registration -
Normal
Premium Customer
2) Room Details
3) Res truant
4) Payment
Design Pattern
Factory Pattern -
While customer Registration , We can create the customer based on type
Command Pattern
Order the Food in the restruant
Facade Pattern
To Perform the payment with different option
Observer Pattern
To notify to promotion to our existing customer
Builder Pattern
To choose the Menu /Choose the room in Hotel
Chain of Responsbility
Book the room
Decorator Pattern
To settle the bill with some discount .
Adaptor Pattern
To deal with external Travel Agency to book the tour
Strategy Pattern
Refund /Cancel the room in N of days
16_ Prototype and Template Design Pattern
Prototype ,Creating instance is very expensive . Instead of creating the Instance from scratch will clone the object
Template , Use framework to handle the algorithm,
.
DP is nothing but solution for the particular problem in specific context.
2) What are major types of DP ?
Creation Pattern
Structured Pattern
Behavior Pattern
3) What are pattern under the Creation Pattern Umberlla?
1) Builder Pattern
2) ProtoType Pattern
3) Singleton Pattern
4) Factory Pattern
4) What are patterns under the Structural Pattern
1) Proxy Pattern
2) Facade
3) Decorator
4) Adapator
5) FlyWeight Pattern
5) What are the patterns are under the Behavior Patterns
1) Chain of Responsiblity
2) State
3) Command
4) Strategy
5) Observer
6) Visitor
7) Command
8) Template
9) Iterator
10) Momentum
Builder Pattern :
Its mainly used to separate the Object construction from Object representation
Ex: In our world customer sign in the Mobile phone (Post Paid/Pre Paid) with different Combo Plan /packages
Construction is Mobile Plan signin and representation of the mobile plan is as follows
Construction is
Mobileplan plan =new Postpaid()..
Representation is
MobilePlan plan = new Postpaid(line,sms,mms) ;
Singleton Pattern :
Allows to create the Single instance in the JVM level .
It mainly used in Logging/Cache/Data Source/Thread Pooling and etc
Ways to Implement the Singleton :
1) Make the constructor with Private along with Synchronized block/statement
2) Use Enum
3) Use annotation (JDK 1.5)
6) How to implement the Singleton in Cluster level ?
We can create the Singleton instance in cluster level in following ways
1) Application Service API from respective Apps Server (JBOSS/Weblogic)
2) JGroups use to create the Singleton in Cluster level
3) Terracotta/oracle coherence tool to implement the Singleton in cluster
7) What is difference between Singleton Pattern and Singleton Bean?
Singtleton Pattern , create the single instance in JVM level
Singleton Bean ,Create the single instance bean in Spring Container level
8) What is proxy Pattern ?
Object representation by another object
Ex:
In Real world, Customer purchase do the online shopping and payment through credit card/debit card indirectly it will deducted the amount from your bank account ie instead of using bank account directly will use the CC/DC for shopping . Here CC/DC acts as Proxy for Bank Account
In our world , Consumer not able to access our Business Service directly instead they are consume those services through Proxy services.
9) What is difference between Adapter and Mediator Pattern ?
Mediator :
Avoid the tightly coupling between the object or System
Ex: ESB
ESB act as Mediator in SOA world , Consumer consume the services through ESB instead of accessing directly to Back end Services.
Adapter :
Its used to transform one format to another format
Ex: CE
In our world we are using Consumer Adapter used to convert the message to one format to another format.
10) What is flyweight pattern ?
Its mainly used to create the fine grained instance in the application level .
Ex: Connection Pooling , How to open /release the connection effectively
11) What is advantages of Facade Pattern ?
Its mainly used to access entire application
Advantages
1) Reduce the network call
2) Abstract entire system implementation
3) Define the Txn Boundry
Ex: In our we have different ways to perform the payment ie CC/Enets/Paypal/Mobile wallet all are treat as payment sub system umbrella. if customer initiate the payment it will call the facade for the payment module instead of calling individual system separately.
12) Decorator Pattern :
Adding additional responsibility dynamically .
https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm
Ex: In our world ,Create the purchase order with discount for some plan .Adding discount dynamically.
13) State Pattern :
Changes the state of object Dynamically .
Ex: In the real world , Fan with Regulator , whenever we change the regulator fans speed will change.
14) Visitor Pattern:
Mainly for Open for Extension and closed for Modification principle
Ex: API from any framework , we can extend their capabilities not able to modify the original.
15) What is difference between Command and Chain of Responsibility Pattern?
CCR :
Manage algorithms, relationships and responsibilities between objects
Ex:
Processing Refund , it will send to first CSR--> Reviewer --> Approver
Command Pattern :
It allows the requester of a particular action to be decoupled from the object that performs the action
enable loose coupling between Invoker and Receiver
Ex: Order the mobile phone
Customer Initiate--> Eligiblity --> Create order in BSS--> Create order in OSS--> Provisioning --> Status updates in BSS--> Billing account creation --> Delivery
Command VS Strategy
Command Pattern : enable loose coupling between Invoker and Receiver
Strategy :
Multiple implementations (algorithms) for a given feature and you want to change the algorithm at runtime depending on parameter type.
Ex: Create Customer
Normal Customer and Premium Customer
15 ) Come with Proper Design Pattern for Hotel Management
Step 1 : List of User Stories
1) Customer Registration -
Normal
Premium Customer
2) Room Details
3) Res truant
4) Payment
Design Pattern
Factory Pattern -
While customer Registration , We can create the customer based on type
Command Pattern
Order the Food in the restruant
Facade Pattern
To Perform the payment with different option
Observer Pattern
To notify to promotion to our existing customer
Builder Pattern
To choose the Menu /Choose the room in Hotel
Chain of Responsbility
Book the room
Decorator Pattern
To settle the bill with some discount .
Adaptor Pattern
To deal with external Travel Agency to book the tour
Strategy Pattern
Refund /Cancel the room in N of days
16_ Prototype and Template Design Pattern
Prototype ,Creating instance is very expensive . Instead of creating the Instance from scratch will clone the object
Template , Use framework to handle the algorithm,
.
Comments
Post a Comment