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
> Creational patterns are focused towards how to instantiate an object or group of related objects.
@@ -72,7 +72,7 @@ Wikipedia says
72
72
<b><a href="#">↥ back to top</a></b>
73
73
</div>
74
74
75
-
## Simple Factory Design Pattern
75
+
## # Simple Factory Design Pattern
76
76
77
77
Real world example
78
78
> Consider, you are building a house and you need doors. It would be a mess if every time you need a door, you put on your carpenter clothes and start making a door in your house. Instead you get it made from a factory.
@@ -136,7 +136,7 @@ When creating an object is not just a few assignments and involves some logic, i
136
136
<b><a href="#">↥ back to top</a></b>
137
137
</div>
138
138
139
-
## Factory Method Design Pattern
139
+
## # Factory Method Design Pattern
140
140
141
141
Real world example
142
142
> Consider the case of a hiring manager. It is impossible for one person to interview for each of the positions. Based on the job opening, she has to decide and delegate the interview steps to different people.
@@ -217,7 +217,7 @@ Useful when there is some generic processing in a class but the required sub-cla
217
217
<b><a href="#">↥ back to top</a></b>
218
218
</div>
219
219
220
-
## Abstract Factory Design Pattern
220
+
## # Abstract Factory Design Pattern
221
221
222
222
Real world example
223
223
> Extending our door example from Simple Factory. Based on your needs you might get a wooden door from a wooden door shop, iron door from an iron shop or a PVC door from the relevant shop. Plus you might need a guy with different kind of specialities to fit the door, for example a carpenter for wooden door, welder for iron door etc. As you can see there is a dependency between the doors now, wooden door needs carpenter, iron door needs a welder etc.
@@ -338,7 +338,7 @@ When there are interrelated dependencies with not-that-simple creation logic inv
338
338
<b><a href="#">↥ back to top</a></b>
339
339
</div>
340
340
341
-
## Builder Design Pattern
341
+
## # Builder Design Pattern
342
342
343
343
Real world example
344
344
> Imagine you are at Hardee's and you order a specific deal, lets say, "Big Hardee" and they hand it over to you without *any questions* this is the example of simple factory. But there are cases when the creation logic might involve more steps. For example you want a customized Subway deal, you have several options in how your burger is made e.g what bread do you want? what types of sauces would you like? What cheese would you want? etc. In such cases builder pattern comes to the rescue.
@@ -451,7 +451,7 @@ When there could be several flavors of an object and to avoid the constructor te
451
451
<b><a href="#">↥ back to top</a></b>
452
452
</div>
453
453
454
-
## Singleton Design Pattern
454
+
## # Singleton Design Pattern
455
455
456
456
Real world example
457
457
> There can only be one president of a country at a time. The same president has to be brought to action, whenever duty calls. President here is singleton.
> Structural patterns are mostly concerned with object composition or in other words how the entities can use each other. Or yet another explanation would be, they help in answering "How to build a software component?"
> Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter.
@@ -593,7 +593,7 @@ hunter.hunt(wildDogAdapter)
593
593
<b><a href="#">↥ back to top</a></b>
594
594
</div>
595
595
596
-
## Bridge Design Pattern
596
+
## # Bridge Design Pattern
597
597
598
598
Real world example
599
599
> Consider you have a website with different pages and you are supposed to allow the user to change the theme. What would you do? Create multiple copies of each of the pages for each of the themes or would you just create separate theme and load them based on the user's preferences? Bridge pattern allows you to do the second i.e.
@@ -681,7 +681,7 @@ console.log(careers.getContent() )// "Careers page in Dark Black"
681
681
<b><a href="#">↥ back to top</a></b>
682
682
</div>
683
683
684
-
## Composite Design Pattern
684
+
## # Composite Design Pattern
685
685
686
686
Real world example
687
687
> Every organization is composed of employees. Each of the employees has same features i.e. has a salary, has some responsibilities, may or may not report to someone, may or may not have some subordinates etc.
> How do you turn on the computer? "Hit the power button" you say! That is what you believe because you are using a simple interface that computer provides on the outside, internally it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a facade.
> Did you ever have fresh tea from some stall? They often make more than one cup that you demanded and save the rest for any other customer so to save the resources e.g. gas etc. Flyweight pattern is all about that i.e. sharing.
@@ -1071,7 +1071,7 @@ shop.serve()
1071
1071
<b><a href="#">↥ back to top</a></b>
1072
1072
</div>
1073
1073
1074
-
## Proxy Design Pattern
1074
+
## # Proxy Design Pattern
1075
1075
1076
1076
Real world example
1077
1077
> Have you ever used an access card to go through a door? There are multiple options to open that door i.e. it can be opened either using access card or by pressing a button that bypasses the security. The door's main functionality is to open but there is a proxy added on top of it to add some functionality. Let me better explain it using the code example below.
@@ -1140,7 +1140,7 @@ door.close() // Closing lab door
1140
1140
<b><a href="#">↥ back to top</a></b>
1141
1141
</div>
1142
1142
1143
-
## Behavioral Design Patterns
1143
+
## # Behavioral Design Patterns
1144
1144
1145
1145
In plain words
1146
1146
> It is concerned with assignment of responsibilities between the objects. What makes them different from structural patterns is they don't just specify the structure but also outline the patterns for message passing/communication between them. Or in other words, they assist in answering "How to run a behavior in software component?"
> For example, you have three payment methods (`A`, `B` and `C`) setup in your account each having a different amount in it. `A` has 100 USD, `B` has 300 USD and `C` having 1000 USD and the preference for payments is chosen as `A` then `B` then `C`. You try to purchase something that is worth 210 USD. Using Chain of Responsibility, first of all account `A` will be checked if it can make the purchase, if yes purchase will be made and the chain will be broken. If not, request will move forward to account `B` checking for amount if yes chain will be broken otherwise the request will keep forwarding till it finds the suitable handler. Here `A`, `B` and `C` are links of the chain and the whole phenomenon is Chain of Responsibility.
@@ -1253,7 +1253,7 @@ bank.pay(259)
1253
1253
<b><a href="#">↥ back to top</a></b>
1254
1254
</div>
1255
1255
1256
-
## Command Design Pattern
1256
+
## # Command Design Pattern
1257
1257
1258
1258
Real world example
1259
1259
> A generic example would be you ordering a food at restaurant. You (i.e. `Client`) ask the waiter (i.e. `Invoker`) to bring some food (i.e. `Command`) and waiter simply forwards the request to Chef (i.e. `Receiver`) who has the knowledge of what and how to cook.
@@ -1356,7 +1356,7 @@ Command pattern can also be used to implement a transaction based system. Where
1356
1356
<b><a href="#">↥ back to top</a></b>
1357
1357
</div>
1358
1358
1359
-
## Iterator Design Pattern
1359
+
## # Iterator Design Pattern
1360
1360
1361
1361
Real world example
1362
1362
> An old radio set will be a good example of iterator, where user could start at some channel and then use next or previous buttons to go through the respective channels. Or take an example of MP3 player or a TV set where you could press the next and previous buttons to go through the consecutive channels or in other words they all provide an interface to iterate through the respective channels, songs or radio stations.
@@ -1419,7 +1419,7 @@ stationList.removeStation(new RadioStation(89)) // Will remove station 89
1419
1419
<b><a href="#">↥ back to top</a></b>
1420
1420
</div>
1421
1421
1422
-
## Mediator Design Pattern
1422
+
## # Mediator Design Pattern
1423
1423
1424
1424
Real world example
1425
1425
> A general example would be when you talk to someone on your mobile phone, there is a network provider sitting between you and them and your conversation goes through it instead of being directly sent. In this case network provider is mediator.
@@ -1486,7 +1486,7 @@ jane.send('Hey!')
1486
1486
<b><a href="#">↥ back to top</a></b>
1487
1487
</div>
1488
1488
1489
-
## Memento Design Pattern
1489
+
## # Memento Design Pattern
1490
1490
1491
1491
Real world example
1492
1492
> Take the example of calculator (i.e. originator), where whenever you perform some calculation the last calculation is saved in memory (i.e. memento) so that you can get back to it and maybe get it restored using some action buttons (i.e. caretaker).
@@ -1571,7 +1571,7 @@ console.log(editor.getContent()) // This is the first sentence. This is second.
> Consider someone visiting Dubai. They just need a way (i.e. visa) to enter Dubai. After arrival, they can come and visit any place in Dubai on their own without having to ask for permission or to do some leg work in order to visit any place here just let them know of a place and they can visit it. Visitor pattern let's you do just that, it helps you add places to visit so that they can visit as much as they can without having to do any legwork.
@@ -1747,7 +1747,7 @@ dolphin.accept(jump) // Walked on water a little and disappeared
1747
1747
<b><a href="#">↥ back to top</a></b>
1748
1748
</div>
1749
1749
1750
-
## Strategy Design Pattern
1750
+
## # Strategy Design Pattern
1751
1751
1752
1752
Real world example
1753
1753
> Consider the example of sorting, we implemented bubble sort but the data started to grow and bubble sort started getting very slow. In order to tackle this we implemented Quick sort. But now although the quick sort algorithm was doing better for large datasets, it was very slow for smaller datasets. In order to handle this we implemented a strategy where for small datasets, bubble sort will be used and for larger, quick sort.
> Imagine you are using some drawing application, you choose the paint brush to draw. Now the brush changes it's behavior based on the selected color i.e. if you have chosen red color it will draw in red, if blue then it will be in blue etc.
0 commit comments