tag:blogger.com,1999:blog-39464786812825504542024-03-13T23:54:38.033+05:30Som's Tech World...This is my own world of software and technologies...Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.comBlogger155125tag:blogger.com,1999:blog-3946478681282550454.post-57460655982761913392024-03-01T18:24:00.016+05:302024-03-04T08:36:05.753+05:30C++ 20 - concurrency - stop_token in jthread - politely interrupting the thread...<p> <span style="font-size: large;">When I studied the Android's Asynctask mechanism many years ago, I was fascinated about the implementation of a nice state machine for different stages of an asynchronous task.</span></p><p><span style="font-size: large;">C++ multi threading was a bit raw, in the sense that it was not matured in the initial stage. It seems it is catching up.</span></p><p><span style="font-size: large;">So let's continue - just for fun...</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVRrflHBySC8xddJ5ZwC8KAAQpQUcO9PWMCFBHCP3Xg2R5SvT7wUXZUzp7wQhi07tnb_1RnpUTOV4-bw498BAvhKdESz576lSst0AZYvVQBIcjJBBrEG1kZOJmlNVmAto7Nr8CCiEKBatYNb64_lIoSevbcvmQvBO7BLvfngOZLPZjnEy5OU-79sY6A3A/s640/just%20for%20fun.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="480" data-original-width="640" height="480" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVRrflHBySC8xddJ5ZwC8KAAQpQUcO9PWMCFBHCP3Xg2R5SvT7wUXZUzp7wQhi07tnb_1RnpUTOV4-bw498BAvhKdESz576lSst0AZYvVQBIcjJBBrEG1kZOJmlNVmAto7Nr8CCiEKBatYNb64_lIoSevbcvmQvBO7BLvfngOZLPZjnEy5OU-79sY6A3A/w640-h480/just%20for%20fun.png" width="640" /></a></div><br /><span style="font-size: large;"><br /></span><p></p><p><span style="font-size: large;">A thread cannot be preempted while in the critical section. But what about in case the thread is not inside a critical section and the thread function in the background is doing a heavy duty task like downloading a large video files from a remote server which you want to cancel from the UI (the reason for such a system is when the network is slow, the download may take a very long time and hence you want to cancel it midway). </span></p><p><span style="font-size: large;">In C++ 20, the stop_token header which has introduced a mechanism by which any thread (obviously not in the critical section) can be interrupted and made exit the thread function. </span><span style="font-size: large;">The stop_token header is a nice way of implementing the co-operative thread preemption and cancelling an asynctask midway.</span></p><p><span style="font-size: large;">Here's a brief description of it...</span></p><h3 style="text-align: left;"><span style="font-size: large;">std::stop_token:</span></h3><ul data-sourcepos="7:1-10:0" style="background-color: white; color: #1f1f1f; margin: 4px 0px; padding-inline-start: 1.125rem;"><li data-sourcepos="7:1-7:60" style="margin-bottom: 0px;"><span style="font-family: inherit; font-size: large;">Represents the cancellation state of an asynchronous task.</span></li><li data-sourcepos="8:1-8:91" style="margin-bottom: 0px;"><span style="font-family: inherit; font-size: large;">Doesn't directly control the task, but indicates if a cancellation request has been made.</span></li><li data-sourcepos="9:1-10:0" style="margin-bottom: 0px;"><span style="font-family: inherit; font-size: large;">Used to check if cancellation is requested within the running task</span><span face="Google Sans, Helvetica Neue, sans-serif">.</span></li></ul><h3 style="text-align: left;"><span style="font-size: large;">std::stop_callback</span></h3><ul data-sourcepos="19:1-22:0" style="background-color: white; color: #1f1f1f; margin: 4px 0px; padding-inline-start: 1.125rem;"><li data-sourcepos="19:1-19:63" style="margin-bottom: 0px;"><span style="font-family: inherit; font-size: large;">An optional callback function registered with a <code style="background-color: var(--bard-color-surface-container-high); border-radius: 6px; color: var(--bard-color-on-surface-variant); line-height: 1.25rem; padding: 1px 6px;">stop_token</code>.</span></li><li data-sourcepos="20:1-20:46" style="margin-bottom: 0px;"><span style="font-family: inherit; font-size: large;">Invoked when the associated task is stopped.</span></li></ul><div><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div><span style="color: #1f1f1f; font-size: large;">Here in the example, the main thread goes to sleep for certain time. In that time frame, the worker thread continues to do the background task.</span></div><div><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div><span style="color: #1f1f1f; font-size: large;">But after the main thread awakes from the sleeping mode, it requests the worker thread to stop and exit.</span></div><div><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div><span style="color: #1f1f1f; font-size: large;">Here we go with the source code</span></div><div><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div><span style="color: #1f1f1f; font-size: large;">The thread function is written as</span></div><div style="background-color: white; padding: 0px 0px 0px 2px;"><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;"><br /></span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">void</span> <span style="font-weight: bold;">printIncrementingValues</span>(<span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">std::</span><span style="background-color: #d4d4d4; text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">stop_token</span> stopToken) {</span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">while</span> (!stopToken.<span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">stop_requested</span>()) {</span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: medium;"> std::cout << <span style="color: #0000c0;">startNumber</span>++ << <span style="color: #2a00ff;">" "</span> << std::<span style="color: #642880; font-weight: bold;">flush</span>;</span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: medium;"> std::this_thread::sleep_for(500ms);</span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: medium;"> }</span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: medium;"> std::cout << std::<span style="color: #642880; font-weight: bold;">endl</span>;</span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;"><b><br /></b></span></span></p><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;"></span></span></p><p class="graf graf--p" name="7b8a"><span style="font-family: inherit; font-size: large;">Look at the code line in the above code block.</span></p><p class="graf graf--p" name="7b8a"><span style="font-family: monospace; font-size: large; white-space: pre;"> </span><b><span style="color: #7f0055; font-family: monospace; font-size: large; white-space: pre;">while</span><span style="font-family: monospace; font-size: large; white-space: pre;"> (!stopToken.</span><span style="font-family: monospace; font-size: large; text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy; white-space: pre;">stop_requested</span><span style="font-family: monospace; font-size: large; white-space: pre;">())</span></b></p><p class="graf graf--p" name="7b8a"><span style="font-family: inherit; font-size: large;">This line is the soul of the whole concept as this keeps listening to the event when the stop_request is issued.</span></p><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;">The complete source code is here.</span></span></p><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;"><br /></span></span></p><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;"></span></span></p><div style="padding: 0px 0px 0px 2px;"><div style="font-family: monospace; white-space: pre;"><p style="font-size: 10pt; margin: 0px;"><span style="color: #3f7f5f;">/*</span></p><p style="font-size: 10pt; margin: 0px;"><span style="color: #3f7f5f;"> * Worker.h</span></p><p style="font-size: 10pt; margin: 0px;"><span style="color: #3f7f5f;"> *</span></p><p style="font-size: 10pt; margin: 0px;"><span style="color: #3f7f5f;"> * Created on: Mar 2, 2024</span></p><p style="font-size: 10pt; margin: 0px;"><span style="color: #3f7f5f;"> * Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></p><p style="font-size: 10pt; margin: 0px;"><span style="color: #3f7f5f;"> */</span></p><p style="font-size: 10pt; margin: 0px;"><br /></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#ifndef</span> WORKER_H_</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#define</span> WORKER_H_</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><iostream></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><stop_token></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">using</span> <span style="color: #7f0055; font-weight: bold;">namespace</span> std::literals::chrono_literals;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">using</span> <span style="color: #7f0055; font-weight: bold;">namespace</span> std;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">class</span> <span style="color: #005032;">Worker</span> {</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">private</span>:</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">int</span> <span style="color: #0000c0;">startNumber</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">public</span>:</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="font-weight: bold;">Worker</span>(<span style="color: #7f0055; font-weight: bold;">int</span> start){</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #0000c0;">startNumber</span> = start;</span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">virtual</span> <span style="font-weight: bold;">~Worker</span>(){</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">void</span> <span style="font-weight: bold;">printIncrementingValues</span>(<span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">std::</span><span style="background-color: #d4d4d4; text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">stop_token</span> stopToken) {</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">while</span> (!stopToken.<span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">stop_requested</span>()) {</span></p><p style="margin: 0px;"><span style="font-size: medium;"> std::cout << <span style="color: #0000c0;">startNumber</span>++ << <span style="color: #2a00ff;">" "</span> << std::<span style="color: #642880; font-weight: bold;">flush</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"> std::this_thread::sleep_for(500ms);</span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;"> std::cout << std::<span style="color: #642880; font-weight: bold;">endl</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;">};</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#endif</span> <span style="color: #3f7f5f;">/* WORKER_H_ */</span></span></p><p style="margin: 0px;"></p></div></div><p style="margin: 0px;"><span style="font-family: monospace;"><span style="font-size: medium; white-space: pre;"><br /></span></span></p><div style="padding: 0px 0px 0px 2px;"><div style="font-family: monospace; white-space: pre;"><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><div style="padding: 0px 0px 0px 2px;"><div style="font-family: monospace;"><p style="margin: 0px;"><span style="color: #3f7f5f;"><span style="font-size: medium;">//============================================================================</span></span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"><span style="font-size: medium;">// Name : advanced_stop_token.cpp</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #3f7f5f;">// Author : </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"><span style="font-size: medium;">// Version :</span></span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"><span style="font-size: medium;">// Copyright : som-itsolutions</span></span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"><span style="font-size: medium;">// Description : Hello World in C++, Ansi-style</span></span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"><span style="font-size: medium;">//============================================================================</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><iostream></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><stop_token></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;">"Worker.h"</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">int</span> <span style="font-weight: bold;">main</span>() {</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #005032;">Worker</span> worker(1);</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">std::stop_source</span> stopSource;</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #3f7f5f;">// creating stop_token object</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">std::stop_token</span> stopToken = stopSource.<span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">get_token</span>();</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">std::jthread</span> ridit(&<span style="color: #005032;">Worker</span>::printIncrementingValues, &worker, stopToken);</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #3f7f5f;">// Register a callback</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">std::stop_callback</span> callback(stopToken, []() {</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> std::cout << std::<span style="color: #642880; font-weight: bold;">endl</span><<<span style="color: #2a00ff;">"Callback executed"</span> << std::<span style="color: #642880; font-weight: bold;">endl</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> });</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> std::this_thread::sleep_for(10s);</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> stopSource.<span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">request_stop</span>();</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">return</span> 0;</span></p><p style="margin: 0px;"><span style="font-size: medium;">}</span></p><p style="font-size: 10pt; margin: 0px;"></p></div></div><p style="font-size: 10pt; margin: 0px;"></p></div></div><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;"><b><br /></b></span></span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: large;">And if we run this program, it will show the following </span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: large;">result.</span></p><p style="font-family: monospace; margin: 0px; white-space: pre;"><span style="font-size: large;"><br /></span></p><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;"><b>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Callback executed</b></span></span></p><p style="margin: 0px;"><span style="font-family: monospace; font-size: large;"><span style="white-space: pre;"><b><br /></b></span></span></p><p style="margin: 0px;"><span style="font-size: large;"><span style="white-space: pre;">As the main thread is in the sleep mode for 10 seconds and the </span></span></p><p style="margin: 0px;"><span style="font-size: large;"><span style="white-space: pre;">interval for </span></span><span style="font-size: large; white-space: pre;">printing the numbers is set to 500 ms, hence in the </span></p><p style="margin: 0px;"><span style="font-size: large;"><span style="white-space: pre;">output there will be </span><span style="white-space: pre;">1 to 20.</span></span></p><p style="margin: 0px;"><span style="font-size: large;"><span style="white-space: pre;"><br /></span></span></p><p style="margin: 0px;"><span style="font-size: large;"><span style="white-space: pre;">The new C++ is changing very fast - keeping yourself abreast</span></span></p><p style="margin: 0px;"><span style="font-size: large;"><span style="white-space: pre;">with the </span></span><span style="font-size: large; white-space: pre;">changes is of utmost importance for the engineers who</span></p><p style="margin: 0px;"><span style="font-size: large;"><span style="white-space: pre;">are using modern </span><span style="white-space: pre;">C++. </span></span></p><div style="font-family: monospace; white-space: pre;"><span style="font-size: large;"><br /></span></div></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-71351348210230488312024-02-16T12:08:00.018+05:302024-03-05T07:24:34.679+05:30My C++ exploration - concurrency - async and future - driven by the Intrinsic Motivation...<div><span style="font-family: inherit; font-size: large;">My whole professional life in the world of software was driven by Intrinsic Motivation.</span></div><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit; font-size: large;">Intrinsic motivation is the act of doing something without any obvious external rewards. You do it because it's enjoyable and interesting, rather than because of an outside incentive or pressure to do it, such as a reward or deadline.</span></div><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit; font-size: large;">An example of intrinsic motivation for a software engineer would be deciphering a piece of a framework code because you enjoy doing it and have an interest in the matter or subject, rather than doing it because you have to prove a point to others or pass an exam.</span></div><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit; font-size: large;">Intrinsic motivation comes from within, while extrinsic motivation arises from outside. When you’re intrinsically motivated, you engage in an activity solely because you enjoy it and get personal satisfaction from it.</span></div><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit; font-size: large;">Let's come back to the nitty gritty of C++.</span></div><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit; font-size: large;">Here's my exploration of the C++ async method from the future header.</span></div>
<br />
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/5QEGOv0Kc08?si=DPSVwnvq_2lSNN3w" title="YouTube video player" width="560"></iframe><p><span style="font-family: inherit; font-size: large;">C++ async is a function template from the <future> header that helps you execute functions asynchronously, potentially in separate threads. It returns a std::future object that you can use to track the progress and retrieve the result of the asynchronous task.</span></p><h2 style="text-align: left;"><span style="font-family: inherit; font-size: large;">Key Concepts:</span></h2><p><span style="font-family: inherit; font-size: large;">Asynchronous Execution: async launches the provided function in a different thread (or using other mechanisms) without blocking the calling thread. This allows your program to continue doing other work while the asynchronous task is running.</span></p><p><span style="font-family: inherit; font-size: large;">std::future: The std::future object returned by async serves as a placeholder for the result of the asynchronous task. You can use methods like get(), wait(), valid(), and ready() to manage and access the result.</span></p><h2 style="text-align: left;"><span style="font-family: inherit; font-size: large;">Common Use Cases:</span></h2><p><span style="font-family: inherit; font-size: large;">Performing I/O-bound operations (e.g., network requests, file reading/writing) without blocking the main thread.</span></p><h2 style="text-align: left;"><span style="font-family: inherit; font-size: large;">Source Code:</span></h2><div><span style="font-family: inherit; font-size: large;"><div style="background-color: white; padding: 0px 0px 0px 2px;"><div style="font-family: monospace; font-size: 10pt; font-weight: bold; white-space: pre;"><p style="margin: 0px;"><span style="color: #3f7f5f;">/*</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #3f7f5f;">* Callback.h</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #3f7f5f;">*</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #3f7f5f;">* Created on: 28-</span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Dec</span><span style="color: #3f7f5f;">-2023</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #3f7f5f;">* Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">ridit</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #3f7f5f;">*/</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055;">#ifndef</span> CALLBACK_H_</p><p style="margin: 0px;"><span style="color: #7f0055;">#define</span> CALLBACK_H_</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; text-decoration-color: rgb(255, 255, 0); text-decoration-line: underline; text-decoration-style: wavy;">class</span><span style="text-decoration-color: rgb(255, 255, 0); text-decoration-line: underline; text-decoration-style: wavy;"> </span><span style="background-color: #d4d4d4; color: #005032; text-decoration-color: rgb(255, 255, 0); text-decoration-line: underline; text-decoration-style: wavy;">CallBack</span><span style="text-decoration-color: rgb(255, 255, 0); text-decoration-line: underline; text-decoration-style: wavy;"> {</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055;">public</span>:</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> <span style="color: #7f0055;">virtual</span> <span style="color: #7f0055;">void</span> onStartTask() = 0;</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> <span style="color: #7f0055;">virtual</span> <span style="color: #7f0055;">void</span> onFinishTask() = 0;</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;">};</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055;">#endif</span> <span style="color: #3f7f5f;">/* CALLBACK_H_ */</span></p><p style="margin: 0px;"></p></div></div></span></div><p><span style="font-family: inherit; font-size: large;"><br /></span></p><p><span style="font-family: inherit; font-size: large;"></span></p><div style="background-color: white; padding: 0px 0px 0px 2px;"><div style="white-space: pre;"><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">/*</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> * Caller.h</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> *</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> * Created on: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Feb</span><span style="color: #3f7f5f;"> 15, 2024</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> * Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> */</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#ifndef</span> CALLER_H_</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#define</span> CALLER_H_</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;"><iostream></span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;">"Callee.h"</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">using</span> <span style="color: #7f0055;">namespace</span> std;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">//forward declaration</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">class</span> <span style="color: #005032;">Callee</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">class</span> <span style="color: #005032;">Caller</span> : <span style="color: #7f0055;">public</span> <span style="color: #005032;">CallBack</span>{</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">private</span>:</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #005032;">Callee</span>* <span style="color: #0000c0;">callee</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">public</span>:</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> Caller(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span> </span><span style="color: #0000c0;">callee</span> = <span style="color: #7f0055;">new</span> <span style="color: #005032;">Callee</span>(<span style="color: #7f0055;">this</span>);</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">void</span> doOwnTask(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span> </span>cout<<<span style="color: #2a00ff;">"Main thread id = "</span> <<this_thread::get_id()<<<span style="color: #642880;">endl</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span> </span><span style="color: #7f0055;">for</span>(<span style="color: #7f0055;">int</span> i = 0;i<10;i++){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> cout<<<span style="color: #2a00ff;">"Caller is doing its own task in the main thread..."</span><<<span style="color: #642880;">endl</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">void</span> delegateTaskToCallee(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #0000c0;">callee</span>->doBackgroundTask();</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">virtual</span> ~Caller(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">void</span> onStartTask(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> cout<<<span style="color: #2a00ff;">"The background task is starting"</span><<<span style="color: #642880;">endl</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">void</span> onFinishTask(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> cout<<<span style="color: #2a00ff;">"The background task is finished. Thank you, </span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Callee</span><span style="color: #2a00ff;">, for taking my burden"</span><<<span style="color: #642880;">endl</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;">};</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#endif</span> <span style="color: #3f7f5f;">/* CALLER_H_ */</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><div style="padding: 0px 0px 0px 2px;"><div><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">/*</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> * Callee.h</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> *</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> * Created on: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Feb</span><span style="color: #3f7f5f;"> 15, 2024</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> * Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"> */</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#ifndef</span> CALLEE_H_</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#define</span> CALLEE_H_</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;"><iostream></span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;"><future></span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;">"CallBack.h"</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">using</span> <span style="color: #7f0055;">namespace</span> std;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">class</span> <span style="color: #005032;">Callee</span> {</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">private</span>:</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #005032;">CallBack</span>* <span style="color: #0000c0;">cb</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">public</span>:</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> Callee(<span style="color: #005032;">CallBack</span>* cb){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">this</span>-><span style="color: #0000c0;">cb</span> = cb;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">virtual</span> ~Callee(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">static</span> <span style="color: #7f0055;">bool</span> task(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> cout<<<span style="color: #2a00ff;">"Background thread id = "</span> <<this_thread::get_id()<<<span style="color: #642880;">endl</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">for</span>(<span style="color: #7f0055;">int</span> i = 0;i<10;i++){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span> </span>cout<<<span style="color: #2a00ff;">"</span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Callee</span><span style="color: #2a00ff;"> is doing the background task"</span><<<span style="color: #642880;">endl</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">return</span> <span style="color: #7f0055;">true</span>;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">void</span> doBackgroundTask(){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">this</span>-><span style="color: #0000c0;">cb</span>->onStartTask();</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #005032;">future</span><<span style="color: #7f0055;">bool</span>> futureTask = async(<span style="color: #005032;">launch</span>::<span style="background-color: #d4d4d4; color: #0000c0; font-style: italic;">async</span>, <span style="color: #005032;">Callee</span>::<span style="font-style: italic;">task</span>);</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">if</span>(futureTask.get() == <span style="color: #7f0055;">true</span>){</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span> </span><span style="color: #7f0055;">this</span>-><span style="color: #0000c0;">cb</span>->onFinishTask();</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span> </span>}</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> }</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;">};</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#endif</span> <span style="color: #3f7f5f;">/* CALLEE_H_ */</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"><br /></span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"><br /></span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;"><br /></span></p><div style="padding: 0px 0px 0px 2px;"><div><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">//============================================================================</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">// Name : Callback.cpp</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">// Author : </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Som</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">// Version :</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">// Copyright : som-itsolutions</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">// Description : Hello World in C++, Ansi-style</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #3f7f5f;">//============================================================================</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;"><iostream></span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;">"CallBack.h"</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;">"Caller.h"</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">#include</span> <span style="color: #2a00ff;">"Callee.h"</span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">using</span> <span style="color: #7f0055;">namespace</span> std;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><span style="color: #7f0055;">int</span> main() {</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #005032;">Caller</span>* caller = <span style="color: #7f0055;">new</span> <span style="color: #005032;">Caller</span>();</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> caller->doOwnTask();</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> caller->delegateTaskToCallee();</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"> <span style="color: #7f0055;">return</span> 0;</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;">}</p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"><br /></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;">You may like my other research study vis-a-vis how the</span></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;">evenlistener pattern has been implemented in Android UI input </span></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;">events.</span></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;"><br /></span></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;">Here we go...</span></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;"><br /></span></p><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"></p></div></div><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"></p></div></div><p style="font-family: monospace; font-size: 10pt; font-weight: bold; margin: 0px;"></p></div></div>
<iframe height="700" src="https://som-itsolutions.blogspot.com/2017/01/blog-post_24.html" width="750"></iframe>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-2592253968771900172024-02-13T10:11:00.004+05:302024-02-14T07:49:53.403+05:30Are we heading for a doomsday - HAARP - the earthquake weapon of USA...<p> <span style="font-family: inherit; font-size: x-large;">HAARP, which stands for High-frequency Active Auroral Research Program, is a research facility located in Gakona, Alaska. Its primary purpose is to study the ionosphere, the uppermost layer of Earth's atmosphere. The ionosphere is ionized by radiation from the Sun, and it plays an important role in radio communications and navigation.</span></p><div style="text-align: left;"><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit; font-size: large;">HAARP uses a powerful radio transmitter to send beams of radio waves into the ionosphere. These beams can cause changes in the ionosphere, which can be studied by other instruments at the facility. This research helps scientists to understand how the ionosphere works and how it can be affected by natural and man-made phenomena.</span></div><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit; font-size: large;">Now the crux...</span></div></div><h1 style="text-align: left;">Conspiracy theories !!! Universe you decide...</h1>
<br />
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/mh8oO27X54c?si=dpUVH_VuPRRI3gMG" title="YouTube video player" width="560"></iframe>
<div><br /></div><h2 style="text-align: left;">Here's another video - technical</h2>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/5WdLV3YRMvw?si=t4OEylpYDGrrqw8v" title="YouTube video player" width="560"></iframe>
<div><br /></div><h2 style="text-align: left;"><br /></h2><h2 style="text-align: left;">READ... READ...</h2><div><br /></div>
<br />
<iframe height="700" src="https://docs.google.com/document/d/e/2PACX-1vR0KuaDZbyrVKrtqGs6w1IcG0KPIRfcSV810yS9GF3atfmG3-5U_94s_CKWjxt0hzcZBQMokkpwyjGh/pub?embedded=true" width="700"></iframe>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-67634081689412420572024-02-04T15:10:00.003+05:302024-03-04T08:37:07.114+05:30jthread in C++ 20 concurrency - a wrapper around std::thread...<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-F904NPKTD5"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-F904NPKTD5');
</script>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/Lyst6759wAs?si=eoKPiEpf8SfjgeWI" title="YouTube video player" width="560"></iframe>
<p><span style="font-family: inherit; font-size: x-large;">jthread is a new entrant in C++ 20. It's a simple wrapper around std::thread which is based upon RAII objective - that is resource acquisition is resource initialization.</span></p><p><span style="font-family: inherit; font-size: large;">The j in the jthread stands for automatic joining.</span></p><p><span style="font-family: inherit; font-size: large;">This means that there is no need to call join on the newly created thread object - the program won't terminate abnormally.</span></p><p><span style="font-family: inherit; font-size: large;"><span style="background-color: white; color: #1f1f1f;">When a jthread object goes out of scope or is otherwise destroyed,</span><span style="background-color: white; color: #1f1f1f;"> </span><span style="background-color: white; color: #1f1f1f;">it automatically calls</span><span style="background-color: white; color: #1f1f1f;"> </span><code style="border-radius: 6px; color: var(--bard-color-on-surface-variant); line-height: 1.25rem; padding: 1px 6px;">join()</code><span style="background-color: white; color: #1f1f1f;"> </span><span style="background-color: white; color: #1f1f1f;">to ensure the thread completes before the object is destroyed.</span><span style="background-color: white; color: #1f1f1f;"> </span><span style="background-color: white; color: #1f1f1f;">This prevents potential resource leaks or undefined behavior that could occur with</span><span style="background-color: white; color: #1f1f1f;"> </span><code style="border-radius: 6px; color: var(--bard-color-on-surface-variant); line-height: 1.25rem; padding: 1px 6px;">std::thread.</code></span></p><p><span style="color: #1f1f1f; font-size: large;">The other aspect of jthread is the cooperative interruption.</span></p><p><span style="color: #1f1f1f; font-size: large;">We will discuss it in the next post.</span></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-31461091725887782782024-01-20T14:46:00.005+05:302024-03-04T08:37:28.864+05:30The Barrier in C++ 20 concurrency - the programmer in me is still thriving...<p><span style="font-size: large;">Enjoy my training video on the C++ barrier...</span></p>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/P0_f9xnLRTc?si=kRjvJZJOUX9BAxfB" title="YouTube video player" width="560"></iframe>
<p><br /></p><p><span style="font-size: large;">The std::barrier class is a synchronization primitive introduced in C++20. It allows a set of threads to synchronize at a certain point in their execution. It is similar to the std::latch class, but it can be reused multiple times.</span></p><p><span style="font-size: large;">A std::barrier object is initialized with a count, which specifies the number of threads that must reach the barrier before any of them can proceed. When a thread reaches the barrier, it calls the wait() method. If the count is not yet zero, the thread will be blocked until the count reaches zero. Once the count reaches zero, all of the threads that are waiting on the barrier will be released and can proceed.</span></p><p><span style="font-size: large;">The std::barrier class can be used to implement a variety of synchronization patterns, such as producer-consumer queues, parallel algorithms, and race condition prevention.</span></p><p><span style="font-size: large;">Here's my application in which I used barrier to showcase how it can be used.</span></p><h3 style="text-align: left;"><span style="font-size: large;">Class Student</span></h3><div style="background-color: white; padding: 0px 0px 0px 2px;"><div style="font-family: monospace; font-size: 10pt; white-space: pre;"><p style="margin: 0px;"><span style="color: #3f7f5f;">/*</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> * Student.h</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> *</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> * Created on: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Jan</span><span style="color: #3f7f5f;"> 10, 2024</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> * Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> */</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#ifndef</span> STUDENT_H_</p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#define</span> STUDENT_H_</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><iostream></span></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><string></span></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><barrier></span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">using</span> <span style="color: #7f0055; font-weight: bold;">namespace</span> std;</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">class</span> <span style="color: #005032;">Student</span> {</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">private</span>:</p><p style="margin: 0px;"> <span style="color: #005032;">string</span> <span style="color: #0000c0;">name</span>;</p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">int</span> <span style="color: #0000c0;">timeLapseBeforeStarting</span>;</p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">int</span> <span style="color: #0000c0;">timeToFinish</span>;</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">public</span>:</p><p style="margin: 0px;"> <span style="font-weight: bold; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">Student</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">(</span><span style="color: #005032; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">string</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;"> name, </span><span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">int</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;"> timeLapseBeforeStarting, </span><span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">int</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;"> timeToFinish){</span></p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">name</span> = name;</p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">timeLapseBeforeStarting</span> = timeLapseBeforeStarting;</p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">timeToFinish</span> = timeToFinish;</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> }</p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">virtual</span> <span style="font-weight: bold;">~Student</span>(){</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> }</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">void</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;"> </span><span style="font-weight: bold; text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">task</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">(</span><span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">barrier</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;"><> &b){</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> this_thread::sleep_for(chrono::<span style="color: #005032;">milliseconds</span>(<span style="color: #0000c0;">timeLapseBeforeStarting</span>));</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> <span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">b.arrive_and_wait();</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">const</span> <span style="color: #7f0055; font-weight: bold;">auto</span> now = std::chrono::<span style="color: #005032;">system_clock</span>::<span style="color: #642880; font-weight: bold;">now</span>();</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">const</span> std::<span style="color: #005032;">time_t</span> t_c = std::chrono::<span style="color: #005032;">system_clock</span>::<span style="font-style: italic;">to_time_t</span>(now);</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> cout<< <span style="color: #7f0055; font-weight: bold;">this</span>-> <span style="color: #0000c0;">name</span> << <span style="color: #2a00ff;">" is Starting the task at "</span> << std::<span style="color: #642880; font-weight: bold;">ctime</span>(&t_c);</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> this_thread::sleep_for(chrono::<span style="color: #005032;">milliseconds</span>(<span style="color: #0000c0;">timeToFinish</span>));</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> cout<< <span style="color: #7f0055; font-weight: bold;">this</span>-> <span style="color: #0000c0;">name</span> <<<span style="color: #2a00ff;">" finished the task"</span><<<span style="color: #642880; font-weight: bold;">endl</span>;</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> }</p><p style="margin: 0px;">};</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#endif</span> <span style="color: #3f7f5f;">/* STUDENT_H_ */</span></p><p style="margin: 0px;"></p></div></div><p><br /></p><h3 style="text-align: left;"><span style="font-size: large;">Class classETC</span></h3><div><div style="background-color: white; padding: 0px 0px 0px 2px;"><div style="font-family: monospace; font-size: 10pt; white-space: pre;"><p style="margin: 0px;"><span style="color: #3f7f5f;">/*</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> * classETC.h</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> *</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> * Created on: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Jan</span><span style="color: #3f7f5f;"> 10, 2024</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> * Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></p><p style="margin: 0px;"><span style="color: #3f7f5f;"> */</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#ifndef</span> CLASSETC_H_</p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#define</span> CLASSETC_H_</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><barrier></span></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;">"Student.h"</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">using</span> <span style="color: #7f0055; font-weight: bold;">namespace</span> std;</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">class</span> <span style="color: #005032;">classETC</span> {</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="font-size: 10pt;"> </span></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">public</span>:</p><p style="margin: 0px;"> <span style="font-weight: bold; text-decoration-color: rgb(255, 255, 0); text-decoration-line: underline; text-decoration-style: wavy;">classETC</span><span style="text-decoration-color: rgb(255, 255, 0); text-decoration-line: underline; text-decoration-style: wavy;">(){</span></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="text-decoration-color: rgb(255, 255, 0); text-decoration-line: underline; text-decoration-style: wavy;"> }</span></p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold;">virtual</span> <span style="font-weight: bold;">~classETC</span>(){</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> }</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> <span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">auto</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;"> </span><span style="font-weight: bold; text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">giveTaskToStudent</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">(</span><span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">barrier</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;"><> &b){</span></p><p style="margin: 0px;"> <span style="color: #005032;">thread</span> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">riditT</span>(&<span style="color: #005032;">Student</span>::task, <span style="color: #7f0055; font-weight: bold;">new</span> <span style="color: #005032;">Student</span> (<span style="color: #2a00ff;">"</span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Ridit</span><span style="color: #2a00ff;">"</span>, 1000,2000), <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ref</span>(b));</p><p style="margin: 0px;"> <span style="color: #005032;">thread</span> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ishanT</span>(&<span style="color: #005032;">Student</span>::task, <span style="color: #7f0055; font-weight: bold;">new</span> <span style="color: #005032;">Student</span>(<span style="color: #2a00ff;">"</span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Ishan</span><span style="color: #2a00ff;">"</span>, 3000, 1000), <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ref</span>(b));</p><p style="margin: 0px;"> <span style="color: #005032;">thread</span> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">rajdeepT</span>(&<span style="color: #005032;">Student</span>::task, <span style="color: #7f0055; font-weight: bold;">new</span> <span style="color: #005032;">Student</span>(<span style="color: #2a00ff;">"</span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Rajdeep</span><span style="color: #2a00ff;">"</span>, 900, 1500), <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ref</span>(b));</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"> riditT.<span style="color: #642880; font-weight: bold;">join</span>();</p><p style="margin: 0px;"> ishanT.<span style="color: #642880; font-weight: bold;">join</span>();</p><p style="margin: 0px;"> rajdeepT.<span style="color: #642880; font-weight: bold;">join</span>();</p><p style="margin: 0px;"> }</p><p style="margin: 0px;">};</p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><br /></p><p style="margin: 0px;"><span style="color: #7f0055; font-weight: bold;">#endif</span> <span style="color: #3f7f5f;">/* CLASSETC_H_ */</span></p><p style="margin: 0px;"></p></div></div></div><div><br /></div><div><br /></div><h3 style="text-align: left;"><span style="font-size: large;">The Main method</span></h3><div><span style="font-size: large;"><br /></span></div><div><div style="background-color: white; padding: 0px 0px 0px 2px;"><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">//============================================================================</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">// Name : BarrierC++.</span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">cpp</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">// Author : </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">// Version :</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">// Copyright : som-itsolutions</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">// Description : Hello World in C++, Ansi-style</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">//============================================================================</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #3f7f5f;">// C++ Program to demonstrate use of use </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">std</span><span style="color: #3f7f5f;">::barrier in</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><iostream></span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><barrier></span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;">"classETC.h"</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #7f0055; font-weight: bold;">using</span> <span style="color: #7f0055; font-weight: bold;">namespace</span> std;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><span style="color: #7f0055; font-weight: bold;">int</span> <span style="font-weight: bold;">main</span>() {</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> cout << <span style="color: #2a00ff;">"C++ Version"</span> << <span style="color: #642880; font-weight: bold;">endl</span>; <span style="color: #3f7f5f;">// prints C++ Version</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 202101L)</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> cout << <span style="color: #2a00ff;">"C++23"</span>;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span>(__cplusplus == 202002L)</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> cout << <span style="color: #2a00ff;">"C++20\n"</span>;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 201703L)</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> cout << <span style="color: #2a00ff;">"C++17\n"</span>;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 201402L)</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> cout << <span style="color: #2a00ff;">"C++14\n"</span>;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 201103L)</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> cout << <span style="color: #2a00ff;">"C++11\n"</span>;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 199711L)</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> cout << <span style="color: #2a00ff;">"C++98\n"</span>;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">else</span> cout << <span style="color: #2a00ff;">"pre-standard C++\n"</span>;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">barrier b ( 3);</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #005032;">classETC</span> etc;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">etc.giveTaskToStudent(b);</span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"> <span style="color: #7f0055; font-weight: bold;">return</span> 0;</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;">}</p><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p><h1 style="font-family: monospace; margin: 0px; text-align: left; white-space: pre;"><span style="font-size: x-large;">The Output:</span></h1><div style="font-family: monospace; font-size: 10pt; white-space: pre;"><br /></div><div><span style="font-size: large;"><span style="font-family: monospace;"><span style="white-space: pre;">C++ Version
C++20
Ishan is Starting the task at RiditRajdeep is Starting the task at is Starting the task at Sat Jan 20 17:41:30 2024
Sat Jan 20 17:41:30 2024
Sat Jan 20 17:41:30 2024
Ishan finished the task
Rajdeep finished the task
Ridit finished the task</span></span>
</span></div><div style="font-family: monospace; white-space: pre;"><span style="font-size: large;"><br /></span></div><div style="font-family: monospace; white-space: pre;"><span style="font-size: large;">Have a look at the time when the three different threads are starting - all of them start at the</span></div><div style="font-family: monospace; white-space: pre;"><span style="font-size: large;">same time - 17:41:30 - why?</span></div><div style="font-family: monospace; white-space: pre;"><span style="font-size: large;">As you have guessed it correctly - it's because of the barrier.</span></div><div style="font-family: monospace; white-space: pre;"><span style="font-size: large;">Enjoy...</span></div><p style="font-family: monospace; font-size: 10pt; margin: 0px; white-space: pre;"><br /></p></div></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-11493425766300042972024-01-12T20:12:00.007+05:302024-02-20T09:43:25.826+05:30National Youth Day - here's is a clarion call to the engineers of Bharat - wake up and embrace Sanskrit...<p> </p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlVdeUA62Wb6gBk9j7G3E9-6gaWfkVAvxqKt8engTjAjf7iuCIUOu_abLMqKmtMNwJfiKemkg1FbwhsjK99cYqp99sMxmJrRchRI11J34n8bNIVv5Esh7h88UoZSLBDRQHbFdCyyWylDq8DKCexyrDB3RPn3egVWh9HmmoYdy1J6g8h71rYvlUtCK5hJE/s700/vivekananda%20tesla.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="310" data-original-width="700" height="284" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlVdeUA62Wb6gBk9j7G3E9-6gaWfkVAvxqKt8engTjAjf7iuCIUOu_abLMqKmtMNwJfiKemkg1FbwhsjK99cYqp99sMxmJrRchRI11J34n8bNIVv5Esh7h88UoZSLBDRQHbFdCyyWylDq8DKCexyrDB3RPn3egVWh9HmmoYdy1J6g8h71rYvlUtCK5hJE/w640-h284/vivekananda%20tesla.jpg" width="640" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><p></p><h2 style="margin: 0px; position: relative;"></h2><h1 style="text-align: left;"><span style="font-family: inherit;"><span style="font-size: large;">Nikola Tesla and Swami Vivekananda</span></span></h1><div style="font-size: 22px; text-align: left;"><span style="font-family: inherit; font-size: large;">Mr. Toby Grotz, President, Wireless Engineering</span></div><div class="separator" style="clear: both;"><div><span style="font-family: inherit; font-size: large;"><br /></span></div><div><span style="font-family: inherit;"><span style="font-size: large;">Swami Vivekananda, late in the year l895 wrote in a letter to an English friend, "Mr. Tesla thinks he can demonstrate mathematically that force and matter are reducible to potential energy. I am to go and see him next week to get this new mathematical demonstration. In that case, the Vedantic cosmology will be placed on the surest of foundations. I am working a good deal now on the cosmology and eschatology of the Vedanta. I clearly see their perfect union with modern science, and the elucidation of the one will be followed by that of the other." (Complete Works, Vol. V, Fifth Edition, 1347, p. 77).</span></span></div><div><span style="font-family: inherit;"><span style="font-size: large;"><br /></span></span></div><div><span style="font-family: inherit;"><span style="font-size: large;">Here Swamiji uses the terms force and matter for the Sanskrit terms Prana and Akasha. Tesla used the Sanskrit terms and apparently understood them as energy and mass. (In Swamiji's day, as in many dictionaries published in the first half of the present century, force and energy were not always clearly differentiated. Energy is a more proper translation of the Sanskrit term Prana.)</span></span></div><div><span style="font-family: inherit;"><span style="font-size: large;"><br /></span></span></div><div><span style="font-family: inherit;"><span style="font-size: large;">Tesla apparently failed in his effort to show the identity of mass and energy. Apparently, he understood that when speed increases, mass must decrease. He seems to have thought that mass might be "converted" to energy and vice versa, rather than that they were identical in some way, as is pointed out in Einstein's equations. At any rate, Swamiji seems to have sensed where the difficulty lay in joining the maps of European science and Advaita Vedanta and set Tesla to solve the problem. It is apparently in the hope that Tesla would succeed in this that Swamiji says "In that case, the Vedantic cosmology will be placed on the surest of foundations."</span></span></div><div><span style="font-family: inherit;"><span style="font-size: large;"><br /></span></span></div><div><span style="font-family: inherit;"><span style="font-size: large;">Unfortunately, Tesla failed and the solution did not come till ten years later, in a paper by Albert Einstein. But by then Swamiji was gone and the connecting of the maps was delayed.</span></span></div><div><span style="font-family: inherit;"><span style="font-size: large;"><br /></span></span></div><div><span style="font-size: large;">Engineers of Bharat - don't spend your life as a wage slave.</span></div><div><span style="font-size: large;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8Jkt6LbtPmjPfFTHn28x4R75DY8FCQd8gtL0cOboWr1puwFJEloGdxystkSuKzvS-gLh8EBQ5BXZS14vmZZLOJY3iMS3BU7ty-VaJWKwY29qhYOB0q_Ow8NTHUj505LOz2cN8XpNcLRzwazUO1NPXV2bAeWYz81ou1wWYRHYQ3bXSKIom5JgmBGkWp2c/s500/wage%20slave.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="500" data-original-width="500" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8Jkt6LbtPmjPfFTHn28x4R75DY8FCQd8gtL0cOboWr1puwFJEloGdxystkSuKzvS-gLh8EBQ5BXZS14vmZZLOJY3iMS3BU7ty-VaJWKwY29qhYOB0q_Ow8NTHUj505LOz2cN8XpNcLRzwazUO1NPXV2bAeWYz81ou1wWYRHYQ3bXSKIom5JgmBGkWp2c/w400-h400/wage%20slave.png" width="400" /></a></div><br /><span style="font-size: large;"><br /></span></div><div><span style="font-size: x-large;">Reclaim your true identity... </span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Embrace #Sanskrit</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Read... Read...</span></div>
<br />
<iframe height="800" src="https://www.sanskritimagazine.com/hitler-invited-dandibhatla-viswanatha-sastry-to-decode-vedas-build-missiles-and-weapons/?amp=1&fbclid=IwAR1fk06HHwevYuY3VrFVpHSxEDzgF%E2%80%93v68-frywIEAROlZ5RwuG4NCOiRjU" width="700"></iframe>
<br />
<br /><h2 style="text-align: left;">Here's why we must declare Sanskrit as the national language of Bharat...</h2><div><br /></div></div><div class="separator" style="clear: both;"><span style="font-size: large;">Let's discard Unity in diversity and embrace Unity in Unison - let's all embrace Sanskrit...</span><br /><br /><br />
<iframe height="800" src="https://sommukhopadhyay.blogspot.com/2022/05/lets-discrad-unity-in-diversity-and.html" width="800"></iframe>
<div><span style="font-family: inherit;"><span style="font-size: large;"><br /></span></span></div><div><br /></div><h2 style="text-align: left;"><span style="font-family: inherit;"><span style="font-size: large;">Here's my wife Reema reciting her own poem on wisdom</span></span></h2><div><br /></div></div>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/mDAFphWy00E?si=3u5g-Jq3xHnpY7-g" title="YouTube video player" width="560"></iframe>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-80857524604977923052024-01-07T17:43:00.012+05:302024-03-04T08:37:50.398+05:30Latches in C++ 20 concurrency - just like the CountdownLatch of Java concurrency package...<p><span style="font-family: inherit; font-size: large;">Multithreaded programming is inherently difficult. One of the reasons is that we can't have control over how a thread will start and finish - in which order - it all depends upon the thread scheduling algorithm of the OS. This makes the reproduction of test cases difficult. Moreover, there are race conditions and deadlocks.</span></p><p><span style="font-family: inherit; font-size: large;">When I was teaching the Countdown latch - a thread synchronization technique used in the Java Concurrency package, there was none like that available in C++. I am happy to see that the concept of latch is introduced in C++20.</span></p><p><span style="font-family: inherit; font-size: large;">So...</span></p><p><span style="font-family: inherit; font-size: large;"><span style="background-color: white; color: #1f1f1f; white-space-collapse: preserve;">What is a Latch in C++?</span></span></p><span style="font-family: inherit; font-size: large;"><span style="background-color: white; color: #1f1f1f;"></span></span><ul data-sourcepos="5:1-5:3" style="background-color: white; color: #1f1f1f; margin: 4px 0px; padding-inline-start: 36px;"><li data-sourcepos="5:1-5:3" style="margin-bottom: 10px;"><span style="font-family: inherit; font-size: large;">A synchronization primitive was introduced in C++20.</span></li><li data-sourcepos="6:1-6:105" style="margin-bottom: 10px;"><span style="font-family: inherit; font-size: large;">It allows one or more threads to wait for a certain number of operations to complete before proceeding.</span></li><li data-sourcepos="7:1-7:1" style="margin-bottom: 10px;"><span style="font-family: inherit; font-size: large;">Acts like a countdown counter that blocks threads until it reaches zero.</span></li></ul><div><span style="color: #1f1f1f; font-size: large;">Good to know that the C++ team is trying to catch up with Java...</span></div><div><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div><span style="color: #1f1f1f; font-size: large;">Here we go...</span></div><div><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div><span style="color: #1f1f1f; font-size: large;">My experimentation with C++ latches.</span></div><div><div class="separator" style="clear: both; text-align: center;"><br /></div></div><div><span style="color: #1f1f1f; font-size: large;"><br /></span></div>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/gSlALl6oxSA?si=REEGKiwhq7sXXpV9" title="YouTube video player" width="560"></iframe>
<div><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div><div style="background-color: white; padding: 0px 0px 0px 2px;"><div style="font-family: monospace; white-space: pre;"><p style="font-size: 10pt; margin: 0px;"><span style="color: #3f7f5f;"><br /></span></p><div style="font-family: "Times New Roman"; white-space: normal;"><span style="color: #1f1f1f; font-size: large;">I taught my young son Ridit about the Java Countdown latch three years ago.</span></div><div style="font-family: "Times New Roman"; white-space: normal;"><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div style="font-family: "Times New Roman"; white-space: normal;"><span style="color: #1f1f1f; font-size: large;">Good to see the C++ team is making the standard library more powerful day by day.</span></div><div style="font-family: "Times New Roman"; white-space: normal;"><span style="color: #1f1f1f; font-size: large;"><br /></span></div><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: large;"></span></p><div style="font-family: "Times New Roman"; white-space: normal;"><span style="color: #1f1f1f; font-size: large;">Here is the C++ source code of my experimentation.</span></div><div style="font-family: "Times New Roman"; white-space: normal;"><span style="color: #1f1f1f; font-size: large;"><br /></span></div><div style="font-family: "Times New Roman"; white-space: normal;"><span style="color: #1f1f1f; font-size: large;">The following C++ code needs C++20 to compile and execute.</span></div><div style="font-family: "Times New Roman"; white-space: normal;"><br /></div><div style="font-family: "Times New Roman"; white-space: normal;"><br /></div><div style="font-family: "Times New Roman"; font-size: medium; white-space: normal;"><span style="color: #1f1f1f; font-size: large;"><br /></span></div><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;">/*</span></p><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;"> * Student.h</span></p><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;"> *</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #3f7f5f;"> * Created on: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Jan</span><span style="color: #3f7f5f;"> 7, 2024</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #3f7f5f;"> * Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></span></p><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;"> */</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#ifndef</span> STUDENT_H_</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#define</span> STUDENT_H_</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><iostream></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><string></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><latch></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">using</span> <span style="color: #7f0055; font-weight: bold;">namespace</span> std;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">class</span> <span style="color: #005032;">Student</span> {</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">private</span>:</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #005032;">string</span> <span style="color: #0000c0;">name</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">int</span> <span style="color: #0000c0;">timeToFinish</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">public</span>:</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="font-weight: bold; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">Student</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">(</span><span style="color: #005032; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">string</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;"> name, </span><span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">int</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;"> timeToFinish){</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">name</span> = name;</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">timeToFinish</span> = timeToFinish;</span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">virtual</span> <span style="font-weight: bold;">~Student</span>(){</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">void</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;"> </span><span style="font-weight: bold; text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">task</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">(</span><span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">std::latch</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">& l){</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> cout<< <span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">name</span> << <span style="color: #2a00ff;">" is Starting the task"</span><<<span style="color: #642880; font-weight: bold;">endl</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> this_thread::sleep_for(chrono::<span style="color: #005032;">milliseconds</span>(<span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">timeToFinish</span>));</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> cout<< <span style="color: #7f0055; font-weight: bold;">this</span>-><span style="color: #0000c0;">name</span> <<<span style="color: #2a00ff;">" finished the task"</span><<<span style="color: #642880; font-weight: bold;">endl</span>;</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">l.</span><span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">count_down</span><span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">();</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;">};</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#endif</span> <span style="color: #3f7f5f;">/* STUDENT_H_ */</span></span></p><p style="margin: 0px;"></p></div></div></div><div><span style="color: #1f1f1f; font-size: medium;"><br /></span></div><div><span style="color: #1f1f1f; font-size: medium;"><br /></span></div><div><div style="background-color: white; padding: 0px 0px 0px 2px;"><div style="font-family: monospace; white-space: pre;"><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;">/*</span></p><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;"> * classETC.h</span></p><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;"> *</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #3f7f5f;"> * Created on: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Jan</span><span style="color: #3f7f5f;"> 7, 2024</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #3f7f5f;"> * Author: </span><span style="color: #3f7f5f; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">som</span></span></p><p style="margin: 0px;"><span style="color: #3f7f5f; font-size: medium;"> */</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#ifndef</span> CLASSETC_H_</span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#define</span> CLASSETC_H_</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><latch></span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;">"Student.h"</span></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">class</span> <span style="color: #005032;">classETC</span> {</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">public</span>:</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="font-weight: bold;">classETC</span>(){</span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">virtual</span> <span style="font-weight: bold;">~classETC</span>(){</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> }</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"> </span><span style="color: #7f0055; font-size: 10pt; font-weight: bold;">void</span><span style="font-size: 10pt;"> </span><span style="font-size: 10pt; font-weight: bold;">giveTaskToStudent</span><span style="font-size: 10pt;">(</span><span style="font-size: 10pt; text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">latch</span><span style="font-size: 10pt;">& l){</span></p><div style="padding: 0px 0px 0px 2px;"><div style="font-family: Monospace; font-size: 10pt;"><p style="margin: 0;"> <span style="color: #005032; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">thread</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;"> </span><span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">riditT</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">(&</span><span style="color: #005032; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">Student</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">::task, </span><span style="color: #7f0055; font-weight: bold; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">new</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;"> </span><span style="color: #005032; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">Student</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">(</span><span style="color: #2a00ff; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">"</span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Ridit</span><span style="color: #2a00ff; text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">"</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">, 1000),</span><span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ref</span><span style="text-decoration-color: rgb(130, 160, 190); text-decoration-line: underline; text-decoration-style: wavy;">(l));</span></p><p style="margin: 0;"> <span style="color: #005032;">thread</span> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ishanT</span>(&<span style="color: #005032;">Student</span>::task, <span style="color: #7f0055; font-weight: bold;">new</span> <span style="color: #005032;">Student</span>(<span style="color: #2a00ff;">"</span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Ishan</span><span style="color: #2a00ff;">"</span>, 3000), <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ref</span>(l));</p><p style="margin: 0;"> <span style="color: #005032;">thread</span> <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">rajdeepT</span>(&<span style="color: #005032;">Student</span>::task, <span style="color: #7f0055; font-weight: bold;">new</span> <span style="color: #005032;">Student</span>(<span style="color: #2a00ff;">"</span><span style="color: #2a00ff; text-decoration-color: rgb(255, 128, 64); text-decoration-line: underline; text-decoration-style: wavy;">Rajdeep</span><span style="color: #2a00ff;">"</span>, 900), <span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">ref</span>(l));</p><p style="margin: 0;"><br /></p><p style="margin: 0;"> cout<<<span style="color: #2a00ff;">"Teacher is waiting for all the students to finish their task"</span><<<span style="color: #642880; font-weight: bold;">endl</span>;</p><p style="margin: 0;"><br /></p><p style="margin: 0;"><br /></p><p style="margin: 0;"> riditT.<span style="color: #642880; font-weight: bold;">join</span>();</p><p style="margin: 0;"> ishanT.<span style="color: #642880; font-weight: bold;">join</span>();</p><p style="margin: 0;"> rajdeepT.<span style="color: #642880; font-weight: bold;">join</span>();</p><p style="margin: 0;"><br /></p><p style="margin: 0;"> l.<span style="text-decoration-color: rgb(255, 0, 0); text-decoration-line: underline; text-decoration-style: wavy;">wait</span>();</p><p style="margin: 0;"><br /></p><p style="margin: 0;"> cout<<<span style="color: #2a00ff;">"All students submitted their task... Teacher is leaving the class"</span><<<span style="color: #642880; font-weight: bold;">endl</span>;</p><p style="margin: 0;"><br /></p><p style="margin: 0;"><br /></p><p style="margin: 0;"> }</p></div></div><p style="margin: 0px;"><span style="font-size: medium;">};</span></p><p style="margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#endif</span> <span style="color: #3f7f5f;">/* CLASSETC_H_ */</span></span></p><p style="margin: 0px;"></p></div></div></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><br /></span></div><div><div style="background-color: white; padding: 0px 0px 0px 2px;"><div style="white-space: pre;"><h2 style="font-family: monospace; margin: 0px; text-align: left;"><span style="color: #3f7f5f; font-size: medium;">Main:</span></h2><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><iostream></span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><latch></span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><thread></span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;"><chrono></span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">#include</span> <span style="color: #2a00ff;">"classETC.h"</span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">using</span> <span style="color: #7f0055; font-weight: bold;">namespace</span> std;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><span style="color: #7f0055; font-weight: bold;">int</span> <span style="font-weight: bold;">main</span>() {</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> cout << <span style="color: #2a00ff;">"C++ Version"</span> << <span style="color: #642880; font-weight: bold;">endl</span>; <span style="color: #3f7f5f;">// prints C++ Version</span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 202101L)</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> cout << <span style="color: #2a00ff;">"C++23"</span>;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span>(__cplusplus == 202002L)</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> cout << <span style="color: #2a00ff;">"C++20\n"</span>;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 201703L)</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> cout << <span style="color: #2a00ff;">"C++17\n"</span>;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 201402L)</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> cout << <span style="color: #2a00ff;">"C++14\n"</span>;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 201103L)</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> cout << <span style="color: #2a00ff;">"C++11\n"</span>;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">else</span> <span style="color: #7f0055; font-weight: bold;">if</span> (__cplusplus == 199711L)</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> cout << <span style="color: #2a00ff;">"C++98\n"</span>;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">else</span> cout << <span style="color: #2a00ff;">"pre-standard C++\n"</span>;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">latch l(3);</span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #005032;">classETC</span> etc;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="text-decoration-color: rgb(255, 0, 128); text-decoration-line: underline; text-decoration-style: wavy;">etc.giveTaskToStudent(l);</span></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"><br /></span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;"> <span style="color: #7f0055; font-weight: bold;">return</span> 0;</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: medium;">}</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: large;"><br /></span></p><h1 style="font-family: monospace; margin: 0px; text-align: left;"><span style="font-size: large;">Explanation of the code:</span></h1><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: large;">Have a look at the Student class. Each student will do his work in a </span></div><div><span style="font-size: large;">background </span><span style="font-size: large;">thread. A</span><span style="font-size: large;">s there are 3 students, the latch will be initiated </span></div><div><span style="font-size: large;">with 3.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;"><span>After completing the task, a </span><span>student will reduce the latch counter by 1. </span></span></div><div><span style="font-size: large;"><span>So when all the students will complete the task, the </span><span>latch - on which</span></span></div><div><span style="font-size: large;">the main thread was blocked, will become zero and it will unblock the</span></div><div><span style="font-size: large;">main thread.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">The C++ latches work exactly the way the Java Countdown Latch</span></div><div><span style="font-size: large;">works.</span></div><div><span style="font-size: medium;"><br /></span></div><p style="font-family: monospace; font-size: 10pt; margin: 0px;"><br /></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;">For your reference, here's my son Ridit on Java CountdownLatch.</span></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;"><br /></span></p><p style="margin: 0px;"><span style="font-family: inherit; font-size: large;">#Enjoy</span></p><p style="font-family: monospace; margin: 0px;"><span style="font-size: large;"><br /></span></p><p style="font-family: monospace; font-size: 10pt; margin: 0px;"></p></div></div></div>
<iframe height="800" src="https://ridittechworld.blogspot.com/2024/01/countdownlatch-in-java.html" width="800"></iframe>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-49190094808076886052023-12-06T18:16:00.003+05:302024-03-03T09:15:13.721+05:30Why I taught my son Design Pattern even before Data Structure...<p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKR_x42skI8cfG4W1amU1ZgY8MNvD65oNEFKdf8gY8swZJ0EQuWNF8XNJwmMPy5MuBivaFzCzC3v4Riq1G9vZ5kgItXe7pczmJBDkmSj_DG7x3bjcxCrPbX9ZLF7zuctE3fwoyly82h0bVvHQLdqRjQKY8cnj4R4323YZGeBS9M6MqDYcox228rXysGFY/s640/just%20for%20fun%202.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="480" data-original-width="640" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKR_x42skI8cfG4W1amU1ZgY8MNvD65oNEFKdf8gY8swZJ0EQuWNF8XNJwmMPy5MuBivaFzCzC3v4Riq1G9vZ5kgItXe7pczmJBDkmSj_DG7x3bjcxCrPbX9ZLF7zuctE3fwoyly82h0bVvHQLdqRjQKY8cnj4R4323YZGeBS9M6MqDYcox228rXysGFY/w400-h300/just%20for%20fun%202.png" width="400" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><p><span style="font-size: large;">Studying design patterns helps a software developer get the big picture - from a designer's perspective. Knowing the common design patterns enables us to understand how different parts of the software are related to each other. Not only this, but knowledge of design patterns helps to communicate with other developers in a more object-oriented way.</span></p><p><span style="font-size: large;">Let me give you an example.</span></p><p><span style="font-size: large;">You know callback mechanism, observer pattern, and event listener pattern - all do kind of the same work - notify some objects when a specific event occurs. However, without the knowledge of design patterns, we will simply try to understand this phenomenon from just a programmer's point of view - and definitely, we will miss the big picture behind writing such kinds of object-oriented code.</span></p><p><span style="font-size: large;">I remember, when I studied the command routing architecture of Visual C++/MFC source code, I tried to map it with the Gang of Four design patterns and found out that this architecture uses two common design patterns at the same time - Command pattern and Chain of responsibility pattern.</span></p><p><span style="font-size: large;">Studying design patterns helps to decipher object-oriented code more like a professional - and not like a novice programmer. </span></p><p><span style="font-size: large;">It helps to figure out the source code not only from what and how's point of view, but also from Why's point of view. We can explain why a piece of code has been or should be written in a particular way.</span></p><p><span style="font-size: large;">Understanding the Why's is very essential for a software developer.</span></p><p><span style="font-size: large;">Let me tell you about my journey in NOKIA India in 2007 using Symbian S60 C++ framework.</span></p><p><span style="font-size: large;">There is a concept in Symbian called a two-phase constructor. I asked people about the Why's point of view. Nobody gave me the answer - everybody said this is the norm in Symbian. While breaking my head about this "why", I went to Japan and then realized the reason for such a two-phase constructor - to avoid the exception during constructor in case of low memory. Because in the earlier days of mobile phones, there was limited memory and moreover there were no template concepts in C++. Hence there was no smart pointer. This led me to study Boost pointer in Japan in 2008 and voila, in 2009 Boost library became a part of the standard C++ library.</span></p><p><span style="font-size: large;">In concisely, to get the big picture of any object-oriented code structure, it's important to know Design Patterns. It clarifies many questions - particularly why a piece of code should be written in a specific manner.</span></p><p><span style="font-size: large;">Here's the evolutionary journey of Ridit, my son and a young Computer Scientist of Bharat - vis-a-vis Design Pattern...</span></p><p><span style="font-size: large;">My bragging right - as a Guru of my son...</span></p><p><span style="font-size: large;">State Pattern in Java : 8 yrs old</span></p>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/YiClTmfRpSU?si=Ppuz7Tn050gknkjZ" title="YouTube video player" width="560"></iframe>
<p><span style="font-size: large;">State pattern in C++ : 11 yrs old</span></p>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/CySXG5yH77U?si=eFGkW2i2Hry91m1J" title="YouTube video player" width="560"></iframe>
<p><span style="font-size: x-large;">State pattern in Python : 12 yrs old</span></p>
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/IcODm8HiQXU?si=TxDXQqgCqk5H1bnd" title="YouTube video player" width="560"></iframe>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-83727266479668269812023-11-23T10:16:00.007+05:302023-11-24T09:53:50.370+05:30A baby step in the wonderland of Quantum Computing...<p><span style="font-family: inherit; font-size: large;">I am of the opinion that there is no age limit for trying out new things in life.</span></p><p><span style="font-family: inherit; font-size: large;">I consider myself a perpetual student.</span></p><p><span style="font-family: inherit; font-size: large;">Today will be a memorable day for me.</span></p><p><span style="font-family: inherit; font-size: large;">I have started my journey into the world of Quantum Computing.</span></p><p><span style="font-size: large;">From almost no knowledge in college (even didn't know how to start the Basic development environment) to taking concrete steps to pick up Quantum Computing - it's a checkered journey - a lot of midnight oil is burnt - a lot of frustration, many times it became unbearable, traveled many places - but one thing always drove me - the intrinsic motivation - never did anything just for monetary gain - remained a perpetual student. I started my software journey when there was no Google. it was naturally challenging. but worth the effort...</span></p><p><span style="font-family: inherit; font-size: x-large;">So, here we go - my first coding in the area of Quantum Programming.</span></p><div style="background-color: #f7f7f7; font-family: monospace, "Droid Sans Mono", "monospace", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: green;">#Python libraries required for various operations</span></div><div><span style="color: #af00db;">import</span> numpy <span style="color: #af00db;">as</span> np</div><br /><div><span style="color: green;">#Qiskit packages used for building a quantum circuit</span></div><div><span style="color: #af00db;">from</span> qiskit <span style="color: #af00db;">import</span> QuantumRegister, ClassicalRegister</div><div><span style="color: #af00db;">from</span> qiskit <span style="color: #af00db;">import</span> QuantumCircuit</div><br /><div><span style="color: green;">#Qiskit packages used to execute and simulate the quantum circuit</span></div><div><span style="color: #af00db;">from</span> qiskit <span style="color: #af00db;">import</span> execute, Aer</div><br /><div><span style="color: green;">#Qiskit packages used to visualize and analyze results</span></div><div><span style="color: #af00db;">from</span> qiskit.visualization <span style="color: #af00db;">import</span> plot_histogram</div><br /><div><span style="color: green;">#Create quantum register to store qubit</span></div><div>qreg_q = QuantumRegister(<span style="color: #116644;">1</span>, <span style="color: #a31515;">'q'</span>)</div><br /><div><span style="color: green;">#Create classical register to store the results</span></div><div>creg_c = ClassicalRegister(<span style="color: #116644;">1</span>, <span style="color: #a31515;">'c'</span>)</div><br /><div><span style="color: green;">#Initialize quantum circuit</span></div><div>circuit = QuantumCircuit(qreg_q, creg_c)</div><br /><div><span style="color: green;">#Initialize all qubits to |0></span></div><div>circuit.reset(qreg_q)</div><br /><div><span style="color: green;">#Apply the Hadamard gate on the qubit</span></div><div>circuit.h(qreg_q)</div><br /><div><span style="color: green;">#Apply measurement</span></div><div>circuit.measure(qreg_q, creg_c)</div><br /><div><span style="color: green;">#Visualize the constructed circuit</span></div><div>circuit.draw()</div><br /><div><span style="color: green;"># Use Aer's qasm_simulator</span></div><div>simulator = Aer.get_backend(<span style="color: #a31515;">'qasm_simulator'</span>)</div><br /><div><span style="color: green;"># Execute the circuit on the qasm simulator</span></div><div>job = execute(circuit, simulator, shots=<span style="color: #116644;">1000</span>)</div><br /><div><span style="color: green;"># Grab results from the job</span></div><div>result = job.result()</div><br /><div><span style="color: green;"># Returns counts</span></div><div>counts = result.get_counts(circuit)</div><div><span style="color: #795e26;">print</span>(<span style="color: #a31515;">"\n Output counts:"</span>,counts)</div><br /><div><span style="color: green;"># Plot a histogram</span></div><div>plot_histogram(counts)</div></div><p><br /></p><p><span style="font-size: large;">Here is the output</span></p><p><span style="background-color: white; color: #212121; font-family: monospace; white-space: pre;"><span style="font-size: x-large;"><b>Output counts: {'1': 464, '0': 536}</b></span></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwgQt5SinE1mMg_GEi7liNCV0FHyHkJ9ooq4dKo8gzsSIMeYNhlKZ7Wq32thgjzYh_uNF7WCETitiDk0URaLYhbImvar-u-T7Xg6o2VeNZ6aCM9QAavzwUr_NgsFVJHcrToMRoFJpernAF1GJ-E_htbvm1s8yiNNnUkPmhnGsOMlNvwBaOC6XqZ38Y7MQ/s633/download.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="432" data-original-width="633" height="436" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwgQt5SinE1mMg_GEi7liNCV0FHyHkJ9ooq4dKo8gzsSIMeYNhlKZ7Wq32thgjzYh_uNF7WCETitiDk0URaLYhbImvar-u-T7Xg6o2VeNZ6aCM9QAavzwUr_NgsFVJHcrToMRoFJpernAF1GJ-E_htbvm1s8yiNNnUkPmhnGsOMlNvwBaOC6XqZ38Y7MQ/w640-h436/download.png" width="640" /></a></div><br /><p><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: large;">And here goes another simple program of a basic entangler.</span></span></p><p><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: large;">Enjoy...</span></span></p><p><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: large;"></span></span></p><div style="background-color: #f7f7f7; font-family: monospace, "Droid Sans Mono", "monospace", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: green;">#Python libraries required for various operations</span></div><div><span style="color: #af00db;">import</span> numpy <span style="color: #af00db;">as</span> np</div><br /><div><span style="color: green;">#Qiskit packages used for building a quantum circuit</span></div><div><span style="color: #af00db;">from</span> qiskit <span style="color: #af00db;">import</span> QuantumRegister, ClassicalRegister</div><div><span style="color: #af00db;">from</span> qiskit <span style="color: #af00db;">import</span> QuantumCircuit</div><br /><div><span style="color: green;">#Qiskit packages used to execute and simulate the quantum circuit</span></div><div><span style="color: #af00db;">from</span> qiskit <span style="color: #af00db;">import</span> execute, Aer</div><br /><br /><div><span style="color: green;">#Qiskit packages used to visualize and analyze results</span></div><div><span style="color: #af00db;">from</span> qiskit.visualization <span style="color: #af00db;">import</span> plot_histogram</div><br /><div>qreg_q = QuantumRegister(<span style="color: #116644;">2</span>, <span style="color: #a31515;">'q'</span>)</div><div>creg_c = ClassicalRegister(<span style="color: #116644;">2</span>, <span style="color: #a31515;">'c'</span>)</div><div>circuit = QuantumCircuit(qreg_q, creg_c)</div><br /><div>circuit.reset(qreg_q[<span style="color: #116644;">0</span>])</div><div>circuit.reset(qreg_q[<span style="color: #116644;">1</span>])</div><div>circuit.h(qreg_q[<span style="color: #116644;">0</span>])</div><div>circuit.cx(qreg_q[<span style="color: #116644;">0</span>], qreg_q[<span style="color: #116644;">1</span>])</div><div>circuit.measure(qreg_q[<span style="color: #116644;">0</span>], creg_c[<span style="color: #116644;">0</span>])</div><div>circuit.measure(qreg_q[<span style="color: #116644;">1</span>], creg_c[<span style="color: #116644;">1</span>])</div><br /><div><span style="color: green;">#Visualize the constructed circuit</span></div><div>circuit.draw()</div><br /><div><span style="color: green;"># Use Aer's qasm_simulator</span></div><div>simulator = Aer.get_backend(<span style="color: #a31515;">'qasm_simulator'</span>)</div><br /><div><span style="color: green;"># Execute the circuit on the qasm simulator</span></div><div>job = execute(circuit, simulator, shots=<span style="color: #116644;">1000</span>)</div><br /><div><span style="color: green;"># Grab results from the job</span></div><div>result = job.result()</div><br /><div><span style="color: green;"># Returns counts</span></div><div>counts = result.get_counts(circuit)</div><div><span style="color: #795e26;">print</span>(<span style="color: #a31515;">"\n Output counts:"</span>,counts)</div><br /><div><span style="color: green;"># Plot a histogram</span></div><div>plot_histogram(counts)</div></div><p><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: large;">Here goes the output of the above piece of code</span></span></p><p><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: x-large;"><span style="color: #212121; font-family: monospace; white-space: pre;"><b>Output counts: {'00': 489, '11': 511}</b></span></span></span></p><p><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: x-large;"><span style="color: #212121; font-family: monospace; white-space: pre;"></span></span></span></p><div class="separator" style="clear: both; text-align: center;"><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: x-large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_3kA0J3Jjs4Hh-2XA_8X13t16oqhNJkFhKvvNvE0tld99rnLExJl2mnDmNjc7Uix93JjnCmIaQyGu6Kga8bTHuGqV-pWluz2HmNJnv4pfUhmikswLjqB85kgGZOHa1uvsjlkJ8iYoCUzx7E9xvx83h3baWM0PpHxFn7UoTP_l2okIYJL6hFjBfLeRdto/s633/download%20(1).png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="444" data-original-width="633" height="448" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_3kA0J3Jjs4Hh-2XA_8X13t16oqhNJkFhKvvNvE0tld99rnLExJl2mnDmNjc7Uix93JjnCmIaQyGu6Kga8bTHuGqV-pWluz2HmNJnv4pfUhmikswLjqB85kgGZOHa1uvsjlkJ8iYoCUzx7E9xvx83h3baWM0PpHxFn7UoTP_l2okIYJL6hFjBfLeRdto/w640-h448/download%20(1).png" width="640" /></a></span></span></div><p><span style="background-color: white;"><span style="color: #333333; font-family: inherit; font-size: large;">To start something absolutely new is challenging yet satisfying. I am always motivated by intrinsic motivation. Hence the joy of learning is still there in me, a perpetual student…</span></span></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-55173138448258804812023-11-16T13:03:00.006+05:302024-02-06T09:06:37.184+05:30Engineers of Bharat - wakeup - know WhoWeAre - part II<p> <span style="font-size: large;">Here's the clarion call to all of the engineers of Bharat - embrace #Sanskrit and go back to your roots. Know your real worth.</span></p><p><span style="font-size: large;">Till to date, scientists are baffled at how the Tithis are so accurately calculated in ancient Bharat.</span></p><p><span style="font-size: large;">Read the following article.</span></p><p><span style="font-size: large;">Our Vedas were written more than 15000 years ago.</span></p><p><span style="font-size: large;">Isn't it amazing?</span></p><p><span style="font-size: large;">We were invaded by Muslims and then the British - but our only defeat has become true when an inferiority complex was injected into us.</span></p><p><span style="font-size: large;">Come ON... the Humans of Bharat...</span></p><p><span style="font-size: large;">please ... </span></p><p><span style="font-size: x-large;">wake up and reclaim who you are...</span></p><p><span style="font-size: x-large;">Read... Read...</span></p>
<iframe allow="autoplay" height="700" src="https://drive.google.com/file/d/1rS50PaQ3ae8BYwj_Jh98xjXBRUNGcUo-/preview" width="800"></iframe>
<br />
<br />
<div><span style="font-size: large;">And see how modern scientists are struggling to calculate the Tithis given in Panjika of Bharat.</span></div><div><span style="font-size: large;"> </span></div>
<iframe allow="autoplay" height="700" src="https://drive.google.com/file/d/1EfTRMO-GIAJLjVpYGJKjDVDDGuemj_qx/preview" width="800"></iframe>
<br />
<br /><div><span style="font-size: large;">Through this write-up, I am requesting the Government of Bharat to embrace Sanskrit and our ancient knowledge and become a real Vishwaguru - the original, pure, unpolluted.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Reclaiming WhoWeAre...</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">So, the Humans of Bharat...</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">please wake up...</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Read.... Read...</span></div><div><br /></div>
<iframe height="800" src="https://www.sanskritimagazine.com/hitler-invited-dandibhatla-viswanatha-sastry-to-decode-vedas-build-missiles-and-weapons/?amp=1&fbclid=IwAR1fk06HHwevYuY3VrFVpHSxEDzgF%E2%80%93v68-frywIEAROlZ5RwuG4NCOiRjU" width="700"></iframe>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-27507568404240368492023-09-02T08:30:00.005+05:302023-09-02T08:33:20.133+05:30Iterator Design Pattern in Python...<p><span style="font-size: large;">Iterator design pattern is a behavioral design pattern that lets you access the element of a complex data structure - a collection class - without the knowledge of the internal structure of the elements it is accessing.</span></p><p><span style="font-size: large;">To create an iterator in Python, there are two abstract classes from the built-in</span><span style="font-size: x-large;"> </span><span style="font-size: large;">`collections` module - Iterable, and Iterator. We need to implement the</span></p><p><span style="font-size: large;">`__iter__()` method in the iterated object (collection), and the `__next__ ()` </span><span style="font-size: large;">method in the iterator.</span></p><p><span style="font-size: large;">The concrete class that implements the Iterator class provides various means of different ways of iteration through the collection class.</span></p><p><span style="font-size: large;">In our example, </span><span style="font-size: large;">AlphabeticalOrderIterator is such a class that has provided forward and reverse ways of iteration. In this example, the WordsCollection class represents a collection of words and implements the Iterable interface.</span></p><p><span style="font-size: large;">The source code is as follows:</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>collections.abc <span style="color: #cc7832;">import </span>Iterator<span style="color: #cc7832;">, </span>Iterable<br /><span style="color: #cc7832;">from </span>typing <span style="color: #cc7832;">import </span>Any<span style="color: #cc7832;">, </span>List<br /><br /><span style="color: #cc7832;">class </span>WordsCollection(Iterable):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>collection: List[Any]=[]):<br /> <span style="color: #94558d;">self</span>._collection = collection<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__iter__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span>AlphabeticalOrderIterator(<span style="color: #94558d;">self</span>._collection)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">get_reverse_iterator</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span>AlphabeticalOrderIterator(<span style="color: #94558d;">self</span>._collection<span style="color: #cc7832;">, True</span>)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">add_item</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>item:Any):<br /> <span style="color: #94558d;">self</span>._collection.append(item)<br /><br /><span style="color: #cc7832;">class </span>AlphabeticalOrderIterator(Iterator):<br /> _position = <span style="color: #cc7832;">None<br /></span><span style="color: #cc7832;"> </span>_reverse = <span style="color: #cc7832;">False<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"> def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>collection : WordsCollection<span style="color: #cc7832;">, </span>reverse: <span style="color: #8888c6;">bool </span>= <span style="color: #cc7832;">False</span>):<br /> <span style="color: #94558d;">self</span>._collection = collection<br /> <span style="color: #94558d;">self</span>._reverse = reverse<br /> <span style="color: #94558d;">self</span>._position = -<span style="color: #6897bb;">1 </span><span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>._reverse <span style="color: #cc7832;">else </span><span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"><br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">def </span><span style="color: #b200b2;">__next__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">try</span>:<br /> value = <span style="color: #94558d;">self</span>._collection[<span style="color: #94558d;">self</span>._position]<br /> <span style="color: #94558d;">self</span>._position += -<span style="color: #6897bb;">1 </span><span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>._reverse <span style="color: #cc7832;">else </span><span style="color: #6897bb;">1<br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">except </span><span style="color: #8888c6;">IndexError</span>:<br /> <span style="color: #cc7832;">raise </span><span style="color: #8888c6;">StopIteration</span>()<br /><br /> <span style="color: #cc7832;">return </span>value<br /><br /><br /><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> wordsCollection = WordsCollection()<br /> wordsCollection.add_item(<span style="color: #6a8759;">"Som"</span>)<br /> wordsCollection.add_item(<span style="color: #6a8759;">"Reema"</span>)<br /> wordsCollection.add_item(<span style="color: #6a8759;">"Ridit"</span>)<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Forward traversal:"</span>)<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">"</span>.join(wordsCollection))<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">""</span>)<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Reverse traversal:"</span>)<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">"</span>.join(wordsCollection.get_reverse_iterator())<span style="color: #cc7832;">, </span><span style="color: #aa4926;">end</span>=<span style="color: #6a8759;">""</span>)<br /><br /></pre></div><p><span style="font-size: large;">If we run the above program the output will be as follows:</span></p><p><span style="font-size: large;">Forward traversal:</span></p><p><span style="font-size: large;">Som</span></p><p><span style="font-size: large;">Reema</span></p><p><span style="font-size: large;">Ridit</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">Reverse traversal:</span></p><p><span style="font-size: large;">Ridit</span></p><p><span style="font-size: large;">Reema</span></p><p><span style="font-size: large;"></span></p><p><span style="font-size: large;">Som</span></p><p> </p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-78314119072860954272023-09-01T18:02:00.001+05:302023-09-01T18:03:42.138+05:30Prototype Design Pattern in Python...<p><span style="font-size: large;">The prototype</span><span style="font-family: inherit; font-size: large;"> design pattern is a creational pattern.</span></p><p><span style="font-family: inherit; font-size: large;">If the creation of an object is a very costly affair and we already have a similar kind of object, instead of creating it each time we need it, we use the clone of the object that is already there.</span></p><p><span style="font-size: large;">Suppose, there is a large object with many attributes whose data we read from a database. We need to modify that data multiple times in our program. So instead of creating the object each time by reading from the database, we tend to keep a prototype object - we clone it, and then work on it.</span></p><p><span style="font-size: large;">In Python, there is a module called copy which has two methods - copy() and deepcopy(). The first one is for the shallow copy and the second one is for the deepcopy.</span></p><p><span style="font-size: large;">Here is the source code of an example of the Prototype design pattern.</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><span style="color: #cc7832;">import </span>copy<br /><br /><span style="color: #cc7832;">class </span>Person(ABC):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>name):<br /> <span style="color: #94558d;">self</span>._name = name<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">set_name</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>name):<br /> <span style="color: #94558d;">self</span>._name = name<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">get_name</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>._name<br /> <span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">clone</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"> </span><span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>Teacher(Person):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>name<span style="color: #cc7832;">, </span>course):<br /> <span style="color: #8888c6;">super</span>().<span style="color: #b200b2;">__init__</span>(name)<br /> <span style="color: #94558d;">self</span>._course = course<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">set_course</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>course):<br /> <span style="color: #94558d;">self</span>._course = course<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">get_course</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>._course<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">Teacher's name : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>._name<span style="color: #cc7832;">, </span><span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">"</span>)<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">He teaches : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.get_course())<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">clone</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span>copy.deepcopy(<span style="color: #94558d;">self</span>)<br /><br /><span style="color: #cc7832;">class </span>Student(Person):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>name<span style="color: #cc7832;">, </span>teacher):<br /> <span style="color: #8888c6;">super</span>().<span style="color: #b200b2;">__init__</span>(name)<br /> <span style="color: #94558d;">self</span>._teacher = teacher<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> Student's name : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.get_name())<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> Taught by : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>._teacher.get_name())<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> Enrolled in the subject : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>._teacher.get_course())<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">clone</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span>copy.deepcopy(<span style="color: #94558d;">self</span>)<br /><br /><br /><br /><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> teacher = Teacher(<span style="color: #6a8759;">'Som'</span><span style="color: #cc7832;">, </span><span style="color: #6a8759;">"Python Design Patttern"</span>)<br /> teacherClone1 = teacher.clone()<br /><br /> student = Student (<span style="color: #6a8759;">"Ridit"</span><span style="color: #cc7832;">, </span>teacherClone1)<br /><br /> studentClone1 = student.clone()<br /><br /> teacherClone1.set_course(<span style="color: #6a8759;">"DSA"</span>)<br /><br /> teacherClone1.display()<br /><br /> studentClone1.display()<br /><br /><br /><br /></pre></div><p><span style="font-size: large;">If we run the above piece of code the result will be as follows:</span></p><p><span style="font-size: large;">Teacher's name : Som </span></p><p><span style="font-size: large;">He teaches : Python Design Patttern</span></p><p><span style="font-size: large;">Teacher's name : Som </span></p><p><span style="font-size: large;">He teaches : DSA</span></p><p><span style="font-size: large;">Student's name : Ridit</span></p><p><span style="font-size: large;">Taught by : Som</span></p><p><span style="font-size: large;">Enrolled in the subject : Python Design Patttern</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">As you can see, we have used deep copy here.</span></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-55175739580420213182023-08-27T18:24:00.004+05:302023-08-27T18:29:26.924+05:30Decorator Design Pattern in Python<p><span style="font-family: inherit; font-size: large;">In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class.</span></p><p><span style="font-family: inherit; font-size: large;">What problems can it solve?</span></p><p><span style="font-family: inherit; font-size: large;">- Responsibilities should be added to (and removed from) an object dynamically at run-time</span></p><p><span style="font-family: inherit; font-size: large;">- A flexible alternative to subclassing for extending functionality should be provided.</span></p><p><span style="font-family: inherit; font-size: large;">The UML diagram of the example is given below:</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsdFjNQalfvjrWg2Ci4sSWneVDVu1cHybL3-qSx5x30RE1RRVbl05zjzNeALDvFwtw6pDWquD_QZY9Yd4ajvTJYeWNO-sFNbmVtjZBAmbdqjCaovVpQF4eVdfiVdgf1pc2W0PzRufFJ3oXkI-UPV1MPXB_-zBz6bgepwApXL2GdiCKR6s6N3rrG2X3xkw/s717/decorator%20pattern.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="453" data-original-width="717" height="404" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsdFjNQalfvjrWg2Ci4sSWneVDVu1cHybL3-qSx5x30RE1RRVbl05zjzNeALDvFwtw6pDWquD_QZY9Yd4ajvTJYeWNO-sFNbmVtjZBAmbdqjCaovVpQF4eVdfiVdgf1pc2W0PzRufFJ3oXkI-UPV1MPXB_-zBz6bgepwApXL2GdiCKR6s6N3rrG2X3xkw/w640-h404/decorator%20pattern.png" width="640" /></a></div><br /><span style="font-family: inherit; font-size: large;"><br /></span><p></p><p><span style="font-family: inherit; font-size: large;">When using subclassing, different subclasses extend a class in different ways. But an extension is bound to the class at compile-time and can't be changed at run-time.</span></p><p><span style="font-family: inherit; font-size: large;">The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class.</span></p><p><span style="font-family: inherit; font-size: large;">This is achieved by designing a new Decorator class that wraps the original class. This wrapping could be achieved by the following sequence of steps:</span></p><p><span style="font-family: inherit; font-size: large;">Subclass the original Component class into a Decorator class (see UML diagram);</span></p><p><span style="font-family: inherit; font-size: large;">- In the Decorator class (SpecialistTeacher), add a Component reference as an attribute;</span></p><p><span style="font-family: inherit; font-size: large;">- In the Decorator class, pass a Component to the Decorator constructor to initialize the Component attribute;</span></p><p><span style="font-family: inherit; font-size: large;">- In the Decorator class, forward all Component methods to the Component pointer; and</span></p><p><span style="font-family: inherit; font-size: large;">- In the ConcreteDecorator class (specialistMathTeacher, specialistChemistryTeacher, etc), override any Component method(s) whose behavior needs to be modified.</span></p><p><span style="font-family: inherit; font-size: large;">This pattern is designed so that multiple decorators can be stacked on top of each other, each time adding a new functionality to the overridden method(s).</span></p><p><span style="font-family: inherit; font-size: large;">Note that decorators and the original class object share a common set of features. In the UML diagram, the doWork() and calculateSalary() methods were available in both the decorated and undecorated versions.</span></p><p><span style="font-family: inherit; font-size: large;">Here is the source code of the Decorator Design Pattern in Python:</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><br /><span style="color: #cc7832;">class </span>Teacher(ABC):<br /> <span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateSalary</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"> def </span><span style="color: #ffc66d;">doWork</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>BasicTeacher(Teacher):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>baseSalaryForATeacher):<br /> <span style="color: #94558d;">self</span>.baseSalaryForTeacher = baseSalaryForATeacher<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateSalary</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>.baseSalaryForTeacher<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">doWork</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: grey;">##Decorator<br /></span><span style="color: #cc7832;">class </span>SpecialistTeacher(Teacher):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>teacher):<br /> <span style="color: #94558d;">self</span>.teacher = teacher<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateSalary</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.specialistTeacherBaseSalary = <span style="color: #94558d;">self</span>.teacher.calculateSalary()<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>.specialistTeacherBaseSalary<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">doWork</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">super</span>().doWork()<br /> <span style="color: #94558d;">self</span>.teacher.doWork()<br /><br /><span style="color: #cc7832;">class </span>SpecialistPhysicsTeacher(SpecialistTeacher):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>specialistTeacher):<br /> <span style="color: #8888c6;">super</span>().<span style="color: #b200b2;">__init__</span>(specialistTeacher)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateSalary</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #8888c6;">super</span>().calculateSalary() + <span style="color: #6897bb;">8000<br /></span><span style="color: #6897bb;"><br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">doWork</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">super</span>().doWork()<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> I am a specialist Physics Teacher. I teach Physics"</span>)<br /><br /><br /><span style="color: #cc7832;">class </span>SpecialistMathsTeacher(SpecialistTeacher):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>specialistTeacher):<br /> <span style="color: #8888c6;">super</span>().<span style="color: #b200b2;">__init__</span>(specialistTeacher)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateSalary</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #8888c6;">super</span>().calculateSalary() + <span style="color: #6897bb;">10000<br /></span><span style="color: #6897bb;"><br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">doWork</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">super</span>().doWork()<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> I am a specialist Maths Teacher. I teach Maths"</span>)<br /><br /><br /><span style="color: #cc7832;">class </span>SpecialistChemistryTeacher(SpecialistTeacher):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>specialistTeacher):<br /> <span style="color: #8888c6;">super</span>().<span style="color: #b200b2;">__init__</span>(specialistTeacher)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateSalary</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #8888c6;">super</span>().calculateSalary() + <span style="color: #6897bb;">7000<br /></span><span style="color: #6897bb;"><br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">doWork</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">super</span>().doWork()<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> I am a specialist Chemistry Teacher. I teach Chemistry"</span>)<br /><br /><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> specialistPhyTeacher = SpecialistPhysicsTeacher(SpecialistTeacher(BasicTeacher(<span style="color: #6897bb; font-size: 9.8pt;">10000</span><span style="font-size: 9.8pt;">)))</span></pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br /> specialistPhyTeacher.doWork()</pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> My Salary is "</span><span style="color: #cc7832;">, </span>specialistPhyTeacher.calculateSalary())<br /><br /> specialistphymathsTeacher = SpecialistMathsTeacher(SpecialistPhysicsTeacher</pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"> (SpecialistTeacher(BasicTeacher(<span style="color: #6897bb; font-size: 9.8pt;">10000</span><span style="font-size: 9.8pt;">))))</span></pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br /> specialistphymathsTeacher.doWork()</pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> My Salary is "</span><span style="color: #cc7832;">, </span>specialistphymathsTeacher.calculateSalary())<br /></pre></div><p><span style="font-family: inherit; font-size: large;">If we run the above program, the output will look like the following:</span></p><p><span style="font-size: x-large;"><br /></span></p><p><span style="font-size: large;">I am a specialist Physics Teacher. I teach Physics</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">My Salary is 18000</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> I am a specialist Physics Teacher. I teach Physics</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> I am a specialist Maths Teacher. I teach Maths</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> My Salary is 28000</span></p><p><br /></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-46679576660331904712023-08-25T12:56:00.003+05:302023-08-25T13:00:19.681+05:30The Template Method Design Pattern...<p><span style="font-size: large;"> It is a behavioral design pattern. It lets us define the skeleton of an algorithm, delegating some steps to the subclasses.</span></p><p><span style="font-size: large;">There are mainly two participants in this design pattern</span></p><p><span style="font-size: large;">- AbstractClass : It defines the abstract primitive methods that the subclasses will override. It also implements a template method which define the algorithm.</span></p><p><span style="font-size: large;">- ConcreteClass : it implements the abstract primitive methods of the Abstract Class.</span></p><p><span style="font-size: large;">Let us try to understand this design pattern from an example.</span></p><p><span style="font-size: large;">Suppose we are modeling the daily activity of an engineering student in college.</span></p><p><span style="font-size: large;">All students need to sign in and sign out in a common register. So these common tasks are part of the Base class.</span></p><p><span style="font-size: large;">However, each student has to attend the classes of his own stream - maybe Electronics, Computer Science, Mechanicals, etc.</span></p><p><span style="font-size: large;">So this part of the basic activity depends on the stream of the student and constitutes part of the Concrete class.</span></p><p><span style="font-size: large;">The UML diagram of the code is as follows:</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcHx0-80_oMmblo89fZkC-YH_tLFdQcuwe9mzHTZdZQu2QIYjQJ1DLRXzORchH3jnBDflz-OsdumL2VkWKf-kLib_h1nIkeZOMr0whH_zGNi3UhmM005b25sw8FDWbjQ_9vjnEmRIFYG5sKN4rk0G2aRTyJJDpFjvm6nDofclp7i7-74hrqy-doVwBRas/s778/templatemethod.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="280" data-original-width="778" height="230" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcHx0-80_oMmblo89fZkC-YH_tLFdQcuwe9mzHTZdZQu2QIYjQJ1DLRXzORchH3jnBDflz-OsdumL2VkWKf-kLib_h1nIkeZOMr0whH_zGNi3UhmM005b25sw8FDWbjQ_9vjnEmRIFYG5sKN4rk0G2aRTyJJDpFjvm6nDofclp7i7-74hrqy-doVwBRas/w640-h230/templatemethod.png" width="640" /></a></div><br /><span style="font-size: x-large;"><br /></span><p></p><p><span style="font-size: large;">Here is the source code of the Template Method Design Pattern</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><br /><span style="color: #cc7832;">class </span>EngineeringStudent(ABC):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"> def </span><span style="color: #ffc66d;">signIn</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> Start of the day. I need to sign in the common register"</span>)<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">signOut</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> End of the day. I need to sign out in the common register"</span>)<br /> <span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">attendClass</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"> def </span><span style="color: #ffc66d;">activityofAStudent</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.signIn()<br /> <span style="color: #94558d;">self</span>.attendClass()<br /> <span style="color: #94558d;">self</span>.signOut()<br /><br /><span style="color: #cc7832;">class </span>ElectronicsStudent(EngineeringStudent):<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> I am an Electronics Student"</span>)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">attendClass</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> Attend the classes in the Electronics dept"</span>)<br /><br /><span style="color: #cc7832;">class </span>ComputerScienceStudent(EngineeringStudent):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> I am a CS Student"</span>)<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">attendClass</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;"> Attend the classes in the CS dept"</span>)<br /><br /><br /><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> electronicsStudent = ElectronicsStudent()<br /> electronicsStudent.activityofAStudent()<br /> csStudent = ComputerScienceStudent()<br /> csStudent.activityofAStudent()<br /><br /><span style="color: grey;"># See PyCharm help at https://www.jetbrains.com/help/pycharm/<br /></span></pre></div><p><span style="font-size: large;">If we run the above program, the output will be as follows:</span></p><p><span style="font-size: x-large;"><br /></span></p><p><span style="font-size: large;"> I am an Electronics Student</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> Start of the day. I need to sign in the common register</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> Attend the classes in the Electronics dept</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> End of the day. I need to sign out in the common register</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> I am a CS Student</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> Start of the day. I need to sign in the common register</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> Attend the classes in the CS dept</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;"> End of the day. I need to sign out in the common register</span></p><div><br /></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-47830532618350862152023-08-24T08:09:00.004+05:302023-08-31T12:40:22.998+05:30Memento Design Pattern in Python...<p><span style="font-family: inherit; font-size: large;">This design pattern lets you save the state of an object without revealing the implementation.</span></p><p><span style="font-family: inherit; font-size: large;">There are mainly three participants in this design pattern</span></p><p><span style="font-family: inherit; font-size: large;">- Memento: stores the internal state of the Originator object. How much state of the Originator the memento will store completely depends upon the Originator</span></p><p><span style="font-family: inherit; font-size: large;">- Originator: It creates a memento containing a snapshot of its current internal state. It also uses the Memento to restore the Original state</span></p><p><span style="font-family: inherit; font-size: large;">Caretaker: It's responsible for the memento's safekeeping.</span></p><p><span style="font-family: inherit; font-size: large;">The memento design pattern helps to implement Undo facilities for any application. Think about a word processor app - at any point in time we can undo and get back the earlier state of the document - here lies the greatness of the memento design pattern.</span></p><p><span style="font-family: inherit; font-size: large;">The UML class diagram of the Memento pattern looks like the following:</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEsIJeHYG-ssNLX5p5TgBBT_TgqJSxDC37bx2ZWnygmJBQnz57jlYzqGTyk3AcahrOD1aoEBzF6yEBZzAyE0tXKyDpZUvSovhU9FM6BBSmzWE8MkLAa3S_k7h2JleFjH_sszsA9vAg4ZQ-91SuZQsHmETKZmPqmCmhvntYblKw2PJcvFVF9hYPLDxs_fs/s791/Memento.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="344" data-original-width="791" height="278" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEsIJeHYG-ssNLX5p5TgBBT_TgqJSxDC37bx2ZWnygmJBQnz57jlYzqGTyk3AcahrOD1aoEBzF6yEBZzAyE0tXKyDpZUvSovhU9FM6BBSmzWE8MkLAa3S_k7h2JleFjH_sszsA9vAg4ZQ-91SuZQsHmETKZmPqmCmhvntYblKw2PJcvFVF9hYPLDxs_fs/w640-h278/Memento.png" width="640" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><br /><span style="font-family: inherit; font-size: large;">The source code for the Memento Design Pattern is as follows:</span><p></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">c</span>class <span style="font-size: 9.8pt;">Originator:</span></pre><div><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"> _state = <span style="color: #6a8759;">""<br /></span><span style="color: #6a8759;"><br /></span><span style="color: #6a8759;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">set</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>state):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">f"Originator: Setting state to </span><span style="color: #cc7832;">{</span>state<span style="color: #cc7832;">}</span><span style="color: #6a8759;">"</span>)<br /> <span style="color: #94558d;">self</span>._state = state<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">save_to_memento</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>.Memento(<span style="color: #94558d;">self</span>._state)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">restore_from_memento</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>m) :<br /> <span style="color: #94558d;">self</span>._state = m.get_saved_state()<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">f"Originator: State after restoring from Memento: </span><span style="color: #cc7832;">{</span><span style="color: #94558d;">self</span>._state<span style="color: #cc7832;">}</span><span style="color: #6a8759;">"</span>)<br /><br /> <span style="color: #cc7832;">class </span>Memento:<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>state):<br /> <span style="color: #94558d;">self</span>._state = state<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">get_saved_state</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>._state<br /><br /><span style="color: #cc7832;">class </span>Caretaker:<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>originator):<br /> <span style="color: #94558d;">self</span>.originator = originator<br /> <span style="color: #94558d;">self</span>.saved_states = []<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">saveState</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.saved_states.append(<span style="color: #94558d;">self</span>.originator.save_to_memento())<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">undo</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">if not </span><span style="color: #8888c6;">len</span>(<span style="color: #94558d;">self</span>.saved_states):<br /> <span style="color: #cc7832;">return<br /></span><span style="color: #cc7832;"> </span><span style="color: #94558d;">self</span>.originator.restore_from_memento(<span style="color: #94558d;">self</span>.saved_states.pop())<br /><br /><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> originator = Originator()<br /> caretaker = Caretaker(originator)<br /> originator.set(<span style="color: #6a8759;">"State1"</span>)<br /> caretaker.saveState()<br /> originator.set(<span style="color: #6a8759;">'State2'</span>)<br /> caretaker.saveState()<br /><br /> originator.set(<span style="color: #6a8759;">"State3"</span>)<br /> caretaker.saveState()<br /><br /> originator.set(<span style="color: #6a8759;">"State4"</span>)<br /> caretaker.saveState()<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">Client: Now, let's rollback!</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">"</span>)<br /> caretaker.undo()<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">Client: Once more!</span><span style="color: #cc7832;">\n</span><span style="color: #6a8759;">"</span>)<br /> caretaker.undo()<br /><br /></pre></div></div><div><span style="font-size: large;">If we run the above lines of code, the output will be:</span></div><div><br /></div><div><div><br /></div><div><span style="font-size: large;">Originator: Setting state to State1</span></div><div><span style="font-size: large;">Originator: Setting state to State2</span></div><div><span style="font-size: large;">Originator: Setting state to State3</span></div><div><span style="font-size: large;">Originator: Setting state to State4</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Client: Now, let's rollback!</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Originator: State after restoring from Memento: State4</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Client: Once more!</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Originator: State after restoring from Memento: State3</span></div><div><br /></div></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-29496329736224849672023-08-23T09:16:00.004+05:302023-08-23T10:21:25.754+05:30Facade Design Pattern in Python...<p><span style="font-family: inherit; font-size: large;">This design pattern provides a simplified and unified interface to hide the inner complexities of several subsystems or libraries.</span></p><p><span style="font-family: inherit; font-size: large;">As most of the time, the user of a subsystem does not want to know about the internal complexities. He just wants a simplified interface to use the subsystem. Subsystems become complex as they evolve. A facade can provide this simplified interface to the users of the subsystem.</span></p><p><span style="font-family: inherit; font-size: large;">The main participants of the Facade design pattern are</span></p><p><span style="font-family: inherit; font-size: large;">- facade itself</span></p><p><span style="font-family: inherit; font-size: large;">- and the several subsystems</span></p><p><span style="font-family: inherit; font-size: large;">The subsystems have no knowledge about the facade - meaning they don't keep any reference to the facade.</span></p><p><span style="font-family: inherit; font-size: large;">Let me give you an example.</span></p><p><span style="font-family: inherit; font-size: large;">Suppose there are many geometrical shapes whose drawing functionalities are complex and each one must be drawn differently. But the client really does not want to go into that complexity.</span></p><p><span style="font-family: inherit; font-size: large;">Here comes the facade pattern in the rescue. This pattern will give a single method called drawShapes which will take care of all the internal complexity of the individual shapes.</span></p><p><span style="font-family: inherit; font-size: large;">The UML class diagram of the facade design pattern example looks like the following:</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhv7J_Yj1PdKXjM48JG5OA-2VF8Da9g-c8fKns6FKz8A6arSZeCI0i3t6f2M6TxQJWuLSYA8ySuzcq-J76v0bjbcgbXYzsSZ_UgMiQ1qszPpJnWzWp8dXABhs94_RwHDhlD0YRxOG7_Xo1q87eBzrRhviMtS0OnkYT6ZhLdv0oG6k281ZnLqimJ5ztsc38/s823/facade.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="407" data-original-width="823" height="316" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhv7J_Yj1PdKXjM48JG5OA-2VF8Da9g-c8fKns6FKz8A6arSZeCI0i3t6f2M6TxQJWuLSYA8ySuzcq-J76v0bjbcgbXYzsSZ_UgMiQ1qszPpJnWzWp8dXABhs94_RwHDhlD0YRxOG7_Xo1q87eBzrRhviMtS0OnkYT6ZhLdv0oG6k281ZnLqimJ5ztsc38/w640-h316/facade.png" width="640" /></a></div><span style="font-family: inherit; font-size: large;"><br /></span><p></p><p><span style="font-family: inherit; font-size: large;">Here goes the source code of the example.</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><br /><span style="color: #cc7832;">class </span>Shape(ABC):<br /> <span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">draw</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>Circle(Shape):<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">draw</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Special method to draw a circle"</span>)<br /><br /><span style="color: #cc7832;">class </span>Rectangle(Shape):<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">draw</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Special method to draw a rectangle"</span>)<br /><br /><span style="color: #cc7832;">class </span>Triangle(Shape):<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">draw</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Special method to draw a triangle"</span>)<br /><br /><span style="color: #cc7832;">class </span>FacadeToShape:<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>circle<span style="color: #cc7832;">, </span>rectangle<span style="color: #cc7832;">, </span>triangle):<br /> <span style="color: #94558d;">self</span>.circle = circle<br /> <span style="color: #94558d;">self</span>.rectangle = rectangle<br /> <span style="color: #94558d;">self</span>.triangle = triangle<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">drawCircle</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.circle.draw()<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">drawRectangle</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.rectangle.draw()<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">drawTriangle</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.triangle.draw()<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">drawShapes</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.drawCircle()<br /> <span style="color: #94558d;">self</span>.drawTriangle()<br /> <span style="color: #94558d;">self</span>.drawRectangle()<br /><br /><br /><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> facateToShape = FacadeToShape(Circle()<span style="color: #cc7832;">, </span>Rectangle()<span style="color: #cc7832;">, </span>Triangle())<br /> facateToShape.drawShapes()<br /><br /></pre></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-57659866408500851332023-08-22T17:29:00.002+05:302023-08-23T14:09:49.839+05:30Builder Design Pattern implemented using Python...<p> <span style="font-family: inherit; font-size: large;">Today I am presenting the Builder Pattern in Python.</span></p><p><span style="font-family: inherit; font-size: large;">As the name suggests - it is part of the creational pattern.</span></p><p><span style="font-family: inherit; font-size: large;">The idea is that if a class has many member variables - and the final object may not depend on all of them - and the number of parameters that the constructor takes varies - instead of making a lot of overloaded constructors, we use this Builder pattern</span></p><p><span style="font-family: inherit; font-size: large;">The code footprint is heavily reduced - as there is no need to create a matching number of overloaded constructors to match each and every permutation and combination of the member variables passed in the constructors.</span></p><p><span style="font-family: inherit; font-size: large;">In the given example, we have created a common Student class which may represent a 10th std, a 12th std, or an engineering student - maybe in 1st yr or 2nd year, or third year or maybe in the final fourth year.</span></p><p><span style="font-family: inherit; font-size: large;">In the case of a student who is a qualified engineer, we need to supply all the marks…</span></p><p><span style="font-family: inherit; font-size: large;">But in the case of a 2nd-year student - we don’t need to bother about third-year marks or fourth-year marks, the 10th, 12th, and 1st-year marks are sufficient to represent the object of a second-year student vis-a-vis marks.</span></p><p><span style="font-family: inherit; font-size: large;">Here goes the code for the CS students of the UNIVERSE.</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><br /><span style="color: #cc7832;">class </span>Student:<br /> <span style="color: #cc7832;">class </span>Builder:<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.name=<span style="color: #6a8759;">""<br /></span><span style="color: #6a8759;"> </span><span style="color: #94558d;">self</span>.address = <span style="color: #6a8759;">""<br /></span><span style="color: #6a8759;"> </span><span style="color: #94558d;">self</span>.tenthmarks = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"> </span><span style="color: #94558d;">self</span>.twelfthmarks = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"> </span><span style="color: #94558d;">self</span>.firstyrmarks = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"> </span><span style="color: #94558d;">self</span>.secondyrmarks = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"> </span><span style="color: #94558d;">self</span>.thirdyrmarks = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"> </span><span style="color: #94558d;">self</span>.fourthyrmarks = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"><br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setname</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>name):<br /> <span style="color: #94558d;">self</span>.name = name<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setaddress</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>address):<br /> <span style="color: #94558d;">self</span>.address = address<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">settenthmarks</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>tenthmarks):<br /> <span style="color: #94558d;">self</span>.tenthmarks = tenthmarks<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">settwelfthmarks</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>twelfthmarks):<br /> <span style="color: #94558d;">self</span>.twelfthmarks = twelfthmarks<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setfirstyrmarks</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>firstyrmarks):<br /> <span style="color: #94558d;">self</span>.firstyrmarks = firstyrmarks<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setsecondyrmarks</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>secondyrmarks):<br /> <span style="color: #94558d;">self</span>.secondyrmarks = secondyrmarks<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setthirdyrmarks9</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>thirdyrmarks):<br /> <span style="color: #94558d;">self</span>.thirdyrmarks = thirdyrmarks<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setfourthyrmarks</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>fourthyrmarks):<br /> <span style="color: #94558d;">self</span>.fourthyrmarks = fourthyrmarks<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self<br /></span><span style="color: #94558d;"><br /></span><span style="color: #94558d;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">build</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span>Student(<span style="color: #94558d;">self</span>)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>builder):<br /> <span style="color: #94558d;">self</span>.name = builder.name<br /> <span style="color: #94558d;">self</span>.addrees = builder.address<br /> <span style="color: #94558d;">self</span>.tenthmarks = builder.tenthmarks<br /> <span style="color: #94558d;">self</span>.twelfthmarks = builder.twelfthmarks<br /> <span style="color: #94558d;">self</span>.firstyrmarks = builder.firstyrmarks<br /> <span style="color: #94558d;">self</span>.secondyrmarks = builder.secondyrmarks<br /> <span style="color: #94558d;">self</span>.thirdyrmarks = builder.thirdyrmarks<br /> <span style="color: #94558d;">self</span>.fourthyrmarks = builder.fourthyrmarks<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">displayData</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.name != <span style="color: #6a8759;">""</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Name : " </span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.name)<br /><br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.addrees != <span style="color: #6a8759;">""</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Address : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.addrees)<br /><br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.tenthmarks != <span style="color: #6897bb;">0</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"10th Marks : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.tenthmarks)<br /><br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.twelfthmarks != <span style="color: #6897bb;">0</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"12th Marks : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.twelfthmarks)<br /><br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.firstyrmarks != <span style="color: #6897bb;">0</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"1st Yr Marks : "</span><span style="color: #cc7832;">,</span><span style="color: #94558d;">self</span>.firstyrmarks)<br /><br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.secondyrmarks != <span style="color: #6897bb;">0</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"2nd Yr Marks : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.secondyrmarks)<br /><br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.thirdyrmarks != <span style="color: #6897bb;">0</span>:<br /> <span style="color: #8888c6;">print </span>(<span style="color: #6a8759;">"3rd Yr marks : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.thirdyrmarks)<br /><br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.fourthyrmarks != <span style="color: #6897bb;">0</span>:<br /> <span style="color: #8888c6;">print </span>(<span style="color: #6a8759;">"4th Yr Marks : "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.fourthyrmarks)<br /><br /><br /><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> ram12th = Student.Builder().setname(<span style="color: #6a8759;">"Ram"</span>).setaddress(<span style="color: #6a8759;">"Kolkata"</span>).</pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;">settenthmarks(<span style="color: #6897bb;">50</span>).build()<br /><br /> shyam2ndYr = Student.Builder().setname(<span style="color: #6a8759;">"Shyam"</span>).setaddress(<span style="color: #6a8759;">"Delhi"</span>)</pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;">.settenthmarks(<span style="color: #6897bb;">70</span>).settwelfthmarks(<span style="color: #6897bb;">78</span>).setfirstyrmarks(<span style="color: #6897bb;">70</span>).build()</pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br /> ram12th.displayData()</pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br /> shyam2ndYr.displayData()<br /><br /></pre></div><div><span style="font-size: large;">If we run the above program, the output will be as follows:</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;"><div>Name : Ram</div><div>Address : Kolkata</div><div>10th Marks : 50</div><div><br /></div><div>Name : Shyam</div><div>Address : Delhi</div><div>10th Marks : 70</div><div>12th Marks : 78</div><div>1st Yr Marks : 70</div><div><br /></div><div>As it is clear for Ram, till 10th marks was important whereas, for Shyam, on the other hand, marks till 1st year are important.</div><div><br /></div></span></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-54592806360866322142023-08-20T15:36:00.003+05:302023-08-21T09:58:36.143+05:30Visitor Pattern in Python...<p><span style="font-size: large;"> Visitor design pattern allows the addition of completely different functionalities to an existing class without much alteration in the original class.</span></p><p><span style="font-size: large;">Let me explain it with an example.</span></p><p><span style="font-size: large;">Suppose there are two items a shop sells - Book and Medicine</span></p><p><span style="font-size: large;">Now say, normally these two Item classes would look like two - what we call in Java as POJO classes where the most important attribute will be the price.</span></p><p><span style="font-size: large;">So far so good.</span></p><p><span style="font-size: large;">Now suppose the government is running </span></p><p><span style="font-size: large;">- one literacy mission</span></p><p><span style="font-size: large;">and </span></p><p><span style="font-size: large;">- one Health mission</span></p><p><span style="font-size: large;">Under these missions, few books are given huge discounts, and few medicines are sold at discounted prices.</span></p><p><span style="font-size: large;">Without the Visitor pattern, the algorithm of the discounts would have been put inside the POJO classes which might create maintenance problems in the future when the algorithm for discount changes.</span></p><p><span style="font-size: large;">With the visitor pattern, we encapsulate all these discount algorithms inside a special method called <b>visit </b>which comes from a Visitor interface.</span></p><p><span style="font-size: large;">So if it is a LiteracyMissionVisitor, the special algorithm offers a discount on special books whereas if it is a HealthMissionVisitor, the discount goes to specific medicines – all we have to do is to call accept on these special Book and Medicine objects passing the proper Visitor object,</span></p><p><span style="font-size: large;">That's it...</span></p><p><span style="font-size: x-large;">The UML class diagram looks as follows:</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaSW8_6huxv60ULjjf9TgPz1Zybd-V8rDzTj7N2gTbEam11mIW8naj6UpDG5I8iNbaEIJs-1gPE4GHpstRFfYhUIn2Lv-Lbegmup7J-kGhQhI9ll041wyKLWiUxAOQl-OxrJ4tfHfP97Zi9UlXHTOtpQ9YTEc4raRbv4CGhEQk4VB75DAx0OnTmULWf34/s1113/Visitor%20Pattern.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="406" data-original-width="1113" height="234" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaSW8_6huxv60ULjjf9TgPz1Zybd-V8rDzTj7N2gTbEam11mIW8naj6UpDG5I8iNbaEIJs-1gPE4GHpstRFfYhUIn2Lv-Lbegmup7J-kGhQhI9ll041wyKLWiUxAOQl-OxrJ4tfHfP97Zi9UlXHTOtpQ9YTEc4raRbv4CGhEQk4VB75DAx0OnTmULWf34/w640-h234/Visitor%20Pattern.png" width="640" /></a></div><br /><span style="font-size: large;"><br /></span><p></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">And here goes the source code of this Visitor Pattern</span></p><div style="background-color: #1e1f22; color: #bcbec4;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cf8e6d;">from </span>abc <span style="color: #cf8e6d;">import </span>ABC, abstractmethod<br /><br /><span style="color: #cf8e6d;">class </span>Visitor(ABC):<br /> <span style="color: #b3ae60;">@abstractmethod<br /></span><span style="color: #b3ae60;"> </span><span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">visit</span>(<span style="color: #94558d;">self</span>, book):<br /> <span style="color: #cf8e6d;">pass<br /></span><span style="color: #cf8e6d;"> </span><span style="color: #b3ae60;">@abstractmethod<br /></span><span style="color: #b3ae60;"> </span><span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">visit</span>(<span style="color: #94558d;">self</span>, medicine):<br /> <span style="color: #cf8e6d;">pass<br /></span><span style="color: #cf8e6d;"><br /></span><span style="color: #cf8e6d;">class </span>Visitable(ABC):<br /> <span style="color: #b3ae60;">@abstractmethod<br /></span><span style="color: #b3ae60;"> </span><span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">accept</span>(<span style="color: #94558d;">self</span>, visitor):<br /> <span style="color: #cf8e6d;">pass<br /></span><span style="color: #cf8e6d;"><br /></span><span style="color: #cf8e6d;">class </span>Book(Visitable):<br /> <span style="color: #cf8e6d;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>, price):<br /> <span style="color: #94558d;">self</span>.price = price<br /> <span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">accept</span>(<span style="color: #94558d;">self</span>, visitor):<br /> <span style="color: #cf8e6d;">return </span>visitor.visit(<span style="color: #94558d;">self</span>)<br /><br /> <span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">getPrice</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cf8e6d;">return </span><span style="color: #94558d;">self</span>.price<br /><br /><span style="color: #cf8e6d;">class </span>Medicine(Visitable):<br /> <span style="color: #cf8e6d;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>, price):<br /> <span style="color: #94558d;">self</span>.price = price<br /><br /> <span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">accept</span>(<span style="color: #94558d;">self</span>, visitor):<br /> <span style="color: #cf8e6d;">return </span>visitor.visit(<span style="color: #94558d;">self</span>)<br /><br /> <span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">getPrice</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cf8e6d;">return </span><span style="color: #94558d;">self</span>.price<br /><br /><span style="color: #cf8e6d;">class </span>LiteracyMissionVisitor(Visitor):<br /> <span style="color: #cf8e6d;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>, percentagediscountOnBook):<br /> <span style="color: #94558d;">self</span>.discount = percentagediscountOnBook<br /> <span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">visit</span>(<span style="color: #94558d;">self</span>, book):<br /> book.price = book.price - (book.price * <span style="color: #94558d;">self</span>.discount)/<span style="color: #2aacb8;">100<br /></span><span style="color: #2aacb8;"> </span><span style="color: #cf8e6d;">return </span>book.price<br /><br /><span style="color: #cf8e6d;">class </span>HealthMissionVisitor(Visitor):<br /> <span style="color: #cf8e6d;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>, percentagediscountOnMedicine):<br /> <span style="color: #94558d;">self</span>.discount = percentagediscountOnMedicine<br /><br /> <span style="color: #cf8e6d;">def </span><span style="color: #56a8f5;">visit</span>(<span style="color: #94558d;">self</span>, medicine):<br /> medicine.price = medicine.price - (medicine.price * <span style="color: #94558d;">self</span>.discount)/<span style="color: #2aacb8;">100<br /></span><span style="color: #2aacb8;"> </span><span style="color: #cf8e6d;">return </span>medicine.price<br /><br /><br /><br /><br /><span style="color: #7a7e85;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cf8e6d;">if </span>__name__ == <span style="color: #6aab73;">'__main__'</span>:<br /><br /> literacyMissionVisitor = LiteracyMissionVisitor(<span style="color: #2aacb8;">50</span>)<br /><br /> chandaMama = Book(<span style="color: #2aacb8;">100</span>)<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6aab73;">"Original price of the book is "</span>, chandaMama.getPrice())<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6aab73;">"Due to literacy mission, there is huge discount on the book. </span></pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #6aab73;"><span> </span><span> </span>After discount, the price is "</span>, chandaMama.accept(literacyMissionVisitor))<br /><br /> healthDriveVisitor = HealthMissionVisitor(<span style="color: #2aacb8;">70</span>)<br /><br /> vitaminDCapsule = Medicine(<span style="color: #2aacb8;">200</span>)<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6aab73;">"Original price of the medinine is "</span>, vitaminDCapsule.getPrice())<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6aab73;">"Due to health mission the reduced price of the </span></pre><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #6aab73;"><span> </span><span> </span><span> </span>medicine is "</span>, vitaminDCapsule.accept(healthDriveVisitor))<br /></pre></div><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">If we run this program, the output will be as follows:</span></p><p><span style="font-size: x-large;"><b>Original price of the book is 100</b></span></p><p><span style="font-size: large;"><b>Due to literacy mission, there is huge discount on the book. After discount, the price is 50.0</b></span></p><p><span style="font-size: large;"><b>Original price of the medinine is 200</b></span></p><p><span style="font-size: large;"><b>Due to health mission the reduced price of the medicine is 60.0</b></span></p><div><br /></div><p><span style="font-size: large;"><br /></span></p><p><br /></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-53244845344751582632023-08-19T09:24:00.003+05:302023-09-11T19:14:02.566+05:30Mediator Design Pattern in Python...<p><span style="font-family: inherit; font-size: large;">This design pattern lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.</span></p><p><span style="font-family: inherit; font-size: large;">The Mediator interface declares a method used by components to notify the mediator about various events. The Mediator may react to these events and triggers specific components to execute different yet specific tasks.</span></p><p><span style="font-family: inherit; font-size: large;">The Base Component provides the basic functionality of storing a mediator's reference inside the component.</span></p><p><span style="font-family: inherit; font-size: large;">Concrete Components implement various functionality. They don't depend on other components.</span></p><p><span style="font-family: inherit; font-size: large;">Let me explain it to you with an example.</span></p><p><span style="font-family: inherit; font-size: large;">Suppose two students Ridit and Rajdeep take a lot of responsibility in a class. But something bad happens between them, and hence they don't talk to each other. So, here comes the ClassMonitor - as a Mediator between Rajdeep and Ridit - if Ridit does anything that needs Rajdeep to do another task, Ridit notifies the Class Monitor, and then the Class Monitor asks Rajdeep to execute the necessary task.</span></p><p><span style="font-family: inherit; font-size: large;">Here's the class diagram of the Mediator Pattern</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPd9ZvEt5d9KtYHmuQhCs8mwQg62pyeAjFDML4KnPi5120pOiorMCP5RBmsxLceZho6kEtpGUcdNTEIze7x2wqWzbzaHmfvR2bXcvfKk1jgGXwHwOlNP4gbtslDT2pobrMua3APekeceHcG59rh_g_fUt4x6qSrdxVlGumV3bZNrTFKhnBb2mIounxv3Y/s962/Mediator%20Patter%20Class%20Diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="459" data-original-width="962" height="306" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPd9ZvEt5d9KtYHmuQhCs8mwQg62pyeAjFDML4KnPi5120pOiorMCP5RBmsxLceZho6kEtpGUcdNTEIze7x2wqWzbzaHmfvR2bXcvfKk1jgGXwHwOlNP4gbtslDT2pobrMua3APekeceHcG59rh_g_fUt4x6qSrdxVlGumV3bZNrTFKhnBb2mIounxv3Y/w640-h306/Mediator%20Patter%20Class%20Diagram.png" width="640" /></a></div><span style="font-family: inherit; font-size: x-large;">There are many uses for this design pattern. This design pattern can be found in most of the widget message-passing systems - individual widgets don't pass or communicate with each other - but they communicate through the container window.</span><p></p><p><span style="font-family: inherit; font-size: large;">For Android engineers - you remember how two fragments talk to each other through the container window.</span></p><p><span style="font-family: inherit; font-size: large;">Here goes the source code for this Design Pattern.</span></p><p><span style="font-family: inherit; font-size: large;">Enjoy.</span></p><p><span style="font-family: inherit; font-size: large;">Happy learning.</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><br /><span style="color: #cc7832;">class </span>ClassMonitor(ABC):<br /> <span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">notify</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>sender<span style="color: #cc7832;">, </span>event):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;">class </span>ConcreteClassMonitor(ClassMonitor):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>student1<span style="color: #cc7832;">, </span>student2):<br /> <span style="color: #94558d;">self</span>.student1 = student1<br /> <span style="color: #94558d;">self</span>.student2 = student2<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">notify</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>sender<span style="color: #cc7832;">, </span>event):<br /> <span style="color: #cc7832;">if </span>event == <span style="color: #6a8759;">"A"</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"The class Monitor reacts to event A and triggers Rajdeep to do task C"</span>)<br /> <span style="color: #94558d;">self</span>.student2.do_c()<br /> <span style="color: #cc7832;">elif </span>event == <span style="color: #6a8759;">"D"</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"The class Monitor reacts to event D and triggers Ridit to do Task B and Rajdeep to do Task C"</span>)<br /> <span style="color: #94558d;">self</span>.student1.do_b()<br /> <span style="color: #94558d;">self</span>.student2.do_c()<br /><br /><span style="color: #cc7832;">class </span>BaseStudent:<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>monitor):<br /> <span style="color: #94558d;">self</span>._monitor = monitor<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">getMonitorr</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>._monitor<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setMonitor</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>monitor):<br /> <span style="color: #94558d;">self</span>._monitor = monitor<br /><br /><span style="color: #cc7832;">class </span>Ridit(BaseStudent):<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">do_a</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Ridit does A."</span>)<br /> <span style="color: #94558d;">self</span>._monitor.notify(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span><span style="color: #6a8759;">"A"</span>)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">do_b</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Ridit does B."</span>)<br /> <span style="color: #94558d;">self</span>._monitor.notify(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span><span style="color: #6a8759;">"B"</span>)<br /><br /><span style="color: #cc7832;">class </span>Rajdeep(BaseStudent):<br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">do_c</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Rajdeep does C."</span>)<br /> <span style="color: #94558d;">self</span>._monitor.notify(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span><span style="color: #6a8759;">"C"</span>)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">do_d</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Rajdeep does D"</span>)<br /> <span style="color: #94558d;">self</span>._monitor.notify(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span><span style="color: #6a8759;">"D"</span>)<br /><br /><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> ridit = Ridit(<span style="color: #cc7832;">None</span>)<br /> rajdeep = Rajdeep(<span style="color: #cc7832;">None</span>)<br /> classMonitor = ConcreteClassMonitor(ridit<span style="color: #cc7832;">,</span>rajdeep)<br /> ridit.setMonitor(classMonitor)<br /> rajdeep.setMonitor(classMonitor)<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Ma'am asks Ridit to do A"</span>)<br /> ridit.do_a()<br /><br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Ma'am asks Rajdeep to do D."</span>)<br /> rajdeep.do_d()<br /></pre></div>
<span style="font-size: large;">If we run the above program the output will be like the following:</span><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;"><div><br /></div><div>Ma'am asks Ridit to do A</div><div>Ridit does A.</div><div>The class Monitor reacts to event A and triggers Rajdeep to do task C</div><div>Rajdeep does C.</div><div>Ma'am asks Rajdeep to do D.</div><div>Rajdeep does D</div><div>The class Monitor reacts to event D and triggers Ridit to do Task B and Rajdeep to do Task C</div><div>Ridit does B.</div><div>Rajdeep does C.</div><div><br /></div></span>
<br />
</div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-88750113276883955002023-08-18T12:45:00.004+05:302023-08-18T13:23:29.984+05:30Composite Design Pattern in Python....<p> <span style="font-family: inherit; font-size: large;">When I first started studying the Gang of Four book back in 2003-2004, it was an absolutely new thing to me. I must admit today that not everything became clear at that time. But gradually, the knowledge is being imbibed, and now while teaching my young son Ridit on this subject, I just love it.</span></p><p><span style="font-family: inherit; font-size: large;">Moreover, as I am now comfortable with three languages namely C++, Java, and Python, recreating the stuff for various languages looks pretty easy now.</span></p><p><span style="font-family: inherit; font-size: large;">So, today, I am rewriting the Composite Design Pattern in Python. Here goes my original Composite Design pattern implemented using C++ and written in 2008.</span></p>
<br />
<iframe height="800" src="https://som-itsolutions.blogspot.com/2008/12/composite-design-pattern-is-structural.html" width="800"></iframe>
<br />
<br /><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">And here goes the </span><span style="font-size: large;"><span>exploration of the Composite Design Pattern by </span><span>my young son, Ridit, currently a class VII student.</span></span></div><div><br /></div><div><br /></div>
<iframe height="800" src="https://ridittechworld.blogspot.com/2022/06/my-learning-and-implenting-composite.html" width="800"></iframe>
<div><span style="font-size: x-large;"><br /></span></div><div><span style="font-size: x-large;">And here goes the Composite Design Pattern implemented in Python.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">I hope you will like it.</span></div><div><span style="font-size: large;"><br /></span></div><div><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><br /><span style="color: #cc7832;">class </span>Shape(ABC):<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.parent = <span style="color: #cc7832;">None<br /></span><span style="color: #cc7832;"> </span><span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">add</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>shape):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"> </span><span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">remove</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>shape):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"> def </span><span style="color: #ffc66d;">getparent</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">return </span><span style="color: #94558d;">self</span>.parent<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setparent</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>parent):<br /> <span style="color: #94558d;">self</span>.parent = parent<br /><br /> <span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>CompositeShape(Shape):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #94558d;">self</span>.listofShapes = []<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">add</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>shape):<br /> shape.setparent(<span style="color: #94558d;">self</span>)<br /> <span style="color: #94558d;">self</span>.listofShapes.append(shape)<br /><br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">remove</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>shape):<br /> <span style="color: #94558d;">self</span>.listofShapes.remove(shape)<br /> shape.setparent(<span style="color: #cc7832;">None</span>)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">setParent</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>parent):<br /> <span style="color: #94558d;">self</span>.parent = parent<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>Point(Shape):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>x<span style="color: #cc7832;">, </span>y):<br /> <span style="color: #94558d;">self</span>.x = x<br /> <span style="color: #94558d;">self</span>.y = y<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print </span>(<span style="color: #6a8759;">"X = "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.x<span style="color: #cc7832;">, </span><span style="color: #6a8759;">"Y = "</span><span style="color: #cc7832;">, </span><span style="color: #94558d;">self</span>.y)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">add</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>shape):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"This is a leaf class.... Can't add to it..."</span>)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">remove</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>shape):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"This is a leaf class.... Can't remove from it..."</span>)<br /><br /><span style="color: #cc7832;">class </span>Line(CompositeShape):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>p1<span style="color: #cc7832;">, </span>p2):<br /> <span style="color: #8888c6;">super</span>().<span style="color: #b200b2;">__init__</span>()<br /> <span style="color: #94558d;">self</span>.add(p1)<br /> <span style="color: #94558d;">self</span>.add(p2)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"The starting and end points of the line are"</span>)<br /> <span style="color: #cc7832;">for </span>shape <span style="color: #cc7832;">in </span><span style="color: #94558d;">self</span>.listofShapes:<br /> shape.display()<br /><br /><span style="color: #cc7832;">class </span>Quadrilateral(CompositeShape):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>p1<span style="color: #cc7832;">,</span>p2<span style="color: #cc7832;">,</span>p3<span style="color: #cc7832;">,</span>p4):<br /> <span style="color: #8888c6;">super</span>().<span style="color: #b200b2;">__init__</span>()<br /> <span style="color: #94558d;">self</span>.add(p1)<br /> <span style="color: #94558d;">self</span>.add(p2)<br /> <span style="color: #94558d;">self</span>.add(p3)<br /> <span style="color: #94558d;">self</span>.add(p4)<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">display</span>(<span style="color: #94558d;">self</span>):<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"The four corners of the quadrilateral are "</span>)<br /> <span style="color: #cc7832;">for </span>shape <span style="color: #cc7832;">in </span><span style="color: #94558d;">self</span>.listofShapes:<br /> shape.display()<br /><br /><br /><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> p1 = Point(<span style="color: #6897bb;">10</span><span style="color: #cc7832;">,</span><span style="color: #6897bb;">20</span>)<br /> p2 = Point(<span style="color: #6897bb;">30</span><span style="color: #cc7832;">,</span><span style="color: #6897bb;">40</span>)<br /> p3 = Point(<span style="color: #6897bb;">50</span><span style="color: #cc7832;">,</span><span style="color: #6897bb;">60</span>)<br /> p4 = Point(<span style="color: #6897bb;">70</span><span style="color: #cc7832;">,</span><span style="color: #6897bb;">80</span>)<br /><br /> p1.add(p2)<br /><br /> L1 = Line(p1<span style="color: #cc7832;">,</span>p2)<br /> q1 = Quadrilateral(p1<span style="color: #cc7832;">,</span>p2<span style="color: #cc7832;">,</span>p3<span style="color: #cc7832;">,</span>p4)<br /><br /> p1.display()<br /> p2.display()<br /> L1.display()<br /><br /> q1.display()</pre></div></div>
<div><br /></div><span style="font-size: large;">If we execute the above code, the console window will display the following result</span><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;"><div><br /></div><div>This is a leaf class... Can't add to it...</div><div>X = 10 Y = 20</div><div>X = 30 Y = 40</div><div>The starting and end points of the line are</div><div>X = 10 Y = 20</div><div>X = 30 Y = 40</div><div>The four corners of the quadrilateral are </div><div>X = 10 Y = 20</div><div>X = 30 Y = 40</div><div>X = 50 Y = 60</div><div>X = 70 Y = 80</div></span>
<br /></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-49115222340197826282023-08-16T18:04:00.001+05:302023-08-16T18:04:48.472+05:30Chain Of Responsibility in Python...<p> <span style="font-family: inherit; font-size: large;">In this design pattern, there are some sorts of executors of a command tied in a chain - at different levels. Depending upon the complexity of the task, the command is handled at different levels. Each receiver is well aware of if he can handle the command. If he can't he simply passes the buck to the next person in the chain. Once the command reaches the highest level of the chain, it must be handled there as there is no more successor after the highest authority.</span></p><p><span style="font-family: inherit; font-size: large;">The idea is that the command is always delivered at the lowest level of the chain and then it moves forward - one step at a time till it is handled at one such level.</span></p><p><span style="font-family: inherit; font-size: large;">Here is the class diagram of the Chain of Responsibility design pattern.</span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd07c8s6EGyABZzpm5f5CFR0FScg--mmpOkpEtWUWvSp0ylm8pUPFmpi6SAeQYwak91TSSnLwr7AGtIG8TIVkgjQpGescyZlxxMD4s7RL00eY_SaiiRIQZ4kDg6LsC0UJkFPK4g7ZMyQHBBg6c0p1cZwZLoXpOw3DpoQ_ju37mNggXAD3pS7dFWMcXh1o/s1366/Chain%20of%20Responsibility.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="768" data-original-width="1366" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd07c8s6EGyABZzpm5f5CFR0FScg--mmpOkpEtWUWvSp0ylm8pUPFmpi6SAeQYwak91TSSnLwr7AGtIG8TIVkgjQpGescyZlxxMD4s7RL00eY_SaiiRIQZ4kDg6LsC0UJkFPK4g7ZMyQHBBg6c0p1cZwZLoXpOw3DpoQ_ju37mNggXAD3pS7dFWMcXh1o/w640-h360/Chain%20of%20Responsibility.png" width="640" /></a></div><br /><p><span style="font-size: large;">Here goes the source code of the chain of responsibility implemented in Python...</span></p><div style="background-color: #2b2b2b; color: #a9b7c6;"><pre style="font-family: 'JetBrains Mono',monospace; font-size: 9.8pt;"><span style="color: #cc7832;">from </span>abc <span style="color: #cc7832;">import </span>ABC<span style="color: #cc7832;">, </span>abstractmethod<br /><br /><br /><span style="color: #cc7832;">class </span>ICommand(ABC):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>level):<br /> <span style="color: #94558d;">self</span>.level = level<br /><br /><br /><span style="color: #cc7832;">class </span>CommandReceiver(ABC):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>successor):<br /> <span style="color: #94558d;">self</span>.successor = successor<br /><br /><br /> <span style="color: #bbb529;">@abstractmethod<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">handleCommandImpl</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>command):<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"> def </span><span style="color: #ffc66d;">handleCommand</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>command):<br /> retType = <span style="color: #94558d;">self</span>.handleCommandImpl(command)<br /> <span style="color: #cc7832;">if </span><span style="color: #94558d;">self</span>.successor != <span style="color: #cc7832;">None and </span>retType == <span style="color: #cc7832;">False</span>:<br /> <span style="color: #94558d;">self</span>.successor.handleCommand(command)<br /> <span style="color: #cc7832;">else</span>:<br /> <span style="color: #cc7832;">return<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>ReceiverLevel1(CommandReceiver):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>successor):<br /> <span style="color: #94558d;">self</span>.successor = successor<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">handleCommandImpl</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>command):<br /> <span style="color: #cc7832;">if </span>command.level == <span style="color: #6897bb;">1</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"The Command is handled at level 1"</span>)<br /> <span style="color: #cc7832;">return True<br /></span><span style="color: #cc7832;"> else</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Forwarding the command from level 1 to level 2"</span>)<br /> <span style="color: #cc7832;">return False<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>ReceiverLevel2(CommandReceiver):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>successor):<br /> <span style="color: #94558d;">self</span>.successor = successor<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">handleCommandImpl</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>command):<br /> <span style="color: #cc7832;">if </span>command.level == <span style="color: #6897bb;">2</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"The Command is handled at level 2"</span>)<br /> <span style="color: #cc7832;">return True<br /></span><span style="color: #cc7832;"> else</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Forwarding the command from level 2 to level 3"</span>)<br /> <span style="color: #cc7832;">return False<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">class </span>ReceiverLevel3(CommandReceiver):<br /> <span style="color: #cc7832;">def </span><span style="color: #b200b2;">__init__</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>successor):<br /> <span style="color: #94558d;">self</span>.successor = successor<br /><br /> <span style="color: #cc7832;">def </span><span style="color: #ffc66d;">handleCommandImpl</span>(<span style="color: #94558d;">self</span><span style="color: #cc7832;">, </span>command):<br /> <span style="color: #cc7832;">if </span>command.level == <span style="color: #6897bb;">3</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"The Command is handled at level 3"</span>)<br /> <span style="color: #cc7832;">return True<br /></span><span style="color: #cc7832;"> else</span>:<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Last level... The Command has to be managed here..."</span>)<br /> <span style="color: #cc7832;">return False<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"><br /></span><span style="color: grey;"># Press the green button in the gutter to run the script.<br /></span><span style="color: #cc7832;">if </span>__name__ == <span style="color: #6a8759;">'__main__'</span>:<br /> commandLevel1 = ICommand(<span style="color: #6897bb;">1</span>)<br /> commandLevel2 = ICommand(<span style="color: #6897bb;">2</span>)<br /> commandLevel3 = ICommand(<span style="color: #6897bb;">3</span>)<br /><br /> receiverLevel3 = ReceiverLevel3(<span style="color: #cc7832;">None</span>)<br /> receiverLevel2 = ReceiverLevel2(receiverLevel3)<br /> receiverLevel1 = ReceiverLevel1(receiverLevel2)<br /><br /> receiverLevel1.handleCommand(commandLevel3)</pre></div><p><br /></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-45825004503079360802023-05-18T06:04:00.003+05:302023-05-18T06:31:01.376+05:30State Design Pattern in C++ using boost's pointer...<span style="font-family: inherit; font-size: large;">After a long time, I am again back to my tech domain. Please enjoy my work on State Design Pattern in C++ using boost's pointer.</span><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Here is the class diagram of my code</span></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgs6L2WDXPI7bHkzxfuXP0_DDyaALK9oOeQuisnhG5kFGB9qCUcmiuFg_ns05DtQxwpK_GbVO019TzYE_fWYCKhcQsrI0Rs8d_WemfZXpsbzzRNZucUzzK-B0gpllNcZPkbDpeMn3sqK0_z_-o86K7pV23BeEFwNcOGwq5i9NE3aN6KLsVy3ZA8gq0K/s1139/state%20pattern%20class%20diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="537" data-original-width="1139" height="302" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgs6L2WDXPI7bHkzxfuXP0_DDyaALK9oOeQuisnhG5kFGB9qCUcmiuFg_ns05DtQxwpK_GbVO019TzYE_fWYCKhcQsrI0Rs8d_WemfZXpsbzzRNZucUzzK-B0gpllNcZPkbDpeMn3sqK0_z_-o86K7pV23BeEFwNcOGwq5i9NE3aN6KLsVy3ZA8gq0K/w640-h302/state%20pattern%20class%20diagram.png" width="640" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><br /><h2 style="text-align: center;">Class Diagram</h2><div><div><br />
<br />
<iframe height="800" src="https://docs.google.com/document/d/e/2PACX-1vSmKXZ-mCcUvcX5CdK5PIspCc1OqveVXJgjRxowEFJtr3b_ETh7ZqWI_XKhC-1q1FQnBVqiU9mJ8yAr/pub?embedded=true" width="800"></iframe></div></div>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-6730641799724488242023-04-22T12:32:00.001+05:302023-04-22T12:32:09.033+05:30The concept of an Orthodox Canonical class in C++<p> </p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="364" src="https://www.youtube.com/embed/2yL3k2MGnz0" width="438" youtube-src-id="2yL3k2MGnz0"></iframe></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: large;">After the new release of C++ and the inclusion of boost's pointer, many original concepts of C++ had been overwritten. When I studied C++ back in the '90s, the orthodox canonical class formation was important to make a UDT behave as closely as possible to a built-in type. The orthodox canonical class must be having a default constructor, a virtual destructor, a copy constructor, and an assignment operator. These four building blocks must be there in a class before we can call it a canonical C++ class.</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: large;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: large;">Here I have developed an Orthodox canonical String class in its simplest form.</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: large;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: large;">I hope you will like the video of creating it.</span></div><p></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-33066164727349170062023-03-15T10:09:00.002+05:302023-03-15T10:13:56.043+05:30Pi Day and the genius mathematician from Bharat - Ramanujan<p> </p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="336" src="https://www.youtube.com/embed/RpDWHlFKHy4" width="404" youtube-src-id="RpDWHlFKHy4"></iframe></div><div class="separator" style="clear: both; text-align: center;"><br /></div><h2 style="clear: both; text-align: center;">The genius mathematician who used to dream Maths formulas</h2><div><br /></div><div><span style="font-size: large;">Ramajunan lived just 32 years - but created history in Maths which shows the brain power of Bharat.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">But again, due to colonialism, we celebrated this genius once he was celebrated abroad - in England - the same way Swami Vivekananda and Rabindranath Tagore were termed genius only after they became iconic figures abroad.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">The way Mozart used to visualize the music, this genius from Bharat used to dream of Maths.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">The first letter he wrote to a famous British Mathematician G H Hardy, contained 120 Maths formulas but no demonstration of how he got all these formulas.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">This forced the British mathematician to almost pooh-pooh Ramanujan, but in the end, even he said, all these formulas must be true - otherwise how one could ever imagine them.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Let me conclude this write-up with only one request to the Humans of Bharat - you have already wasted a very long period of time in hibernation - now it's time for you to wake up and reclaim your true spirit - the Vishwaguru Bharat.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">I am really happy to see that Baharat has come up with her own designed railways locomotive - which can make even the technologies of Japan envy us. We have come up with our indigenous Kabach - the shield - to be used in the Railways as safety norms.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">The possibilities are infinite.</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Only we have to change the mindset - we are here to contribute to the global community as Vishwaguru Bharat.</span></div><div><br /></div><div><span style="font-size: large;">This is the true nature of Bharat - let us change the global perception of Bharat</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">From the takers to the givers</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">we have a lot of stuff to give to the UNIVERSE - just as Vishwaguru Bharat</span></div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;">Jai Hind...</span></div><p></p>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0tag:blogger.com,1999:blog-3946478681282550454.post-58348877493638759082023-03-13T18:11:00.001+05:302023-03-13T18:17:15.706+05:30A new journey restarted - the beginner's step - Monte Carlo Methpd<p><span style="font-size: large;">Monte Carlo Simulation, also known as the Monte Carlo Method or a multiple probability simulation, is a mathematical technique, which is used to estimate the possible outcomes of an uncertain event. The Monte Carlo Method was invented by John von Neumann and Stanislaw Ulam during World War II to improve decision-making under uncertain conditions.</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">Unlike a normal forecasting model, Monte Carlo Simulation predicts a set of outcomes based on an estimated range of values versus a set of fixed input values.</span></p><h2 style="text-align: left;"><span style="font-size: large;">Example 1</span></h2><p><span style="font-size: large;">Problem: the probability of three heads and two tails for a coin tossed five times (assuming a fair coin)</span></p><p><span style="font-size: large;"># Answer (from Maths Probability theory) : binomial(3 + 2, 3) 2^(-(3 + 2)) = ((3 + 2)!)/(3! 2! 2^(3 + 2))= 5/16 ≈ 0.3125</span></p><h2 style="text-align: left;"><span style="font-size: large;">Example 2</span></h2><p><span style="font-size: large;">Problem: The probability of getting three consecutive Heads at the beginning if a coin is tossed 5 times</span></p><p><span style="font-size: large;"># Answer (Using Probability theory): ½ X ½ X 1/2 = ⅛ = 0.125</span></p><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">The Mathematical model of Monte Carlo written in Python</span></p><h2 style="text-align: left;"><span style="font-size: large;">Problem 1</span></h2><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; white-space: pre;"><div><span style="font-size: medium;"><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateprobability1</span>(attempts):<br /> success = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">for </span>i <span style="color: #cc7832;">in </span><span style="color: #8888c6;">range</span>(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span>attempts):<br /> <span style="color: #cc7832;">if </span>(random.randint(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>) + random.randint(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>) +<br /> random.randint(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>) + random.randint(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>)<br /> + random.randint(<br /> <span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>)) != <span style="color: #6897bb;">3</span>:<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"> else</span>:<br /> success += <span style="color: #6897bb;">1<br /></span><span style="color: #6897bb;"> </span><span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Number of attempts = "</span><span style="color: #cc7832;">, </span>attempts)<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Number of success = "</span><span style="color: #cc7832;">, </span>success)<br /><br /> <span style="color: #cc7832;">return </span>success / attempts</span></div></div><div><br /></div><div><br /></div><h2 style="text-align: left;">Problem 2</h2><div><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; white-space: pre;"><div><span style="font-size: medium;"><span style="color: #cc7832;">def </span><span style="color: #ffc66d;">calculateprobability2</span>(attempts):<br /> success = <span style="color: #6897bb;">0<br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">for </span>i <span style="color: #cc7832;">in </span><span style="color: #8888c6;">range</span>(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span>attempts):<br /> <span style="color: #cc7832;">if </span>(random.randint(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>) == <span style="color: #6897bb;">1 </span><span style="color: #cc7832;">and </span>random.randint(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>) == <span style="color: #6897bb;">1<br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">and </span>random.randint(<span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span><span style="color: #6897bb;">1</span>) == <span style="color: #6897bb;">1</span>):<br /> success += <span style="color: #6897bb;">1<br /></span><span style="color: #6897bb;"> </span><span style="color: #cc7832;">else</span>:<br /> <span style="color: #cc7832;">pass<br /></span><span style="color: #cc7832;"> </span><span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Number of attempts = "</span><span style="color: #cc7832;">, </span>attempts)<br /> <span style="color: #8888c6;">print</span>(<span style="color: #6a8759;">"Number of success = "</span><span style="color: #cc7832;">, </span>success)<br /><br /> <span style="color: #cc7832;">return </span>success / attempts</span></div></div></div><div><br /></div><div><br /></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.blogger.com/video.g?token=AD6v5dxJTidrkG6TVlyQM3bte8bNnJ56SXzRjPtg-AELXg6q3zCFyHE8451JmCcz74Qu5gBJjbKLMJw6VDK5b64phQ' class='b-hbp-video b-uploaded' frameborder='0'></iframe></div><h2 style="text-align: center;">The Monte Carlo examples implemented in Python...</h2>Somenathhttp://www.blogger.com/profile/09519633424407534012noreply@blogger.com0