Skip to content

Commit 2aa9dd2

Browse files
committed
Update java-design-pattern-questions.md
1 parent da08ca5 commit 2aa9dd2

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

java-design-pattern-questions.md

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Java Design Pattern Questions and Answers
22

3-
#### Q. Exaplain MVC, Front-Controller, DAO, DTO, Service-Locator, Prototype design patterns?
3+
## Q. Exaplain MVC, Front-Controller, DAO, DTO, Service-Locator, Prototype design patterns?
44

55
**2. Front-Controller**
66

@@ -12,7 +12,7 @@ The front controller design pattern is used to provide a centralized request han
1212

1313
* View - Views are the object for which the requests are made.
1414

15-
Example:
15+
*Example:*
1616

1717
We are going to create a `FrontController` and `Dispatcher` to act as Front Controller and Dispatcher correspondingly. `HomeView` and `StudentView` represent various views for which requests can come to front controller.
1818

@@ -88,11 +88,11 @@ public class FrontController {
8888
public void dispatchRequest(String request){
8989
//log each request
9090
trackRequest(request);
91-
91+
9292
//authenticate the user
9393
if(isAuthenticUser()){
9494
dispatcher.dispatch(request);
95-
}
95+
}
9696
}
9797
}
9898
```
@@ -112,19 +112,24 @@ public class FrontControllerPatternDemo {
112112
}
113113
```
114114

115-
*ToDo*
115+
<div align="right">
116+
<b><a href="#">↥ back to top</a></b>
117+
</div>
116118

117119
## Q. What are the design patterns available in Java?
120+
118121
Java Design Patterns are divided into three categories – creational, structural, and behavioral design patterns.
119122

120123
**1. Creational Design Patterns**
124+
121125
* Singleton Pattern
122126
* Factory Pattern
123127
* Abstract Factory Pattern
124128
* Builder Pattern
125129
* Prototype Pattern
126130

127131
**2. Structural Design Patterns**
132+
128133
* Adapter Pattern
129134
* Composite Pattern
130135
* Proxy Pattern
@@ -134,6 +139,7 @@ Java Design Patterns are divided into three categories – creational, structura
134139
* Decorator Pattern
135140

136141
**3. Behavioral Design Patterns**
142+
137143
* Template Method Pattern
138144
* Mediator Pattern
139145
* Chain of Responsibility Pattern
@@ -147,11 +153,17 @@ Java Design Patterns are divided into three categories – creational, structura
147153
* Memento Pattern
148154

149155
**4. Miscellaneous Design Patterns**
156+
150157
* DAO Design Pattern
151158
* Dependency Injection Pattern
152159
* MVC Pattern
153160

154-
## Q. Explain Singleton Design Pattern in Java?
161+
<div align="right">
162+
<b><a href="#">↥ back to top</a></b>
163+
</div>
164+
165+
## Q. Explain Singleton Design Pattern in Java?
166+
155167
**1. Eager initialization:**
156168
In eager initialization, the instance of Singleton Class is created at the time of class loading.
157169

@@ -254,11 +266,13 @@ public class BillPughSingleton {
254266
}
255267
}
256268
```
269+
257270
<div align="right">
258271
<b><a href="#">↥ back to top</a></b>
259272
</div>
260273

261274
## Q. Explain Adapter Design Pattern in Java?
275+
262276
Adapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter.
263277

264278
Example:
@@ -337,15 +351,18 @@ public class Main {
337351
}
338352
}
339353
```
354+
340355
<div align="right">
341356
<b><a href="#">↥ back to top</a></b>
342357
</div>
343358

344359
## Q. Explain Factory Design Pattern in Java?
360+
345361
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.
346362

347363
Example: Calculate Electricity Bill
348364
Plan.java
365+
349366
```java
350367
import java.io.*;
351368
abstract class Plan {
@@ -435,11 +452,13 @@ class GenerateBill {
435452
}
436453
}
437454
```
455+
438456
<div align="right">
439457
<b><a href="#">↥ back to top</a></b>
440458
</div>
441459

442460
## Q. Explain Strategy Design Pattern in Java?
461+
443462
Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.
444463

445464
Example: Simple Shopping Cart where we have two payment strategies – using Credit Card or using PayPal.
@@ -570,6 +589,11 @@ Output
570589
500 paid using Paypal.
571590
500 paid with credit/debit card
572591
```
592+
593+
<div align="right">
594+
<b><a href="#">↥ back to top</a></b>
595+
</div>
596+
573597
#### Q. When do you use Flyweight pattern?
574598
#### Q. What is difference between dependency injection and factory design pattern?
575599
#### Q. Difference between Adapter and Decorator pattern?

0 commit comments

Comments
 (0)