-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.html
1730 lines (1655 loc) · 70.3 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>README.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
body {
font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
font-size: var(--vscode-markdown-font-size, 14px);
padding: 0 26px;
line-height: var(--vscode-markdown-line-height, 22px);
word-wrap: break-word;
}
#code-csp-warning {
position: fixed;
top: 0;
right: 0;
color: white;
margin: 16px;
text-align: center;
font-size: 12px;
font-family: sans-serif;
background-color:#444444;
cursor: pointer;
padding: 6px;
box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}
#code-csp-warning:hover {
text-decoration: none;
background-color:#007acc;
box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}
body.scrollBeyondLastLine {
margin-bottom: calc(100vh - 22px);
}
body.showEditorSelection .code-line {
position: relative;
}
body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
content: "";
display: block;
position: absolute;
top: 0;
left: -12px;
height: 100%;
}
body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
left: -30px;
}
.vscode-light.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(0, 0, 0, 0.15);
}
.vscode-light.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(0, 0, 0, 0.40);
}
.vscode-light.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-dark.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 255, 255, 0.4);
}
.vscode-dark.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 255, 255, 0.60);
}
.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-high-contrast.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 160, 0, 0.7);
}
.vscode-high-contrast.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 160, 0, 1);
}
.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
img {
max-width: 100%;
max-height: 100%;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:focus,
input:focus,
select:focus,
textarea:focus {
outline: 1px solid -webkit-focus-ring-color;
outline-offset: -1px;
}
hr {
border: 0;
height: 2px;
border-bottom: 2px solid;
}
h1 {
padding-bottom: 0.3em;
line-height: 1.2;
border-bottom-width: 1px;
border-bottom-style: solid;
}
h1, h2, h3 {
font-weight: normal;
}
table {
border-collapse: collapse;
}
table > thead > tr > th {
text-align: left;
border-bottom: 1px solid;
}
table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
padding: 5px 10px;
}
table > tbody > tr + tr > td {
border-top: 1px solid;
}
blockquote {
margin: 0 7px 0 5px;
padding: 0 16px 0 10px;
border-left-width: 5px;
border-left-style: solid;
}
code {
font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
font-size: 1em;
line-height: 1.357em;
}
body.wordWrap pre {
white-space: pre-wrap;
}
pre:not(.hljs),
pre.hljs code > div {
padding: 16px;
border-radius: 3px;
overflow: auto;
}
pre code {
color: var(--vscode-editor-foreground);
tab-size: 4;
}
/** Theming */
.vscode-light pre {
background-color: rgba(220, 220, 220, 0.4);
}
.vscode-dark pre {
background-color: rgba(10, 10, 10, 0.4);
}
.vscode-high-contrast pre {
background-color: rgb(0, 0, 0);
}
.vscode-high-contrast h1 {
border-color: rgb(0, 0, 0);
}
.vscode-light table > thead > tr > th {
border-color: rgba(0, 0, 0, 0.69);
}
.vscode-dark table > thead > tr > th {
border-color: rgba(255, 255, 255, 0.69);
}
.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
border-color: rgba(0, 0, 0, 0.18);
}
.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
border-color: rgba(255, 255, 255, 0.18);
}
</style>
<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
color: #8e908c;
}
/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #c82829;
}
/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
color: #f5871f;
}
/* Tomorrow Yellow */
.hljs-attribute {
color: #eab700;
}
/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #718c00;
}
/* Tomorrow Blue */
.hljs-title,
.hljs-section {
color: #4271ae;
}
/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #8959a8;
}
.hljs {
display: block;
overflow-x: auto;
color: #4d4d4c;
padding: 0.5em;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
</style>
<style>
/*
* Markdown PDF CSS
*/
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
padding: 0 12px;
}
pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
border-radius: 3px;
overflow-x: auto;
white-space: pre-wrap;
overflow-wrap: break-word;
}
pre:not(.hljs) {
padding: 23px;
line-height: 19px;
}
blockquote {
background: rgba(127, 127, 127, 0.1);
border-color: rgba(0, 122, 204, 0.5);
}
.emoji {
height: 1.4em;
}
code {
font-size: 14px;
line-height: 19px;
}
/* for inline code */
:not(pre):not(.hljs) > code {
color: #C9AE75; /* Change the old color so it seems less like an error */
font-size: inherit;
}
/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
page-break-after: always;
}
</style>
<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
<script>
mermaid.initialize({
startOnLoad: true,
theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? 'dark'
: 'default'
});
</script>
<h1 id="%F0%9F%8E%AF-java-design-patterns-gof-collection">🎯 Java Design Patterns (GoF) Collection</h1>
<p>Welcome to the <strong>Java Design Patterns (GoF)</strong> repository! This project demonstrates the power of <strong>Gang of Four (GoF)</strong> design patterns through real-world <strong>mini systems</strong> implemented in Java. Each pattern is neatly explained and organized into categories with dedicated examples.</p>
<hr>
<h1 id="%F0%9F%A7%A0-about-the-project">🧠 About the Project</h1>
<p>This repository is built to help Java developers <strong>learn, understand, and implement</strong> core <strong>Design Patterns</strong> through practical, easy-to-understand <strong>mini Java systems</strong>. It covers the three main categories:</p>
<ul>
<li>🛠️ <strong>Creational Patterns</strong></li>
<li>🏗️ <strong>Structural Patterns</strong></li>
<li>🔁 <strong>Behavioral Patterns</strong></li>
</ul>
<p>All examples follow <strong>best coding practices</strong> and include visual aids and commentary to enhance comprehension.</p>
<hr>
<h2 id="%F0%9F%93%81-project-structure">📁 Project Structure</h2>
<ul>
<li>📂 Example path:
Design-Patterns/ Creational Patterns/ AbstractFactory Pattern/ src/ Food_Ordering_System/</li>
</ul>
<hr>
<h3 id="each-pattern-is-organized-in-its-own-folder-and-includes">Each pattern is organized in its own folder and includes:</h3>
<ul>
<li>📄 Java source code</li>
<li>🧪 A mini project/system implementing the pattern</li>
<li>📘 Inline comments, structure diagram, class diagram, participants, and consequences</li>
</ul>
<hr>
<h2 id="%F0%9F%A7%B0-covered-design-patternsto-do-list">🧰 Covered Design Patterns(TO-DO List)</h2>
<table>
<thead>
<tr>
<th>🔨 Creational Patterns</th>
<th>🏗️ Structural Patterns</th>
<th>🔁 Behavioral Patterns</th>
</tr>
</thead>
<tbody>
<tr>
<td>✅ Factory</td>
<td>❌ Adapter</td>
<td>✅ Chain of Responsibility(COR)</td>
</tr>
<tr>
<td>✅ Abstract Factory</td>
<td>❌ Bridge</td>
<td>❌ Command</td>
</tr>
<tr>
<td>❌ Builder</td>
<td>❌ Composite</td>
<td>❌ Iterator</td>
</tr>
<tr>
<td>❌ Prototype</td>
<td>✅ Decorator</td>
<td>❌ Mediator</td>
</tr>
<tr>
<td>✅ Singleton</td>
<td>✅ Facade</td>
<td>❌ Memento</td>
</tr>
<tr>
<td></td>
<td>❌ Flyweight</td>
<td>✅ Observer</td>
</tr>
<tr>
<td></td>
<td>✅ Proxy</td>
<td>✅ State</td>
</tr>
<tr>
<td></td>
<td></td>
<td>❌ Strategy</td>
</tr>
<tr>
<td></td>
<td></td>
<td>❌ Template Method</td>
</tr>
<tr>
<td></td>
<td></td>
<td>❌ Visitor</td>
</tr>
</tbody>
</table>
<p>✅ = Implemented | ❌ = Coming Soon</p>
<hr>
<h2 id="%F0%9F%92%BB-language--tools">💻 Language & Tools</h2>
<ul>
<li>💡 <strong>Language:</strong> Java (JDK 8+)</li>
<li>🧱 <strong>Architecture:</strong> Object-Oriented Programming (OOP)</li>
<li>✅ <strong>Status:</strong> All implemented patterns are compiled and tested with sample use cases</li>
</ul>
<hr>
<h1 id="%F0%9F%93%98-what-is-a-design-pattern">📘 What is a Design Pattern?</h1>
<h2 id="%E2%9C%85-definition-based-on-gof">✅ Definition (Based on GoF)</h2>
<p>A <strong>design pattern</strong> is a <strong>reusable solution</strong> to a <strong>common problem</strong> that occurs in software design.</p>
<p>It is <strong>not a complete code</strong>, but a <strong>template or guide</strong> for how to solve a problem that keeps coming up in different projects.</p>
<blockquote>
<p><strong>GoF Definition:</strong><br>
"<em>A design pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem in such a way that you can use this solution a million times over, without ever doing it the same way twice.</em>"</p>
</blockquote>
<hr>
<h2 id="%F0%9F%A7%A0-key-points">🧠 Key Points</h2>
<ul>
<li>Design patterns are <strong>tried and tested solutions</strong>.</li>
<li>They are <strong>language-independent</strong> (but can be implemented in Java, C++, etc.).</li>
<li>Improve <strong>code reuse</strong>, <strong>flexibility</strong>, and <strong>maintainability</strong>.</li>
<li>GoF categorized <strong>23 design patterns</strong> into:
<ul>
<li><strong>Creational Patterns</strong></li>
<li><strong>Structural Patterns</strong></li>
<li><strong>Behavioral Patterns</strong></li>
</ul>
</li>
</ul>
<hr>
<h1 id="%F0%9F%A7%AD-how-to-select-a-design-pattern--solve-design-problems--gof-way">🧭 How to Select a Design Pattern & Solve Design Problems ? (GoF Way)</h1>
<h2 id="%E2%9C%85-step-by-step-approach-from-gof">✅ Step-by-Step Approach from GoF</h2>
<p>The <strong>Gang of Four (GoF)</strong> suggests using patterns by <strong>understanding the problem first</strong>, then finding a pattern that fits. Here's how:</p>
<hr>
<h2 id="%F0%9F%9B%A0%EF%B8%8F-1-understand-the-problem">🛠️ 1. Understand the Problem</h2>
<ul>
<li><strong>What are you trying to solve?</strong></li>
<li>Is it about <strong>object creation</strong>, <strong>object structure</strong>, or <strong>object behavior</strong>?</li>
<li>Example questions:
<ul>
<li>Do you want to restrict object creation? (→ <em>Creational</em>)</li>
<li>Do you want to make objects easier to change or extend? (→ <em>Structural</em>)</li>
<li>Do you want objects to communicate flexibly? (→ <em>Behavioral</em>)</li>
</ul>
</li>
</ul>
<hr>
<h2 id="%F0%9F%94%8D-2-classify-the-problem">🔍 2. Classify the Problem</h2>
<p>Check what kind of problem you're solving:</p>
<table>
<thead>
<tr>
<th>Type</th>
<th>Pattern Category</th>
</tr>
</thead>
<tbody>
<tr>
<td>Object creation</td>
<td>Creational</td>
</tr>
<tr>
<td>Class structure</td>
<td>Structural</td>
</tr>
<tr>
<td>Object behavior</td>
<td>Behavioral</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="%F0%9F%A7%A9-3-look-for-similar-use-cases">🧩 3. Look for Similar Use Cases</h2>
<p>Read the <strong>intent</strong> and <strong>applicability</strong> section of each pattern in the GoF book.</p>
<p>Example:</p>
<blockquote>
<p>Want to ensure only one instance of a class?<br>
✅ Use <strong>Singleton Pattern</strong></p>
</blockquote>
<hr>
<h1 id="%F0%9F%92%A1-what-do-you-mean-by-%22instance%22-in-java">💡 What Do You Mean by "Instance" in Java?</h1>
<h2 id="%E2%9C%85-definition">✅ Definition</h2>
<p>An <strong>instance</strong> is a <strong>concrete object</strong> created from a <strong>class</strong>.<br>
You can think of a class as a <strong>blueprint</strong>, and an instance as the <strong>actual object</strong> built using that blueprint.</p>
<hr>
<h2 id="%F0%9F%8F%A0-real-life-analogy">🏠 Real-life Analogy</h2>
<ul>
<li><strong>Class</strong> = House Blueprint</li>
<li><strong>Instance</strong> = The actual House built from that blueprint</li>
</ul>
<p>So when you create an object using <code>new</code>, you're <strong>instantiating</strong> a class — creating an instance!</p>
<hr>
<h2 id="%E2%98%95-java-example">☕ Java Example</h2>
<pre class="hljs"><code><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> </span>{
String name;
Student(String name) {
<span class="hljs-keyword">this</span>.name = name;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span> </span>{
System.out.println(<span class="hljs-string">"Hello, my name is "</span> + name);
}
}
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
Student s1 = <span class="hljs-keyword">new</span> Student(<span class="hljs-string">"Aman"</span>); <span class="hljs-comment">// s1 is an instance of Student</span>
Student s2 = <span class="hljs-keyword">new</span> Student(<span class="hljs-string">"Riya"</span>); <span class="hljs-comment">// s2 is another instance</span>
s1.sayHello(); <span class="hljs-comment">// Output: Hello, my name is Aman</span>
s2.sayHello(); <span class="hljs-comment">// Output: Hello, my name is Riya</span>
}
}
</div></code></pre>
<hr>
<h2 id="%F0%9F%A7%A0-4-use-pattern-selection-hints-gof-tips">🧠 4. Use Pattern Selection Hints (GoF Tips)</h2>
<p>GoF provides clues based on problem types:</p>
<table>
<thead>
<tr>
<th>Situation</th>
<th>Pattern Suggestion</th>
</tr>
</thead>
<tbody>
<tr>
<td>You want to vary object creation</td>
<td>Abstract Factory, Builder, Prototype</td>
</tr>
<tr>
<td>You want one object only</td>
<td>Singleton</td>
</tr>
<tr>
<td>You want to compose objects into trees</td>
<td>Composite</td>
</tr>
<tr>
<td>You want to change behavior at runtime</td>
<td>Strategy, State</td>
</tr>
<tr>
<td>You want to avoid tight coupling</td>
<td>Observer, Mediator</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="%E2%98%95-example-solving-with-design-pattern-in-java">☕ Example: Solving with Design Pattern in Java</h2>
<p><strong>Problem:</strong> You need to create different types of documents (PDF, Word) but don’t want to hardcode creation.</p>
<p><strong>Solution:</strong> Use <strong>Factory Method</strong> pattern.</p>
<pre class="hljs"><code><div><span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Document</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span></span>;
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PDFDocument</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Document</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span> </span>{
System.out.println(<span class="hljs-string">"Opening PDF Document"</span>);
}
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WordDocument</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Document</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span> </span>{
System.out.println(<span class="hljs-string">"Opening Word Document"</span>);
}
}
<span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Application</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> Document <span class="hljs-title">createDocument</span><span class="hljs-params">()</span></span>;
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PDFApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Application</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">public</span> Document <span class="hljs-title">createDocument</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> PDFDocument();
}
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WordApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Application</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">public</span> Document <span class="hljs-title">createDocument</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> WordDocument();
}
}
</div></code></pre>
<h1 id="%F0%9F%9B%A0%EF%B8%8F-creational-pattern">🛠️ Creational Pattern</h1>
<h1 id="%F0%9F%9B%A0%EF%B8%8Fabstract-factory-pattern">🛠️Abstract Factory Pattern</h1>
<h2 id="%F0%9F%93%8C-intent">📌 Intent</h2>
<blockquote>
<p><strong>"Provide an interface for creating families of related or dependent objects without specifying their concrete classes."</strong><br>
(GoF, Design Patterns: Elements of Reusable Object-Oriented Software, Page 87)</p>
</blockquote>
<hr>
<h2 id="%F0%9F%A7%BE-also-known-as">🧾 Also Known As</h2>
<ul>
<li><strong>Kit</strong> (mentioned informally in the GoF book)</li>
</ul>
<hr>
<h2 id="%F0%9F%93%88-applicability-when-to-use">📈 Applicability (When to Use)</h2>
<p>Use the Abstract Factory pattern when:</p>
<ul>
<li>You want to create families of related objects that must be used together.</li>
<li>You want to enforce consistency among products.</li>
<li>You want to hide the concrete classes from the client code.</li>
<li>The system should be independent of how its products are created, composed, and represented.</li>
</ul>
<p>(Reference: GoF, Page 88)</p>
<hr>
<h2 id="%F0%9F%91%A5-participants-gof-book-page-90">👥 Participants (GoF Book, Page 90)</h2>
<ol>
<li><strong>AbstractFactory</strong></li>
</ol>
<ul>
<li>Declares an interface for operations that create abstract product objects.</li>
</ul>
<ol start="2">
<li><strong>ConcreteFactory</strong></li>
</ol>
<ul>
<li>Implements the operations to create concrete product objects.</li>
</ul>
<ol start="3">
<li><strong>AbstractProduct</strong></li>
</ol>
<ul>
<li>Declares an interface for a type of product object.</li>
</ul>
<ol start="4">
<li><strong>ConcreteProduct</strong></li>
</ol>
<ul>
<li>Defines a product object to be created by the corresponding concrete factory.</li>
</ul>
<ol start="5">
<li><strong>Client</strong></li>
</ol>
<ul>
<li>Uses only interfaces declared by AbstractFactory and AbstractProduct classes.</li>
</ul>
<hr>
<h2 id="%E2%9A%A0%EF%B8%8F-consequences-gof-book-page-91">⚠️ Consequences (GoF Book, Page 91)</h2>
<ul>
<li>✅ It <strong>isolates concrete classes</strong>.</li>
<li>✅ It makes it easy to <strong>exchange product families</strong>.</li>
<li>✅ Promotes <strong>consistency among products</strong>.</li>
<li>❌ It can be <strong>difficult to support new kinds of products</strong> (requires changing the factory interface).</li>
</ul>
<hr>
<h2 id="%F0%9F%94%97-related-patterns-gof-book-page-95">🔗 Related Patterns (GoF Book, Page 95)</h2>
<table>
<thead>
<tr>
<th>Related Pattern</th>
<th>Why Related?</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Factory Method</strong></td>
<td>Abstract Factory is often implemented using Factory Methods.</td>
</tr>
<tr>
<td><strong>Prototype</strong></td>
<td>Sometimes used instead of factories if many product configurations are possible.</td>
</tr>
<tr>
<td><strong>Builder</strong></td>
<td>Used for assembling complex products step by step, but Abstract Factory focuses on families of products.</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="%F0%9F%A7%A0-principles-used-in-abstract-factory-pattern">🧠 Principles Used in <strong>Abstract Factory</strong> Pattern</h2>
<h3 id="open-closed-principle-ocp"><strong>Open-Closed Principle (OCP)</strong></h3>
<ul>
<li><strong>Why it's used:</strong> The Abstract Factory adheres to the <strong>Open-Closed Principle</strong>. You can introduce new families of products by adding new concrete factories, without altering existing client code.</li>
<li><strong>Benefit:</strong> It makes it easy to extend the system with new product families without breaking the existing system or requiring modifications to the client code.</li>
</ul>
<hr>
<h2 id="%E2%9D%93-why-use-the-abstract-factory-pattern">❓ Why Use the Abstract Factory Pattern?</h2>
<p>The <strong>Abstract Factory</strong> pattern is used for several important reasons. It provides a way to create families of related objects without specifying their concrete classes. Below are some key reasons to use the Abstract Factory pattern:</p>
<h3 id="1-ensures-consistency-among-products">1. <strong>Ensures Consistency Among Products</strong></h3>
<ul>
<li>The Abstract Factory ensures that the client can only create objects that belong to the same family, thereby ensuring consistency between related objects.</li>
<li>It helps in enforcing a consistent theme or style, like creating buttons, checkboxes, and other UI components with the same visual appearance across different platforms.</li>
</ul>
<h3 id="2-promotes-loose-coupling">2. <strong>Promotes Loose Coupling</strong></h3>
<ul>
<li>Just like the Factory Method, the Abstract Factory promotes loose coupling between the client code and concrete classes.</li>
<li>The client interacts only with abstract products and factories, never knowing or depending on the specific implementation of the products.</li>
<li>This allows you to change the family of products without affecting the client code.</li>
</ul>
<h3 id="3-eases-product-family-variations">3. <strong>Eases Product Family Variations</strong></h3>
<ul>
<li>The Abstract Factory is ideal when the system should be independent of how its products are created, composed, and represented.</li>
<li>It allows you to create different sets of products (like different themes or platform-specific components) without changing the rest of the system.</li>
<li>You can easily add new families of related products as needed.</li>
</ul>
<h3 id="4-encourages-code-reusability">4. <strong>Encourages Code Reusability</strong></h3>
<ul>
<li>It fosters reusability by centralizing object creation in factory classes, ensuring that product families are created consistently throughout the application.</li>
<li>The pattern allows easy extension to accommodate new product families by adding new concrete factories without modifying existing code.</li>
</ul>
<h3 id="5-facilitates-future-extension">5. <strong>Facilitates Future Extension</strong></h3>
<ul>
<li>Adding new types of products to a system often requires changes in the factory. With the Abstract Factory pattern, new product families can be added without altering existing client code.</li>
<li>This supports the <strong>Open-Closed Principle</strong>, making it easy to extend the system to support new product families.</li>
</ul>
<h3 id="6-encourages-platform-independence">6. <strong>Encourages Platform Independence</strong></h3>
<ul>
<li>In cases where a system needs to support multiple platforms (e.g., different UI toolkits for different operating systems), the Abstract Factory pattern can help abstract platform-specific details and provide platform-independent code for the client.</li>
</ul>
<h3 id="7-simplifies-object-creation-for-complex-families">7. <strong>Simplifies Object Creation for Complex Families</strong></h3>
<ul>
<li>When products within a family have complex relationships or need to be created together, the Abstract Factory provides a structured way to create the entire family without exposing the details to the client.</li>
</ul>
<h3 id="example-scenario">Example Scenario:</h3>
<p>Consider a system that needs to create UI components such as buttons and checkboxes in two different styles: <strong>Light Theme</strong> and <strong>Dark Theme</strong>. The Abstract Factory pattern allows you to create families of related objects (buttons and checkboxes) for both themes, and the client code can interact with abstract product interfaces, keeping it decoupled from the concrete product classes.</p>
<p>By using the Abstract Factory pattern, you ensure that the client always creates products that belong to the same theme (Light or Dark) without having to know the details of how each product is implemented.</p>
<p>Thus, the <strong>Abstract Factory</strong> pattern helps in managing complex product families, ensuring consistency, and promoting flexibility and maintainability in your code.</p>
<h1 id="%F0%9F%9B%A0%EF%B8%8F-factory-method-pattern">🛠️ Factory Method Pattern</h1>
<h2 id="%F0%9F%93%8C-intent">📌 Intent</h2>
<blockquote>
<p><strong>"Define an interface for creating an object, but let subclasses alter the type of objects that will be created."</strong><br>
This pattern allows a class to delegate the responsibility of creating an object to its subclasses, without specifying the exact class of object that will be created.</p>
</blockquote>
<hr>
<h2 id="%F0%9F%A7%BE-also-known-as">🧾 Also Known As</h2>
<ul>
<li><strong>Virtual Constructor</strong></li>
</ul>
<hr>
<h2 id="%F0%9F%93%88-applicability-when-to-use">📈 Applicability (When to Use)</h2>
<p>Use the <strong>Factory Method</strong> pattern when:</p>
<ul>
<li>You need to create objects of a class, but you don’t want to specify the exact class of the object.</li>
<li>You want to let subclasses decide which class to instantiate.</li>
<li>You want to control the instantiation process of a class.</li>
<li>You need to delegate the responsibility of object creation to child classes.</li>
</ul>
<p>Example use cases:</p>
<ul>
<li>When the class cannot anticipate the type of objects it needs to create.</li>
<li>When the client code shouldn't be aware of the specific class types and their instantiation details.</li>
</ul>
<hr>
<h2 id="%F0%9F%91%A5-participants">👥 Participants</h2>
<ol>
<li><strong>Creator (Abstract Class)</strong></li>
</ol>
<ul>
<li>Declares the factory method, which returns an object of type Product.</li>
<li>May also define a default implementation of the factory method.</li>
</ul>
<ol start="2">
<li><strong>ConcreteCreator (Concrete Class)</strong></li>
</ol>
<ul>
<li>Implements the factory method to create a specific product object.</li>
</ul>
<ol start="3">
<li><strong>Product (Abstract Class)</strong></li>
</ol>
<ul>
<li>Declares an interface for objects created by the factory method.</li>
</ul>
<ol start="4">
<li><strong>ConcreteProduct (Concrete Class)</strong></li>
</ol>
<ul>
<li>Implements the <code>Product</code> interface and defines the objects that are created by the factory method.</li>
</ul>
<ol start="5">
<li><strong>Client</strong></li>
</ol>
<ul>
<li>Uses the <code>Creator</code> class and its factory method to create a <code>Product</code>.</li>
</ul>
<hr>
<h2 id="%E2%9A%A0%EF%B8%8F-consequences">⚠️ Consequences</h2>
<p>The <strong>Factory Method</strong> pattern has several advantages and disadvantages:</p>
<h3 id="%E2%9C%85-benefits">✅ Benefits:</h3>
<ul>
<li><strong>Loose coupling</strong>: The client code is decoupled from the concrete class of the object being created.</li>
<li><strong>Subclasses can decide the product type</strong>: The decision of which object to instantiate is delegated to subclasses.</li>
<li><strong>More flexible and extensible</strong>: You can introduce new products without modifying the existing code.</li>
</ul>
<h3 id="%E2%9D%8C-drawbacks">❌ Drawbacks:</h3>
<ul>
<li><strong>Increase in number of classes</strong>: You may end up with more classes, especially when there are several concrete product variants.</li>
<li><strong>Requires a hierarchy of creators</strong>: If there are multiple creators, it can lead to more complex code and hierarchy.</li>
</ul>
<hr>
<h2 id="%F0%9F%94%97-related-patterns">🔗 Related Patterns</h2>
<table>
<thead>
<tr>
<th>Related Pattern</th>
<th>Why Related?</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Abstract Factory</strong></td>
<td>The Abstract Factory pattern uses Factory Methods to create a family of related objects.</td>
</tr>
<tr>
<td><strong>Prototype</strong></td>
<td>If you want to clone an object instead of creating a new one, the Prototype pattern is an alternative.</td>
</tr>
<tr>
<td><strong>Builder</strong></td>
<td>The Builder pattern creates complex objects step-by-step. It's useful when you need to control the creation process in greater detail.</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="%F0%9F%A7%A0-principles-used-in-factory-method-pattern">🧠 Principles Used in <strong>Factory Method</strong> Pattern</h2>
<h3 id="open-closed-principle-ocp"><strong>Open-Closed Principle (OCP)</strong></h3>
<ul>
<li><strong>Why it's used:</strong> The Factory Method follows the <strong>Open-Closed Principle</strong> because the client code can easily add new types of products without modifying existing code. To add a new product, you only need to introduce a new concrete class and extend the factory.</li>
<li><strong>Benefit:</strong> The system can evolve without affecting existing functionality, making it flexible and adaptable to future requirements.</li>
</ul>
<hr>
<h2 id="%E2%9D%93-why-use-the-factory-method-pattern">❓ Why Use the Factory Method Pattern?</h2>
<p>The <strong>Factory Method</strong> pattern is widely used for several reasons:</p>
<h3 id="1-promotes-loose-coupling">1. <strong>Promotes Loose Coupling</strong></h3>
<ul>
<li>The Factory Method separates the creation logic of objects from the client code. This reduces the dependency between client code and concrete product classes.</li>
<li>By using the Factory Method, client code interacts with a product interface, not knowing or caring about the specific class that implements the product.</li>
</ul>
<h3 id="2-enhances-flexibility">2. <strong>Enhances Flexibility</strong></h3>
<ul>
<li>The pattern provides flexibility in terms of the types of objects that can be created. New types of products can be added without modifying the client code or other parts of the application.</li>
<li>You can change the product that’s created by modifying the concrete creator class, without changing the code that uses the product.</li>
</ul>
<h3 id="3-subclasses-decide-the-object-creation">3. <strong>Subclasses Decide the Object Creation</strong></h3>
<ul>
<li>The responsibility of object creation is delegated to subclasses, allowing them to decide which specific class to instantiate. This can be especially useful when the exact type of object to be created is not known until runtime.</li>
</ul>
<h3 id="4-fosters-code-reusability">4. <strong>Fosters Code Reusability</strong></h3>
<ul>
<li>By defining the object creation in a common method, the code can be reused in different parts of the program or across different projects.</li>
<li>If multiple creators share the same method of creating products, they can all use the same base class or interface, promoting code reuse.</li>
</ul>
<h3 id="5-helps-in-code-maintenance">5. <strong>Helps in Code Maintenance</strong></h3>
<ul>
<li>The pattern simplifies code maintenance by isolating the object creation logic from the rest of the code. Changes to how an object is created (e.g., switching from one implementation to another) can be confined to the factory method, avoiding widespread changes across the system.</li>
</ul>
<h3 id="6-encourages-open-closed-principle">6. <strong>Encourages Open-Closed Principle</strong></h3>
<ul>
<li>The Factory Method pattern encourages the open-closed principle, where you can add new product types without changing the existing code. You just need to create a new subclass of the creator and implement the factory method.</li>
</ul>
<h3 id="7-improves-testability">7. <strong>Improves Testability</strong></h3>
<ul>
<li>Since object creation is abstracted, it's easier to mock or substitute real product instances with test doubles or stubs during unit testing. This improves the testability of the application.</li>
</ul>
<h3 id="example-scenario">Example Scenario:</h3>
<p>You are designing a system that needs to work with multiple types of documents (e.g., PDFs, Word files). By using the <strong>Factory Method</strong>, you can define an interface for document creation, allowing subclasses to decide which document type to create without modifying the existing code where documents are processed.</p>
<p>Thus, the <strong>Factory Method</strong> pattern helps in managing complex object creation while keeping your code maintainable, flexible, and scalable.</p>
<hr>
<h1 id="%F0%9F%9B%A0%EF%B8%8F-singleton-pattern-gof">🛠️ <strong>Singleton Pattern</strong> (GoF)</h1>
<h2 id="%F0%9F%93%8C-intent">📌 Intent</h2>
<blockquote>
<p><strong>"Ensure a class has only one instance, and provide a global point of access to it."</strong><br>
(GoF, Design Patterns: Elements of Reusable Object-Oriented Software, Page 127)</p>
</blockquote>
<p>The <strong>Singleton pattern</strong> ensures that a class has only one instance and provides a global access point to that instance. This pattern is often used when a single shared resource (like a configuration manager, logging service, or database connection) needs to be accessed across different parts of an application.</p>
<hr>
<h2 id="%F0%9F%A7%BE-also-known-as">🧾 Also Known As</h2>
<ul>
<li><strong>Single Instance Pattern</strong></li>
<li><strong>One Object Pattern</strong></li>
</ul>
<hr>
<h2 id="%F0%9F%93%88-applicability-when-to-use">📈 Applicability (When to Use)</h2>
<p>Use the Singleton pattern when:</p>
<ul>
<li>You need to control access to a shared resource.</li>
<li>You want to restrict the instantiation of a class to <strong>one instance only</strong>.</li>
<li>You need a global access point to that single instance.</li>
<li>You want to ensure that there is <strong>only one object</strong> managing a resource throughout the application's lifecycle, such as logging or configuration settings.</li>
</ul>
<p>(Reference: GoF, Page 128)</p>
<hr>
<h2 id="%F0%9F%91%A5-participants-gof-book-page-130">👥 Participants (GoF Book, Page 130)</h2>
<ol>
<li><strong>Singleton</strong></li>
</ol>
<ul>
<li>Ensures that only one instance of the class is created and provides a global point of access to that instance.</li>
</ul>
<ol start="2">
<li><strong>Client</strong></li>
</ol>
<ul>
<li>Accesses the singleton instance to interact with the resource managed by the Singleton class.</li>
</ul>
<hr>
<h2 id="%E2%9A%A0%EF%B8%8F-consequences-gof-book-page-131">⚠️ Consequences (GoF Book, Page 131)</h2>
<ul>
<li>✅ <strong>Controlled access to the sole instance</strong>: Ensures that the Singleton class is the only one providing access to the resource.</li>
<li>✅ <strong>Lazy instantiation</strong>: The Singleton object can be created when it's first needed, not necessarily when the program starts.</li>
<li>✅ <strong>Global point of access</strong>: The singleton instance can be accessed from any part of the program.</li>
<li>❌ <strong>Global state</strong>: Having a global point of access to a single instance can lead to tight coupling and difficulty in testing because the Singleton class essentially becomes a global object.</li>
<li>❌ <strong>Single responsibility</strong>: The Singleton pattern can violate the <strong>Single Responsibility Principle (SRP)</strong> if the singleton class handles too much functionality. The global nature of the Singleton may also make the system harder to maintain.</li>
</ul>
<hr>
<h2 id="%F0%9F%94%97-related-patterns-gof-book-page-134">🔗 Related Patterns (GoF Book, Page 134)</h2>
<table>
<thead>
<tr>
<th>Related Pattern</th>
<th>Why Related?</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Factory Method</strong></td>
<td>Singleton can sometimes be combined with Factory Methods to manage the instantiation process of a single instance in a controlled way.</td>
</tr>
<tr>
<td><strong>Abstract Factory</strong></td>
<td>If you need to ensure a single instance of a factory, you can use the Singleton pattern to enforce this behavior.</td>
</tr>
<tr>
<td><strong>Proxy</strong></td>
<td>A Proxy pattern can be used to manage access to the Singleton, especially if access control is needed to ensure only one client at a time interacts with it.</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="%E2%9D%93-why-use-this-pattern">❓ Why Use This Pattern?</h2>
<p>The <strong>Singleton pattern</strong> is used when there is a need to manage shared resources, such as database connections, configuration settings, logging, etc., throughout the application. This ensures that these resources are only initialized once and can be accessed globally.</p>
<h3 id="reasons-for-using-singleton-pattern">Reasons for Using Singleton Pattern:</h3>
<ol>
<li><strong>Global Access</strong>: It provides a global point of access to the unique instance.</li>
<li><strong>Control over Resource Management</strong>: It ensures that only one instance of the resource is ever created, saving system resources and simplifying the code.</li>
<li><strong>Lazy Initialization</strong>: The Singleton instance is created only when it is needed, which can optimize resource usage and application startup time.</li>
</ol>
<hr>
<h2 id="%F0%9F%A7%A0-principles-used-in-singleton-pattern">🧠 Principles Used in Singleton Pattern</h2>
<h3 id="1-single-responsibility-principle-srp">1. <strong>Single Responsibility Principle (SRP)</strong></h3>
<ul>
<li>The Singleton ensures that the responsibility of managing the resource is given to a single class, making it easier to maintain. However, if the Singleton class takes on too many responsibilities, it can violate SRP.</li>