You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -12,7 +12,7 @@ The front controller design pattern is used to provide a centralized request han
12
12
13
13
* View - Views are the object for which the requests are made.
14
14
15
-
Example:
15
+
*Example:*
16
16
17
17
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.
18
18
@@ -88,11 +88,11 @@ public class FrontController {
88
88
publicvoiddispatchRequest(Stringrequest){
89
89
//log each request
90
90
trackRequest(request);
91
-
91
+
92
92
//authenticate the user
93
93
if(isAuthenticUser()){
94
94
dispatcher.dispatch(request);
95
-
}
95
+
}
96
96
}
97
97
}
98
98
```
@@ -112,19 +112,24 @@ public class FrontControllerPatternDemo {
112
112
}
113
113
```
114
114
115
-
*ToDo*
115
+
<divalign="right">
116
+
<b><a href="#">↥ back to top</a></b>
117
+
</div>
116
118
117
119
## Q. What are the design patterns available in Java?
120
+
118
121
Java Design Patterns are divided into three categories – creational, structural, and behavioral design patterns.
119
122
120
123
**1. Creational Design Patterns**
124
+
121
125
* Singleton Pattern
122
126
* Factory Pattern
123
127
* Abstract Factory Pattern
124
128
* Builder Pattern
125
129
* Prototype Pattern
126
130
127
131
**2. Structural Design Patterns**
132
+
128
133
* Adapter Pattern
129
134
* Composite Pattern
130
135
* Proxy Pattern
@@ -134,6 +139,7 @@ Java Design Patterns are divided into three categories – creational, structura
134
139
* Decorator Pattern
135
140
136
141
**3. Behavioral Design Patterns**
142
+
137
143
* Template Method Pattern
138
144
* Mediator Pattern
139
145
* Chain of Responsibility Pattern
@@ -147,11 +153,17 @@ Java Design Patterns are divided into three categories – creational, structura
147
153
* Memento Pattern
148
154
149
155
**4. Miscellaneous Design Patterns**
156
+
150
157
* DAO Design Pattern
151
158
* Dependency Injection Pattern
152
159
* MVC Pattern
153
160
154
-
## Q. Explain Singleton Design Pattern in Java?
161
+
<divalign="right">
162
+
<b><a href="#">↥ back to top</a></b>
163
+
</div>
164
+
165
+
## Q. Explain Singleton Design Pattern in Java?
166
+
155
167
**1. Eager initialization:**
156
168
In eager initialization, the instance of Singleton Class is created at the time of class loading.
157
169
@@ -254,11 +266,13 @@ public class BillPughSingleton {
254
266
}
255
267
}
256
268
```
269
+
257
270
<divalign="right">
258
271
<b><a href="#">↥ back to top</a></b>
259
272
</div>
260
273
261
274
## Q. Explain Adapter Design Pattern in Java?
275
+
262
276
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.
263
277
264
278
Example:
@@ -337,15 +351,18 @@ public class Main {
337
351
}
338
352
}
339
353
```
354
+
340
355
<divalign="right">
341
356
<b><a href="#">↥ back to top</a></b>
342
357
</div>
343
358
344
359
## Q. Explain Factory Design Pattern in Java?
360
+
345
361
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.
346
362
347
363
Example: Calculate Electricity Bill
348
364
Plan.java
365
+
349
366
```java
350
367
importjava.io.*;
351
368
abstractclassPlan {
@@ -435,11 +452,13 @@ class GenerateBill {
435
452
}
436
453
}
437
454
```
455
+
438
456
<divalign="right">
439
457
<b><a href="#">↥ back to top</a></b>
440
458
</div>
441
459
442
460
## Q. Explain Strategy Design Pattern in Java?
461
+
443
462
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.
444
463
445
464
Example: Simple Shopping Cart where we have two payment strategies – using Credit Card or using PayPal.
@@ -570,6 +589,11 @@ Output
570
589
500 paid using Paypal.
571
590
500 paid with credit/debit card
572
591
```
592
+
593
+
<divalign="right">
594
+
<b><a href="#">↥ back to top</a></b>
595
+
</div>
596
+
573
597
#### Q. When do you use Flyweight pattern?
574
598
#### Q. What is difference between dependency injection and factory design pattern?
575
599
#### Q. Difference between Adapter and Decorator pattern?
0 commit comments