tag:blogger.com,1999:blog-32234446687442415862024-03-14T12:45:03.155+05:30Mitesh Sureja's BlogSharing my knowledge, experience, learning using this Blog on various Microsoft Technologies like WPF, C#, XAML, Silverlight, LINQ, WCF, Threading, ASP.Net, SQL Server.Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.comBlogger156125tag:blogger.com,1999:blog-3223444668744241586.post-52580416546655437122019-01-27T10:24:00.001+05:302019-01-27T10:24:07.070+05:30Import-Csv and Export-Csv PowerShell cmdlets<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="MsoNormal">
Many times we need to export data in CSV files and read
specific information from it. Best example would be log files which contain
lots of data but we want to read some specific information from it like Errors,
Warnings etc.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In this small article I’ll explain how we can export data to
csv files using Export-CSV cmdlets and import csv files and read specific
information from it using Import-CSV cmdlets.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<h3 style="text-align: left;">
<b>Export-Csv</b></h3>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This command will export data into csv file. In below
example I exported latest 100 Application event log entries to logs.csv file.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-EventLog Application -Newest 100 | Export-Csv logs.csv<o:p></o:p></b></div>
<a href="https://2.bp.blogspot.com/-K6jX7WR9owo/WwlnSJ7b52I/AAAAAAAAA-Q/TNqzpiWvOY4iHMlPdTdNgEaBkvp7bymcgCLcBGAs/s1600/Import-csv-1.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="100" data-original-width="602" src="https://2.bp.blogspot.com/-K6jX7WR9owo/WwlnSJ7b52I/AAAAAAAAA-Q/TNqzpiWvOY4iHMlPdTdNgEaBkvp7bymcgCLcBGAs/s1600/Import-csv-1.png" /></a><br />
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can verify data inside csv file using below command.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Content logs.csv<o:p></o:p></b></div>
<a href="https://1.bp.blogspot.com/-nbkPvLOuDv0/WwlnSJAY6JI/AAAAAAAAA-Y/3GM5pVXNAr46IBWkLrvDIc5YAPWXm1q2wCLcBGAs/s1600/Import-csv-2.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="165" data-original-width="602" src="https://1.bp.blogspot.com/-nbkPvLOuDv0/WwlnSJAY6JI/AAAAAAAAA-Y/3GM5pVXNAr46IBWkLrvDIc5YAPWXm1q2wCLcBGAs/s1600/Import-csv-2.png" /></a><br />
<br />
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<h3 style="text-align: left;">
<b>Import-Csv</b></h3>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This command will import data from CSV file. You can specify
delimiter and headers to load only selected columns from csv file. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Script – </b><o:p></o:p></div>
<div class="MsoNormal">
<span style="background-color: white; color: darkgreen; font-family: "lucida console"; font-size: 9pt;"><br /></span></div>
<div class="MsoNormal">
<span style="background-color: white; color: darkgreen; font-family: "lucida console"; font-size: 9pt;">#specify path</span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$path</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"C:\PowerShell\logs.csv"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#import csv file and specify specific columns you want to
import using -Header </span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$file</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: blue;">Import-Csv</span> <span style="color: orangered;">$path</span>
<span style="color: navy;">-Delimiter</span> <span style="color: darkred;">","</span>
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#$file</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#loop all the rows in file</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkblue; font-family: "lucida console"; font-size: 9.0pt;">foreach</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> (<span style="color: orangered;">$row</span>
<span style="color: darkblue;">in</span> <span style="color: orangered;">$file</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: darkgreen;">#condition to
read only Errors</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: darkblue;">if</span> (<span style="color: orangered;">$row</span><span style="color: darkgrey;">.</span>EntryType
<span style="color: darkgrey;">-like</span> <span style="color: darkred;">'*Error*'</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Write-Host</span>
<span style="color: darkred;">"---------------------------------------------"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Write-Host</span>
<span style="color: orangered;">$row</span><span style="color: darkgrey;">.</span>EntryType<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Write-Host</span>
<span style="color: orangered;">$row</span><span style="color: darkgrey;">.</span>TimeGenerated<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Write-Host</span>
<span style="color: orangered;">$row</span><span style="color: darkgrey;">.</span>Message<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Write-Host</span>
<span style="color: darkred;">"---------------------------------------------"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;">}</span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal">
<b>Output – </b><o:p></o:p></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-HqF4cyj0f1Q/WwlnSJLrx1I/AAAAAAAAA-U/otsCwbSkRrouVNkZHU1IaF-EbmJaAyq5QCLcBGAs/s1600/Export-csv.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="287" data-original-width="463" src="https://2.bp.blogspot.com/-HqF4cyj0f1Q/WwlnSJLrx1I/AAAAAAAAA-U/otsCwbSkRrouVNkZHU1IaF-EbmJaAyq5QCLcBGAs/s1600/Export-csv.png" /></a></div>
<br />
You can download code from <a href="https://gist.github.com/miteshsureja/63700dd4868e7c0483a2209108a935d5">Gist</a>.<br />
<br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
Thank you for reading this article. Please leave your
feedback in comments below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Reference – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://docs.microsoft.com/en-us/powershell/">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/05/read-excel-file-data-using-powershell.html">Read
data from Excel file using PowerShell script</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/04/get-process-cmdlet-in-powershell.html">Get-Process
cmdlet in PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.com/2018/09/date-time-operations-with-powershell.html">Date Time operations with PowerShell</a></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com1tag:blogger.com,1999:blog-3223444668744241586.post-65826938778518172072018-12-18T16:54:00.001+05:302018-12-18T17:18:59.297+05:30Visitor Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
Visitor design pattern lets you to define a
new operation without changing or modifying its existing classes. Visitor
Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.</div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">When to use – </span></h3>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Visitor pattern can use when we want to
perform an operation on similar kind of objects. Using visitor pattern we can add
new functionality to the original objects or hierarchy or objects via Visitor
subclass. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Visitor pattern consist of main two
methods. One method is Visit() which is called for every element in the object
hierarchy. Second method is Accept() which is implemented by the class who is
visiting object hierarchy and accepted by visitors.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">Major components of Visitor pattern – </span></h3>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">Visitor –</span></b><span lang="EN-US"> This
is an interface declares Visit operation.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Concrete Visitor –</span></b><span lang="EN-US"> This class implements Visitor interface and implements visit
operation. <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Element –</span></b><span lang="EN-US"> This
is an interface declares the Accept operation which takes visitor as an argument.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Concrete Element –</span></b><span lang="EN-US"> This class implements Element interface and implements Accept operation.
<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Client –</span></b><span lang="EN-US"> Client
is an entry point class that has access to data structure objects and process
elements to visit object hierarchy.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s have a look on below example of Visitor
design pattern.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Code – </b><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">enum</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Food,<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Oil,<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Electronics,<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Cloths<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//element</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IElement</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Accept(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> visitor);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete element</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Products</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IElement</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Accept(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> visitor)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Name - {0}, Product Visited - {1}, Category -
{2}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>visitor.Name,
visitor.ProductVisited, visitor.Category);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete element</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Discounts</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IElement</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Accept(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> visitor)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> discount = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">switch</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (visitor.Category)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">case</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Food:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>discount = 10;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">break</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">case</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Electronics:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>discount = 20;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">break</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">case</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Cloths:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>discount = 15;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">break</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">default</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>discount = 5;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">break</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Product Name - {0}, Price - {1}, Discount
({2}%) - {3}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>visitor.ProductVisited,
visitor.Price, discount, (visitor.Price * discount)/100);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//visitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ProductVisited { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Price { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Category { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Visit(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IElement</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> element);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete visitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">GroceriesVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ProductVisited { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Price { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Category { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> GroceriesVisitor(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> productVisited, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> category )<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>ProductVisited = productVisited;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Price = price;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Category = category;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Visit(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IElement</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> element)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>element.Accept(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete visitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">AppliancesVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ProductVisited { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Price { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Category { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> AppliancesVisitor(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> productVisited, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> category)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>ProductVisited = productVisited;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Price = price;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Category = category;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Visit(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IElement</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> element)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>element.Accept(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete visitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FashionVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ProductVisited { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Price { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Category { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> FashionVisitor(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> productVisited, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> category)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>ProductVisited = productVisited;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Price = price;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Category = category;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Visit(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IElement</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> element)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>element.Accept(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//entry point</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> Items = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Items.Add(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">GroceriesVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Visitor1"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Rice"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 150, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Food));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Items.Add(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">GroceriesVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Visitor2"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Oil"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 95, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Oil));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Items.Add(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">AppliancesVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Visitor3"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Speakers"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 500, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Electronics));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Items.Add(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">AppliancesVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Visitor4"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Cameras"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 500, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Electronics));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Items.Add(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FashionVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Visitor5"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Shirts"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 1500, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Cloths));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Items.Add(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FashionVisitor</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Visitor6"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Jeans"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 3000, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Categories</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Cloths));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"-----Visiting prices of all items-----"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> item </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Items)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>item.Visit(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Products</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"-----Visiting discounts of all
items-----"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> item </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Items)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>item.Visit(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Discounts</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Output – </b><o:p></o:p></span></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-FwulzXM_z34/WyS1lMKznEI/AAAAAAAABAg/Oxs_Rtx_cqcMZldbEItpCsWr_7Y_AtJdACLcBGAs/s1600/Visitor%2BDesign%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="338" data-original-width="624" src="https://2.bp.blogspot.com/-FwulzXM_z34/WyS1lMKznEI/AAAAAAAABAg/Oxs_Rtx_cqcMZldbEItpCsWr_7Y_AtJdACLcBGAs/s1600/Visitor%2BDesign%2BPattern.png" /></a></div>
<br />
As per above example, there are many
visitor and elements defined. Different type of visitors visits various products.
In this example Product object allows different visitor to visit product
related information like Product Name, category etc. where Discount object
allows different visitor to visit discounts available on various categories of
products.<br />
<br />
<div class="MsoNormal">
<span lang="EN-US">You can download full code from <a href="https://gist.github.com/miteshsureja/d4093c1c8a24bb348f06f2a15ac68a3b">Gist</a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">I hope this article helps you to know more
about Visitor Design Pattern. Please leave your feedback in comments section
below.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">References – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.dofactory.com/net/visitor-design-pattern">DoFactory</a><o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">See Also –</span></b><br />
<b><span lang="EN-US"><br /></span></b>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
<b><span lang="EN-US"><br /></span></b>
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-13548725973250543962018-12-16T10:56:00.000+05:302018-12-18T17:19:52.074+05:30Template Method Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span lang="EN-US">Template Method design pattern to define an
algorithm as skeleton of operations and leave the details to be implemented by
child classes. Template Method Pattern falls under behavioral pattern of GOF
(Gang of Four) patterns.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">When to use – </span></h3>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Template Method pattern used to define
basic steps or structure of an algorithm and allow the implementation of each
steps in derived class. Template method pattern has two components. One is an
abstract class and one or more concrete classes. An abstract class is the
Template class, which defines algorithmic steps and structure and preserves it
across multiple implementations. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">Major components of Template Method pattern
– </span></h3>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Abstract Class –</span></b><span lang="EN-US"> This is an abstract class contains template method and abstract
operations. <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Concrete Class –</span></b><span lang="EN-US"> This class inherits Abstract class and overrides abstract methods. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s have a look on below example of Template
Method design pattern.<o:p></o:p></span></div>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b>Code –</b></span><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Template abstract class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OrderTemplate</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;">
SelectProduct();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Payment();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deliver();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> ProcessOrder()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> SelectProduct();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Payment();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Deliver();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete classe</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OnLineOrder</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OrderTemplate</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deliver()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Product shipped via courier"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Payment()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Online payment done successfully"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;">
SelectProduct()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Product added to cart successfully"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">StoreOrder</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OrderTemplate</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deliver()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Product delivered to customer at
counter"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Payment()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Paid at counter"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> SelectProduct()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Product is selected by customer from rack in
store"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//entry point</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"----------- Online Order Process
-----------"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OrderTemplate</span><span style="font-family: "consolas"; font-size: 9.5pt;"> onlineOrder = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OnLineOrder</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> onlineOrder.ProcessOrder();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"----------- Retail Store Order Process
-----------"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OrderTemplate</span><span style="font-family: "consolas"; font-size: 9.5pt;"> storeOrder = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">StoreOrder</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> storeOrder.ProcessOrder();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b>Output –</b></span><br />
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-f5vJ13bejik/WvhK858RDPI/AAAAAAAAA9A/ySEJvK1cf20Ey0j-zClN-5oCgP5FIjjogCLcBGAs/s1600/Template%2BMethod%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="257" data-original-width="564" src="https://3.bp.blogspot.com/-f5vJ13bejik/WvhK858RDPI/AAAAAAAAA9A/ySEJvK1cf20Ey0j-zClN-5oCgP5FIjjogCLcBGAs/s1600/Template%2BMethod%2BPattern.png" /></a></div>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
As per above example, OrderTemplate abstract
class contains SelectPrduct, Payment and Deliver methods. ProcessOrder method
is Tempate method that calls these methods in sequence. OnLineOrder and
StoreOrder classes derived from OrderTemplate abstract class and overrides
abstracts methods differently in each class.<br />
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">You can download full code from <a href="https://gist.github.com/miteshsureja/11ffe894e8777aba03f9c15553ba2ddb">Gist</a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">I hope this article helps you to know more
about Template Method Design Pattern. Please leave your feedback in comments
section below.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">References – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.dofactory.com/net/template-method-design-pattern">DoFactory</a><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span lang="EN-US">See Also – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-79756483509025384562018-09-22T18:45:00.001+05:302018-09-22T18:45:41.857+05:30Date Time operations with PowerShell<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
In this article, I’ll go through various date time operations you
can perform using PowerShell.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;">Get-Date</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use Get-Date cmdlet to get today’s date.<o:p></o:p></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-lSdYWor-5EQ/WplbWLPFPoI/AAAAAAAAA5I/5I0elNq7G7IUJdqZLob0cgZsiksc7dc0ACLcBGAs/s1600/PowerShell58.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="143" data-original-width="602" height="152" src="https://4.bp.blogspot.com/-lSdYWor-5EQ/WplbWLPFPoI/AAAAAAAAA5I/5I0elNq7G7IUJdqZLob0cgZsiksc7dc0ACLcBGAs/s640/PowerShell58.png" width="640" /></a></div>
<br />
<br />
<div class="MsoNormal">
Below commands can be used to display only Date or Time.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Get-Date -DisplayHint Time</b></div>
<div class="MsoNormal">
<b>Get-Date -DisplayHint Date</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<a href="https://2.bp.blogspot.com/-Xnl0JlvLSMU/WplbV3zNj8I/AAAAAAAAA5E/w_Zr-9Z5f-Ixzr2-RdnWf_hrjA7KingJQCEwYBhgL/s1600/PowerShell59.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="168" data-original-width="602" height="178" src="https://2.bp.blogspot.com/-Xnl0JlvLSMU/WplbV3zNj8I/AAAAAAAAA5E/w_Zr-9Z5f-Ixzr2-RdnWf_hrjA7KingJQCEwYBhgL/s640/PowerShell59.png" width="640" /></a><br />
<br />
You can also specify format to display date and time. You
can use –Format parameter with Get-Date and specify ‘G’ format specifier to get
General short date and short time.<br />
<b><br /></b>
<b>Get-Date -Format G</b><br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<a href="https://1.bp.blogspot.com/-Hqb4DQ-SZiM/WplbWJCEK1I/AAAAAAAAA5M/irnTfkN-4UcHrbj1-8btTP2JWE0b6psMQCEwYBhgL/s1600/PowerShell60.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="90" data-original-width="602" height="94" src="https://1.bp.blogspot.com/-Hqb4DQ-SZiM/WplbWJCEK1I/AAAAAAAAA5M/irnTfkN-4UcHrbj1-8btTP2JWE0b6psMQCEwYBhgL/s640/PowerShell60.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use –UFormat parameter to specify custom format of date time.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Date -UFormat "%d, %B, %Y, %A %r %t"</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<a href="https://2.bp.blogspot.com/-GDMBoiCWJSo/WplbW19peKI/AAAAAAAAA5Q/sIe4lt4iIHoafRnVXnNvXe5Y9WiRC3smQCEwYBhgL/s1600/PowerShell61.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="121" data-original-width="602" height="128" src="https://2.bp.blogspot.com/-GDMBoiCWJSo/WplbW19peKI/AAAAAAAAA5Q/sIe4lt4iIHoafRnVXnNvXe5Y9WiRC3smQCEwYBhgL/s640/PowerShell61.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use foreach with get-date to get format as per your
need.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Date -Format F</b></div>
<div class="MsoNormal">
<b>Get-Date -Format F | foreach {$_ -replace ":",
"-"}</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<a href="https://4.bp.blogspot.com/-KCrYKc_tBDI/WplbXIgaIpI/AAAAAAAAA5U/xcxwzg7RdTgNefTgyKHvZXjRbUsPf3JtQCEwYBhgL/s1600/PowerShell62.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="121" data-original-width="602" height="128" src="https://4.bp.blogspot.com/-KCrYKc_tBDI/WplbXIgaIpI/AAAAAAAAA5U/xcxwzg7RdTgNefTgyKHvZXjRbUsPf3JtQCEwYBhgL/s640/PowerShell62.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can find for all the properties of DateTime using
Get-Member cmdlets.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Date | Get-Member -MemberType Properties</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<a href="https://1.bp.blogspot.com/-f7KmT4wiePk/WplbXczx6EI/AAAAAAAAA5Y/Ldo3Mna6a6weChi5py0YdHfNXo70nkh-gCEwYBhgL/s1600/PowerShell63.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="280" data-original-width="602" height="296" src="https://1.bp.blogspot.com/-f7KmT4wiePk/WplbXczx6EI/AAAAAAAAA5Y/Ldo3Mna6a6weChi5py0YdHfNXo70nkh-gCEwYBhgL/s640/PowerShell63.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can get date time values as per your need using properties
and methods of Date Time object.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Date).TimeOfDay</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-GLF3mayOO8M/WplbXy5u39I/AAAAAAAAA5c/SLKnLqAvwLUuYZDXHHkoBKAkIQC595lRACEwYBhgL/s1600/PowerShell64.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="199" data-original-width="602" height="210" src="https://1.bp.blogspot.com/-GLF3mayOO8M/WplbXy5u39I/AAAAAAAAA5c/SLKnLqAvwLUuYZDXHHkoBKAkIQC595lRACEwYBhgL/s640/PowerShell64.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Below are some output of Get-Date properties and methods can
be used as per your requirements.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Properties/Methods<o:p></o:p></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Output<o:p></o:p></b></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).Date</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02 March 2018 00:00:00</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).Day</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
2</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).DayOfWeek</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Friday</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).DayOfYear</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
61</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).Hour</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
18</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).Minute</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
48</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).Month</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
3</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).Second</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
5</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).Millisecond</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
227</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).IsDaylightSavingTime()</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
False</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).ToShortDateString()</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02-03-2018</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).ToShortTimeString()</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
06:54 PM</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).ToUniversalTime()</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02 March 2018 13:26:17</div>
</td>
</tr>
</tbody></table>
</div>
<br />
<div class="MsoNormal">
You can add or remove date or time to current display date time
using below various methods.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Methods<o:p></o:p></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Output<o:p></o:p></b></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).AddDays(10)<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
12 March 2018 19:01:09<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).AddDays(-10)<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
20 February 2018 19:01:52<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).AddYears(2)<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02 March 2020 19:07:18<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).AddMonths(3)<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02 June 2018 19:04:48<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).AddHours(3)<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02 March 2018 22:03:48<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).AddMinutes(23)</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02 March 2018 19:28:54</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 175.5pt;" valign="top" width="234"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
(Get-Date).AddSeconds(40)</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 286.6pt;" valign="top" width="382"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
02 March 2018 19:06:58</div>
</td>
</tr>
</tbody></table>
</div>
<div>
<br /></div>
<div class="MsoNormal">
You can find difference between two dates using below
command.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>(Get-Date).DayOfYear - (Get-Date -year 2018 -month 11 -date
20).DayOfYear</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-GBcsyYDHiFI/WplbYJ6gZ2I/AAAAAAAAA5g/PVIImS7tFHgVSQGMCkRs8-iPOHDecWfkgCEwYBhgL/s1600/PowerShell65.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="123" data-original-width="602" height="130" src="https://1.bp.blogspot.com/-GBcsyYDHiFI/WplbYJ6gZ2I/AAAAAAAAA5g/PVIImS7tFHgVSQGMCkRs8-iPOHDecWfkgCEwYBhgL/s640/PowerShell65.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can also create New-TimeSpan object to represent date at
some interval of days or time. You can also check date time of that timespan.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>(Get-Date) - (New-TimeSpan -Days 20)</b></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<b>(Get-Date) - (New-TimeSpan -Days -20)</b><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://2.bp.blogspot.com/-434-eow1SyI/WplbYbOmx3I/AAAAAAAAA5k/_3ZSBpV2BAsTDy6Iy-u0EPvFfa7nvNZ5ACEwYBhgL/s1600/PowerShell66.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="128" data-original-width="602" height="134" src="https://2.bp.blogspot.com/-434-eow1SyI/WplbYbOmx3I/AAAAAAAAA5k/_3ZSBpV2BAsTDy6Iy-u0EPvFfa7nvNZ5ACEwYBhgL/s640/PowerShell66.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;">Set-Date</span></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can change system date and time using Set-Date cmdlet.
You can specify new date and time to Set-Date and it will change your system
date and time. You need administrator privileges to execute Set-Date command.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Set-Date -Date (Get-Date).AddDays(5)</b></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<b>Set-Date -Date (Get-Date).AddDays(-5)<o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://1.bp.blogspot.com/-R7T2r4u9ztQ/WplbZLbJ9wI/AAAAAAAAA5o/eX0F6xVwTec_OJyjtULuEIkE60solBg-wCEwYBhgL/s1600/PowerShell67.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="149" data-original-width="602" height="158" src="https://1.bp.blogspot.com/-R7T2r4u9ztQ/WplbZLbJ9wI/AAAAAAAAA5o/eX0F6xVwTec_OJyjtULuEIkE60solBg-wCEwYBhgL/s640/PowerShell67.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope you have now some knowledge about some basic date and
time operations that you can perform in PowerShell. Thank you for reading this
article. Please leave your feedback in comments below.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Reference – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/default.aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/02/introduction-to-windows-powershell.html">Introduction
to Windows PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.com/2018/03/working-with-files-and-folders-using.html">Working with File and Folders using PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.com/2018/03/advanced-files-and-folders-techniques.html">Advanced Files and Folders techniques with PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.com/2018/04/get-process-cmdlet-in-powershell.html">Working with Processes using PowerShell</a></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-47713347952969609132018-08-25T18:57:00.000+05:302018-12-18T17:20:09.964+05:30Strategy Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span lang="EN-US">Strategy design pattern enables selecting
strategy at runtime. Strategy Pattern falls under behavioral pattern of GOF
(Gang of Four) patterns.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
When to use –</h3>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Strategy pattern used to separate different
types of algorithms/processes that shares similar purpose. This decouples code
into multiple classes and promotes reusability. Strategy pattern allows client
to choose strategy/algorithm at runtime that provides more flexibility and
reusability. Strategy pattern is one of the easiest pattern to implement.</div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">Major components of Strategy pattern – </span></h3>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">Context –</span></b><span lang="EN-US"> Context
class maintains reference to Strategy object and provides an interface to set
Strategy dynamically.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Strategy –</span></b><span lang="EN-US">
This is an interface and used by Context object. <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Concrete Strategy –</span></b><span lang="EN-US"> This class implements Strategy interface. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s have a look on below example of Strategy
design pattern.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b>Code –</b></span><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//strategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IPaymentStrategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Pay(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete strategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Cash</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IPaymentStrategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Pay(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} paid in cash."</span><span style="font-family: "consolas"; font-size: 9.5pt;">, amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete strategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">CreditCard</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IPaymentStrategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Pay(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} paid via Credit Card"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete strategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DebitCard</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IPaymentStrategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Pay(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} paid via Debit Card"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//context</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Customer</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IPaymentStrategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Strategy { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Customer(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IPaymentStrategy</span><span style="font-family: "consolas"; font-size: 9.5pt;"> strategy)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Strategy = strategy;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Payment(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Strategy.Pay(amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//entry point</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Customer</span><span style="font-family: "consolas"; font-size: 9.5pt;"> customer = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Customer</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">CreditCard</span><span style="font-family: "consolas"; font-size: 9.5pt;">());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} Current Strategy {1} {2}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">,10),<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Strategy.GetType().Name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 10));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Payment(200);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Strategy = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Cash</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} Current Strategy {1} {2}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 10), <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Strategy.GetType().Name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 10));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Payment(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Strategy = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DebitCard</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} Current Strategy {1} {2}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 10), <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Strategy.GetType().Name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 10));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> customer.Payment(500);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b>Output –</b></span><br />
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-KW8XGWvtqpk/Wvg32xlSLVI/AAAAAAAAA8k/gn8S8HlQksgHUmteQcu4nnJFeL-L_eWRACLcBGAs/s1600/Strategy%2BDesign%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="252" data-original-width="539" src="https://2.bp.blogspot.com/-KW8XGWvtqpk/Wvg32xlSLVI/AAAAAAAAA8k/gn8S8HlQksgHUmteQcu4nnJFeL-L_eWRACLcBGAs/s1600/Strategy%2BDesign%2BPattern.png" /></a></div>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<span lang="EN-US" style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<br />
<div class="MsoNormal">
<span lang="EN-US">In above example, customer wants to pay some
amount and decides between various payments strategies at runtime. There are
three payment strategies Cash, CreditCard, DebitCard available for payment. Initially
Customer object created with CreditCard payment strategy and later on payment strategy
keeps changing as per customer’s decision.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">You can download full code from <a href="https://gist.github.com/miteshsureja/e2601d8c5af7c41d090a7fcd0fc79d12">Gist</a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">I hope this article helps you to know more
about Strategy Design Pattern. Please leave your feedback in comments section
below.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">References – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.dofactory.com/net/strategy-design-pattern">DoFactory</a><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span lang="EN-US">See Also – <o:p></o:p></span></b><br />
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 6;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 7;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 8;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 9;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 10;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 11; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-88006749713141600812018-07-29T18:48:00.000+05:302018-07-29T18:48:45.299+05:30How to execute PowerShell script or cmdlets from C# code?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
Sometimes you need to execute PowerShell script or
commands from C# code. In this article I’ll explain various methods to execute
PowerShell scripts and commands from C# code.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can execute PowerShell scripts using PowerShell object
available in ‘System.Management.Automation’ namespace. This assembly is
available in Nuget for download. You can create PowerShell instance and assign
script file or command which you would like to execute. You can also get output
from PowerShell command after execution and read data from PSObject object. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Let’s have look on below example which executes PowerShell
scripts from C# code.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System;</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System.Management.Automation;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System.Collections.ObjectModel;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PowerShell</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> PowerShellInst = </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PowerShell</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Create())<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span></b><b><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span></b><b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> criteria = </span></b><b><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"system*"</span></b><b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>PowerShellInst.AddScript(</span></b><b><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Get-Service
-DisplayName "</span></b><b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> + criteria);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Collection</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PSObject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> PSOutput = <b>PowerShellInst.Invoke();<o:p></o:p></b></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PSObject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> obj </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> PSOutput)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (obj != </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Write(obj.Properties[</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Status"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">].Value.ToString() + </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">" -
"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(obj.Properties[</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"DisplayName"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">].Value.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Done"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Output –</b></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-zbo-AwAUTvM/WxP6V2c9ZoI/AAAAAAAAA_4/wwl6N-nHgI4Ma0wKXHhDQk2EE2rkLorXwCLcBGAs/s1600/PowerShell68.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="222" data-original-width="591" src="https://3.bp.blogspot.com/-zbo-AwAUTvM/WxP6V2c9ZoI/AAAAAAAAA_4/wwl6N-nHgI4Ma0wKXHhDQk2EE2rkLorXwCLcBGAs/s1600/PowerShell68.png" /></a></div>
<br />
Now I want to execute below PowerShell script file using C#
code.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-0PiUCSSnVuY/WxP6V6MaUII/AAAAAAAAA_0/XtzHklpIpro3xeTsGZ6cvsxKBtBBYTY-ACLcBGAs/s1600/PowerShell69.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="137" data-original-width="602" src="https://3.bp.blogspot.com/-0PiUCSSnVuY/WxP6V6MaUII/AAAAAAAAA_0/XtzHklpIpro3xeTsGZ6cvsxKBtBBYTY-ACLcBGAs/s1600/PowerShell69.png" /></a></div>
<b>Code –</b><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System;</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System.Management.Automation;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System.Collections.ObjectModel;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Execute PS1
(PowerShell script) file</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PowerShell</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> PowerShellInst = </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PowerShell</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Create())<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> path = <b>System.IO.</b></span><b><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Path</span></b><b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.GetDirectoryName(</span></b><b><span style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">@"C:\Temp\"</span></b><b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">) + </span></b><b><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"\\Get-EventLog.ps1"</span></b><b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (!</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.IsNullOrEmpty(path))<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><b>PowerShellInst.AddScript(System.IO.</b></span><b><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">File</span></b><b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.ReadAllText(path));<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Collection</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PSObject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> PSOutput = <b>PowerShellInst.Invoke();<o:p></o:p></b></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">PSObject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> obj </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> PSOutput)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (obj != </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Write(obj.Properties[</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"EntryType"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">].Value.ToString() + </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">" -
"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Write(obj.Properties[</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Source"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">].Value.ToString() + </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">" -
"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(obj.Properties[</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Message"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">].Value.ToString() + </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">" -
"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Done"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Output –</b><a href="https://2.bp.blogspot.com/-yNSBpT_cLU4/WxP6V8ptJyI/AAAAAAAAA_w/NdRqPJcOZRQ5mzBufEYAlx19FCaAocCBwCLcBGAs/s1600/PowerShell70.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="141" data-original-width="602" src="https://2.bp.blogspot.com/-yNSBpT_cLU4/WxP6V8ptJyI/AAAAAAAAA_w/NdRqPJcOZRQ5mzBufEYAlx19FCaAocCBwCLcBGAs/s1600/PowerShell70.png" /></a></div>
<br />
<b>Command Prompt - </b>You can execute PowerShell scripts and command using
PowerShell.exe like below in command prompt.<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-wb-e5Sm_-rk/WxP6XPxCh9I/AAAAAAAAA_8/72pMoerE7JQi8DLL573QPfDKk45l1cdfACLcBGAs/s1600/PowerShell71.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="217" data-original-width="602" src="https://4.bp.blogspot.com/-wb-e5Sm_-rk/WxP6XPxCh9I/AAAAAAAAA_8/72pMoerE7JQi8DLL573QPfDKk45l1cdfACLcBGAs/s1600/PowerShell71.png" /></a></div>
Similarly you can invoke PowerShell process from C# code and
pass command as argument. See below example how execute PowerShell process from
code and pass cmdlet as argument.<br />
<b><br /></b>
<b>Code –</b><br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System;</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System.Management.Automation;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> System.Collections.ObjectModel;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//execute
powershell cmdlets or scripts using command arguments as process</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ProcessStartInfo</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> processInfo = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ProcessStartInfo</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>processInfo.FileName = </span><span style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">@"powershell.exe"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//execute powershell
script using script file</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//processInfo.Arguments
= @"& {c:\temp\Get-EventLog.ps1}";</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//execute
powershell command</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>processInfo.Arguments = </span><span style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">@"& {Get-EventLog -LogName Application -Newest 10
-EntryType Information | Select EntryType, Message}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>processInfo.RedirectStandardError = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">true</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>processInfo.RedirectStandardOutput = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">true</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>processInfo.UseShellExecute = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">false</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>processInfo.CreateNoWindow = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">true</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//start
powershell process using process start info</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Process</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> process = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Process</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>process.StartInfo = processInfo;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>process.Start();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>//read output</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Output - {0}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, process.StandardOutput.ReadToEnd());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>//read errors</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Errors - {0}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, process.StandardError.ReadToEnd());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Output –</b></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-xvJhWgtmoZw/WxP6XmGxKPI/AAAAAAAABAA/8jRCt3lcfvoGhcu8i2JWtwjHUEzVJZbdACLcBGAs/s1600/PowerShell72.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="171" data-original-width="602" src="https://3.bp.blogspot.com/-xvJhWgtmoZw/WxP6XmGxKPI/AAAAAAAABAA/8jRCt3lcfvoGhcu8i2JWtwjHUEzVJZbdACLcBGAs/s1600/PowerShell72.png" /></a></div>
<br />
You can download code from <a href="https://gist.github.com/miteshsureja/f9cbc2f09264a01277a6555a7425debc">Gist</a>.<br />
<br />
Thank you for reading this article. Please leave your
feedback in comments below.<br />
<b><br /></b>
<b>Reference –</b><br />
<div class="MsoNormal">
<a href="https://docs.microsoft.com/en-us/powershell/">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/05/read-excel-file-data-using-powershell.html">Read
data from Excel file using PowerShell script</a></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/04/get-process-cmdlet-in-powershell.html">Get-Process
cmdlet in PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.com/2018/03/advanced-files-and-folders-techniques.html">Advanced Files and Folders techniques with PowerShell</a></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-32014385779073402942018-07-08T11:01:00.000+05:302018-12-18T17:20:37.651+05:30State Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="MsoNormal">
<span lang="EN-US">State design pattern is used to modify
behavior of an object whenever internal state changes. State Pattern falls
under behavioral pattern of GOF (Gang of Four) patterns.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">When to use – </span></h3>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">State pattern used when state of an object
changes based on some action or condition at run-time without changing the
interface. State pattern used to alter the behavior of an object as its
internal state changes. This pattern used for some complex decision making
program which represents multiple states. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">Major components of State pattern – </span></h3>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">Context –</span></b><span lang="EN-US"> The
clients of the State design pattern use the context class directly. Clients do
not have access to state objects. Context class holds State objects that
changes behavior according to its state. <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">State –</span></b><span lang="EN-US"> This
is an abstract class. <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Concrete State –</span></b><span lang="EN-US"> This class inherited from State class. This class provides real
functionality that used by Context object. This class provides behavior to
check and change state based on condition/action. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s have a look on below example of State
design pattern.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Code – </b><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//state</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BankAccount</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Account { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Balance { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deposit(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Withdraw(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete state</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Normal</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Normal(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"> state)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance = state.Balance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account = state.Account;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Normal(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> balance, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BankAccount</span><span style="font-family: "consolas"; font-size: 9.5pt;"> account)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance = balance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account = account;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deposit(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance += amount;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Withdraw(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance -= amount;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (Balance > 1000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account.State = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Classic</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete state</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Classic</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Classic(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"> state)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance = state.Balance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account = state.Account;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Classic(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> balance, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BankAccount</span><span style="font-family: "consolas"; font-size: 9.5pt;"> account)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance = balance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account = account;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deposit(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance += amount;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Withdraw(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance -= amount;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (Balance < 1000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account.State = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Normal</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (Balance
> 2000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account.State = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Platinum</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete state</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Platinum</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Platinum(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"> state)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance = state.Balance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account = state.Account;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Platinum(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> balance, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BankAccount</span><span style="font-family: "consolas"; font-size: 9.5pt;"> account)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance = balance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account = account;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deposit(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance += amount;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Withdraw(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Balance -= amount;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> CheckState()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (Balance < 2000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account.State = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Classic</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (Balance
< 1000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Account.State = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Normal</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//context</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BankAccount</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">State</span><span style="font-family: "consolas"; font-size: 9.5pt;"> State { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> BankAccount(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> name)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> State = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Normal</span><span style="font-family: "consolas"; font-size: 9.5pt;">(0, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Balance<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;"> { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;">
State.Balance; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Deposit(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> State.Deposit(amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Deposited -
{0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Balance - {0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, Balance);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Account Status - {0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">,
State.GetType().Name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 50));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Withdraw(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="font-family: "consolas"; font-size: 9.5pt;"> amount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> State.Withdraw(amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Withdrawn - {0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, amount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Balance - {0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, Balance);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Account Status - {0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">,
State.GetType().Name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 50));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//entry point</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BankAccount</span><span style="font-family: "consolas"; font-size: 9.5pt;"> account = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BankAccount</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Mitesh
Sureja"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> account.Deposit(500);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> account.Deposit(600);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> account.Deposit(1000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> account.Withdraw(500);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> account.Withdraw(1500);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
</div>
<div class="MsoNormal">
<span lang="EN-US"><b>Output –</b></span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-Tkkv3r7pnSI/Wu71rn4fWOI/AAAAAAAAA7o/DrPkHKbm_2EgOs5xt0TwzEuJKW0jEbAEACEwYBhgL/s1600/State%2BDesign%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="475" data-original-width="578" height="524" src="https://3.bp.blogspot.com/-Tkkv3r7pnSI/Wu71rn4fWOI/AAAAAAAAA7o/DrPkHKbm_2EgOs5xt0TwzEuJKW0jEbAEACEwYBhgL/s640/State%2BDesign%2BPattern.png" width="640" /></a></div>
<br />
Above example demonstrates change in bank
account status based on its remaining balance. In this example, BankAccount
class is context class and Normal, Classic, Platinum are various classes of
concrete state. The CheckState method of concrete state classes responsible to
check condition and change states accordingly on each action like Deposit or
Withdraw. Based on available balance in bank account, the status automatically changes
between Normal, Classic and Platinum.<br />
<br />
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">You can download full code from <a href="https://gist.github.com/miteshsureja/cccb5aab4e07a049bd93028c76d8a22a">Gist</a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">I hope this article helps you to know more
about State Design Pattern. Please leave your feedback in comments section
below.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">References – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.dofactory.com/net/state-design-pattern">DoFactory</a><o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">See Also – <o:p></o:p></span></b><br />
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
<br /></div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-77789555916899103352018-06-16T11:24:00.000+05:302018-06-16T11:24:26.655+05:30How to make sure only single instance of your application is running?<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">There are many ways to keep running single instance of your application. In this article i'll explain how you can use Mutex and GetProcesses to keep single instance of your application.</span><br />
<div class="MsoNormal">
<span style="font-family: inherit;"><span style="mso-ansi-language: EN-IN;"><br /></span>
</span><br />
<h3 style="text-align: left;">
<span style="font-family: inherit;">
Mutex </span></h3>
<span style="font-family: inherit;"><br /></span>
<div class="MsoNormal" style="color: black; font-size: medium; font-style: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
</div>
<span style="font-family: inherit;"><br /></span>
<div class="MsoNormal" style="color: black; font-size: medium; font-style: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<div style="margin: 0px;">
<span style="font-family: inherit;"><span style="mso-ansi-language: EN-IN;">Mutex is threading synchronisation mechanism. Named mutex works across multiple applications. I've explained more about </span><a href="http://miteshsureja.blogspot.in/2011/12/mutex-threading.html">Mutex</a> in my previous article under Threading. Below example demonstrated how mutex is used to keep single instance of your console application.</span></div>
</div>
<br /></div>
<div class="MsoNormal">
<span style="mso-ansi-language: EN-IN;"><b>Code – </b><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">bool</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> isFirstInstance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Mutex</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> mutex = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Mutex</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">true</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"MyApp"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">out</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> isFirstInstance))<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (isFirstInstance)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Welcome, Running first instance of MyApp"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"One instance of MyApp is already running."</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">}</span><span style="mso-ansi-language: EN-IN;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="mso-ansi-language: EN-IN;"><b><br /></b></span></div>
<div class="MsoNormal">
<span style="mso-ansi-language: EN-IN;"><b>Output – </b><o:p></o:p></span><br />
<span style="mso-ansi-language: EN-IN;"><b><br /></b></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-Bvj0qaU1b8o/Wwlj7lT9NQI/AAAAAAAAA9k/8qWvpkgAxhYPt-3V3CqR8UoJx0S1CEEKgCLcBGAs/s1600/ConsoleSingleInstance1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="160" data-original-width="624" src="https://1.bp.blogspot.com/-Bvj0qaU1b8o/Wwlj7lT9NQI/AAAAAAAAA9k/8qWvpkgAxhYPt-3V3CqR8UoJx0S1CEEKgCLcBGAs/s1600/ConsoleSingleInstance1.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-tmpPXr7BjWw/Wwlj7uhQYeI/AAAAAAAAA9g/OC0kuR9C0gU3casEv5r1vUXdfFWbw2y2QCLcBGAs/s1600/ConsoleSingleInstance2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="180" data-original-width="624" src="https://3.bp.blogspot.com/-tmpPXr7BjWw/Wwlj7uhQYeI/AAAAAAAAA9g/OC0kuR9C0gU3casEv5r1vUXdfFWbw2y2QCLcBGAs/s1600/ConsoleSingleInstance2.png" /></a></div>
<br />
Similar code you can
do for WPF application.<br />
<div class="MsoNormal">
<span style="mso-ansi-language: EN-IN;"><br /></span></div>
<div class="MsoNormal">
<span style="mso-ansi-language: EN-IN;"><b>Code – </b><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">partial</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">App</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Application</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Mutex</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> mutex = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">protected</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> OnStartup(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">StartupEventArgs</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">bool</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> isFirstInstance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>mutex = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Mutex</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">true</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"MyWPFApp"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">out</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> isFirstInstance);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (isFirstInstance)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MessageBox</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Show(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Welcome,
Running first instance of MyWPFApp"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Info"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MainWindow</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> window = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MainWindow</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>window.Show();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MessageBox</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Show(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"One
instance of MyWPFApp is already running."</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Info"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Application</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Current.Shutdown();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">base</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.OnStartup(e);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<b>Output –</b></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-yprO8v40Vf8/WwlkCd1o76I/AAAAAAAAA9o/ihu1Fe_xYMQmEVFF7eEfaApyTntReRvmgCLcBGAs/s1600/WPFSingleInstance1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="133" data-original-width="286" src="https://3.bp.blogspot.com/-yprO8v40Vf8/WwlkCd1o76I/AAAAAAAAA9o/ihu1Fe_xYMQmEVFF7eEfaApyTntReRvmgCLcBGAs/s1600/WPFSingleInstance1.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-893Al3sdqVA/WwlkCnOQ2JI/AAAAAAAAA9s/LwNNv_pla1IIUNc_iE86yc6srHpylGq6gCLcBGAs/s1600/WPFSingleInstance2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="133" data-original-width="286" src="https://3.bp.blogspot.com/-893Al3sdqVA/WwlkCnOQ2JI/AAAAAAAAA9s/LwNNv_pla1IIUNc_iE86yc6srHpylGq6gCLcBGAs/s1600/WPFSingleInstance2.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<h3>
GetProcesses </h3>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
One more way to check single instance of your running application using Get Processes. See
below example.<o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//get process count of your application</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (System.Diagnostics.</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Process</span><span style="font-family: "consolas"; font-size: 9.5pt;">.GetProcessesByName(<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
System.IO.</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Path</span><span style="font-family: "consolas"; font-size: 9.5pt;">.GetFileNameWithoutExtension(<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
System.Reflection.</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Assembly</span><span style="font-family: "consolas"; font-size: 9.5pt;">.GetEntryAssembly().Location)).Count()
> 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"One instance is already running so closing this instance"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
System.Diagnostics.</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Process</span><span style="font-family: "consolas"; font-size: 9.5pt;">.GetCurrentProcess().Kill();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Welcome, Running first instance."</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<span style="text-align: justify;">
</span><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<span style="text-align: justify;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-Al0X5oOWm84/Wwqs8_NLlFI/AAAAAAAAA_U/_pl8_Jm_q44Jwv5cAM55-0k2HsZJ8zcdACLcBGAs/s1600/SingleProcess1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="199" data-original-width="528" src="https://2.bp.blogspot.com/-Al0X5oOWm84/Wwqs8_NLlFI/AAAAAAAAA_U/_pl8_Jm_q44Jwv5cAM55-0k2HsZJ8zcdACLcBGAs/s1600/SingleProcess1.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-CbkvtgOq5Wg/Wwqs8kCquDI/AAAAAAAAA_Y/q5Fe-LkCejsc6eYQMcy8q7a2C7Mjt0M4wCLcBGAs/s1600/SingleProcess2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="161" data-original-width="624" src="https://4.bp.blogspot.com/-CbkvtgOq5Wg/Wwqs8kCquDI/AAAAAAAAA_Y/q5Fe-LkCejsc6eYQMcy8q7a2C7Mjt0M4wCLcBGAs/s1600/SingleProcess2.png" /></a></div>
<span style="text-align: justify;"><br /></span>
<span style="text-align: justify;"><br /></span>
<span style="text-align: justify;">You can download
code from <a href="https://gist.github.com/miteshsureja/4a577b200a4eda57ae27fff2e53f4430">Gist</a>.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Thank you for reading this article. Please
leave your feedback in comments below.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">Reference – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="https://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx">MSDN</a><o:p></o:p></span></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See also – <o:p></o:p></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<a href="http://miteshsureja.blogspot.in/2016/11/parallelfor-and-parallelforeach-task.html">Parallel.For and Parallel.Foreach</a><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<a href="http://miteshsureja.blogspot.in/2017/04/parallelinvoke-task-parallel-library.html">Parallel.Invoke</a><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<a href="http://miteshsureja.blogspot.com/2011/09/how-to-handle-exceptions-while-working.html">How to handle exception while working with Threading?</a></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-4273362418550159872018-06-01T19:27:00.001+05:302018-12-18T17:20:55.677+05:30Observer Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<span lang="EN-US">Observer design pattern is most suitable for
publishing notification to its subscribers. Observer pattern also known as
publisher-subscriber scenario where publisher notifies change to its all
subscribers. Observer Pattern falls under behavioral pattern of GOF (Gang of
Four) patterns.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">When to use – </span></h3>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Observer pattern used when you have subject,
which observed by many observers. When subject’s state changed, it notifies to
all its subscribers. The observers register with subject (provider), and
whenever the change occurs in subject (provider), the subject (provider)
automatically notifies all observers. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">To simplify Observer pattern, I have used most
common example of stock price change notification. In this example, the
watcher can register to watch on any stocks and whenever the stock price changes,
the watcher will get notify. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">Major components of Observer pattern – </span></h3>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">Observer –</span></b><span lang="EN-US">
This interface defines an update operation. This update operation will be
called when subject’s state changes.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Concrete Observer –</span></b><span lang="EN-US"> The class implements observer interface and implements actual
update operation.</span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Subject –</span></b><span lang="EN-US"> Subject
is an abstract class. It has information about its observers. It also provides mechanism
to subscribe and unsubscribe observers. <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Concrete Subject –</span></b><span lang="EN-US"> This class maintains its own state and calls base class notify
method to update its observers. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s have a look on below example of Observer
design pattern.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Code – </b><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Observer</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IStockPriceWatcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Subject { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Update(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> sub);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete observer</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Watcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IStockPriceWatcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Subject { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Watcher(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Update(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> sub)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0}, {1} price udated to {2}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">,Name, Subject.Name,
Subject.Price);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IStockPriceWatcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> investors = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IStockPriceWatcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Subject(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Price)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>name = Name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>price = Price;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Price<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (price != </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">value</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>price = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">value</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Notify();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Subscribe(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IStockPriceWatcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> observer)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>investors.Add(observer);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>observer.Subject = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} subscribed to {1}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, observer.Name,
name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> UnSubscribe(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IStockPriceWatcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> observer)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>investors.Remove(observer);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} unsubscribed from {1}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, observer.Name, name
);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Notify()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>investors.ForEach(x => x.Update(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TCS</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> TCS(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price) :</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">base</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (name, price) { }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Infosys</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Infosys(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price) : </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">base</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(name, price) { }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//concrete subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Wipro</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Subject</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Wipro(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">double</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> price) : </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">base</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(name, price) { }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//subjects</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TCS</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> tcs = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TCS</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"TCS"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 3000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Infosys</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> infy = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Infosys</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Infosys"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 1100);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Wipro</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> wipro = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Wipro</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Wipro"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 350);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//watchers</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Watcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> watcher1 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Watcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Watcher1"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Watcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> watcher2 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Watcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Watcher2"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Watcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> watcher3 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Watcher</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Watcher3"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">,30));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>tcs.Subscribe(watcher1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>infy.Subscribe(watcher2);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>wipro.Subscribe(watcher3);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 30));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>tcs.Price = 3100;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>infy.Price = 1050;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>wipro.Price = 355;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 30));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>tcs.UnSubscribe(watcher1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>infy.UnSubscribe(watcher2);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 30));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>tcs.Price = 3170;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>infy.Price = 1060;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 30));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>tcs.Subscribe(watcher2);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>infy.Subscribe(watcher1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'-'</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, 30));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>tcs.Price = 3200;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>infy.Price = 1070;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Output –</b></span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-oGhMfYD91tE/Wu1Mz8WQz1I/AAAAAAAAA7M/vZo-yz35oNot-KK7sP2d5vHPBEG4tR4MgCLcBGAs/s1600/Observer%2BDesign%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="392" data-original-width="600" height="417" src="https://3.bp.blogspot.com/-oGhMfYD91tE/Wu1Mz8WQz1I/AAAAAAAAA7M/vZo-yz35oNot-KK7sP2d5vHPBEG4tR4MgCLcBGAs/s640/Observer%2BDesign%2BPattern.png" width="640" /></a></div>
<br />
As per this example, the client class
creates multiple subjects (stocks) and watchers (subscribers). The client class
is also responsible for subscribe and unsubscribe watchers to particular subject
(stock). After successful registration, whenever subject’s state (stock price)
changes, the respective subscribers will get notify about change.<br />
<br />
You can download full code from <a href="https://gist.github.com/miteshsureja/f8b4bf3b2f7376963695efbb3ea08dbb">Gist</a>.<br />
<br />
I hope this article helps you to know more
about Observer Design Pattern. Please leave your feedback in comments section
below.<br />
<b><span lang="EN-US"><br /></span></b>
<b><span lang="EN-US">References –</span></b><br />
<a href="http://www.dofactory.com/net/observer-design-pattern">DoFactory</a><br />
<b><span lang="EN-US"><br /></span></b>
<b><span lang="EN-US">See Also –</span></b><br />
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 6;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 7;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 8;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 9;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 10;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 11; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-18485343604785755172018-05-12T19:02:00.001+05:302018-05-12T19:02:33.413+05:30Read Excel file data using PowerShell script<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
</div>
We quite often need to read data from Excel file (.xlsx) and
perform some action on it. In this article I’ll explain how to open Excel file in
PowerShell and read data from it.<br />
<br />
I want read below data available in my sample excel file using PowerShell script.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-5X6FoQxVHlQ/WvG3hl_X8DI/AAAAAAAAA8E/s2HUy50saNQoOzlOU4BmiGkgPpjLFikYACEwYBhgL/s1600/Excel1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="203" data-original-width="445" src="https://3.bp.blogspot.com/-5X6FoQxVHlQ/WvG3hl_X8DI/AAAAAAAAA8E/s2HUy50saNQoOzlOU4BmiGkgPpjLFikYACEwYBhgL/s1600/Excel1.png" /></a></div>
<br />
Let’s have a look on below PowerShell script which reads
data from sample excel file.<br />
<br />
<b>PS Script - </b><br />
<br />
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#select excel file you want to read</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$file</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"C:\PowerShell\MyContacts.xlsx"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$sheetName</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"Sheet1"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#create new excel COM object</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$excel</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: navy;">-com</span> <span style="color: blueviolet;">Excel.Application</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#open excel file</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$wb</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$excel</span><span style="color: darkgrey;">.</span>workbooks<span style="color: darkgrey;">.</span>open(<span style="color: orangered;">$file</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#select excel sheet to read data</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$sheet</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$wb</span><span style="color: darkgrey;">.</span>Worksheets<span style="color: darkgrey;">.</span>Item(<span style="color: orangered;">$sheetname</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#select total rows</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$rowMax</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> (<span style="color: orangered;">$sheet</span><span style="color: darkgrey;">.</span>UsedRange<span style="color: darkgrey;">.</span>Rows)<span style="color: darkgrey;">.</span>Count<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#create new object with Name, Address, Email properties.</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$myData</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: navy;">-TypeName</span> <span style="color: blueviolet;">psobject</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$myData</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">|</span> <span style="color: blue;">Add-Member</span> <span style="color: navy;">-MemberType</span>
<span style="color: blueviolet;">NoteProperty</span> <span style="color: navy;">-Name</span>
<span style="color: blueviolet;">Name</span> <span style="color: navy;">-Value</span>
<span style="color: orangered;">$null</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$myData</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">|</span> <span style="color: blue;">Add-Member</span> <span style="color: navy;">-MemberType</span>
<span style="color: blueviolet;">NoteProperty</span> <span style="color: navy;">-Name</span>
<span style="color: blueviolet;">Address</span> <span style="color: navy;">-Value</span>
<span style="color: orangered;">$null</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$myData</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">|</span> <span style="color: blue;">Add-Member</span> <span style="color: navy;">-MemberType</span>
<span style="color: blueviolet;">NoteProperty</span> <span style="color: navy;">-Name</span>
<span style="color: blueviolet;">Email</span> <span style="color: navy;">-Value</span>
<span style="color: orangered;">$null</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#create empty arraylist</span><span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$myArray</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: darkgrey;">=</span>
@()<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkblue; font-family: "lucida console"; font-size: 9.0pt;">for</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> (<span style="color: orangered;">$i</span>
<span style="color: darkgrey;">=</span> <span style="color: purple;">2</span>; <span style="color: orangered;">$i</span> <span style="color: darkgrey;">-le</span> <span style="color: orangered;">$rowMax</span>; <span style="color: orangered;">$i</span><span style="color: darkgrey;">++</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: orangered;">$objTemp</span>
<span style="color: darkgrey;">=</span> <span style="color: orangered;">$myData</span>
<span style="color: darkgrey;">|</span> <span style="color: blue;">Select-Object</span>
<span style="color: blueviolet;">*</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: darkgreen;">#read data
from each cell</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: orangered;">$objTemp</span><span style="color: darkgrey;">.</span>Name <span style="color: darkgrey;">=</span> <span style="color: orangered;">$sheet</span><span style="color: darkgrey;">.</span>Cells<span style="color: darkgrey;">.</span>Item(<span style="color: orangered;">$i</span><span style="color: darkgrey;">,</span><span style="color: purple;">1</span>)<span style="color: darkgrey;">.</span>Text<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: orangered;">$objTemp</span><span style="color: darkgrey;">.</span>Address <span style="color: darkgrey;">=</span> <span style="color: orangered;">$sheet</span><span style="color: darkgrey;">.</span>Cells<span style="color: darkgrey;">.</span>Item(<span style="color: orangered;">$i</span><span style="color: darkgrey;">,</span><span style="color: purple;">2</span>)<span style="color: darkgrey;">.</span>Text<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: orangered;">$objTemp</span><span style="color: darkgrey;">.</span>Email <span style="color: darkgrey;">=</span> <span style="color: orangered;">$sheet</span><span style="color: darkgrey;">.</span>Cells<span style="color: darkgrey;">.</span>Item(<span style="color: orangered;">$i</span><span style="color: darkgrey;">,</span><span style="color: purple;">3</span>)<span style="color: darkgrey;">.</span>Text<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: darkgreen;">#Write-Host
'Name-' $objTemp.Name 'Address-' $objTemp.Address 'Email-' $objTemp.Email</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: orangered;">$myArray</span>
<span style="color: darkgrey;">+=</span> <span style="color: orangered;">$objTemp</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#print $myarry object</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#$myArray </span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#print $myarry object with foreach loop</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkblue; font-family: "lucida console"; font-size: 9.0pt;">foreach</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> (<span style="color: orangered;">$x</span>
<span style="color: darkblue;">in</span> <span style="color: orangered;">$myArray</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Echo</span> <span style="color: orangered;">$x</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "lucida console"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: orangered; font-family: "lucida console"; font-size: 9.0pt;">$excel</span><span style="color: darkgrey; font-family: "lucida console"; font-size: 9.0pt;">.</span><span style="font-family: "lucida console"; font-size: 9.0pt;">Quit()<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: darkgreen; font-family: "lucida console"; font-size: 9.0pt;">#force stop Excel process</span><span style="font-family: "lucida console"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "lucida console"; font-size: 9.0pt;">Stop-Process</span><span style="font-family: "lucida console"; font-size: 9.0pt;"> <span style="color: navy;">-Name</span> <span style="color: blueviolet;">EXCEL</span> <span style="color: navy;">-Force <o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output – </b><o:p></o:p></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-s_FVcVoERCc/WvG3h5McyYI/AAAAAAAAA8I/vLerSe_5FS83PPwdQeRWeJe0tS50pI6LwCEwYBhgL/s1600/Excel2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="229" data-original-width="462" height="317" src="https://4.bp.blogspot.com/-s_FVcVoERCc/WvG3h5McyYI/AAAAAAAAA8I/vLerSe_5FS83PPwdQeRWeJe0tS50pI6LwCEwYBhgL/s640/Excel2.png" width="640" /></a></div>
<br />
You can download code from <a href="https://gist.github.com/miteshsureja/55b81f9a2dc563d9d34727f5e14aba9a">Gist</a>.<br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Thank you for reading this article. Please leave your
feedback in comments below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Reference – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/default.aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/02/introduction-to-windows-powershell.html">Introduction
to Windows PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/03/working-with-files-and-folders-using.html">Working with File and Folders using PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/03/advanced-files-and-folders-techniques.html">Advanced Files and Folders techniques with PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/04/get-process-cmdlet-in-powershell.html">Get-Process cmdlet in PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com1tag:blogger.com,1999:blog-3223444668744241586.post-266086022849324052018-05-06T10:52:00.000+05:302018-12-18T17:21:17.874+05:30Memento Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
Memento design pattern allows you to save
state of object and restore it back from historical states. Memento Pattern
falls under behavioral pattern of GOF (Gang of Four) patterns.</div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">When to use – </span></h3>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Memento pattern can be used to save sate of
an object which can be restored back to object later without violating
encapsulation. In your application, you may need to restore previously set
checkpoint or state or data in such scenarios you can consider using Memento design pattern. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">The best example of Memento Pattern is undo
or rollback operation. In this article, I have used Backup application example
in which I store state of multiple objects as backup and restore them later. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">Major components of Memento pattern – </span></h3>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">Client –</span></b><span lang="EN-US">
This class creates multiple instances of originator class and uses caretaker
class to store/restore state of an object.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Memento –</span></b><span lang="EN-US">
The Memento class stores internal state of the Originator class and protect
against access by objects other than originator class. <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Originator –</span></b><span lang="EN-US">
Creates an instance of Memento class and uses it for creating and restoring
state of an object.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Caretaker class –</span></b><span lang="EN-US"> This class manages list of Memento. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s have a look on below example of
Memento pattern.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Code – </b><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Memento class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Memento</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> SetBackup(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DateTime</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> date)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>b = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(name,
date);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> RestoreBackup()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Originator class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> BackupName { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DateTime</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> BackupDate { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Backup(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DateTime</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> date)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>BackupName = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>BackupDate = date;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Memento</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> CreateBackup()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Memento</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> m = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Memento</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>m.SetBackup(BackupName, BackupDate);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> m;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> RestoreBackup(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Memento</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> m)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b = m.RestoreBackup();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>BackupName = b.BackupName;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>BackupDate = b.BackupDate;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ToString()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Format(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Backup Name - {0}, Date - {1}"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, BackupName,
BackupDate.ToString(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"dd-MM-yyyy"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//caretaker class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BackupManager</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Memento</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>
mementoList = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Memento</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> CreateBackup(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} created."</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, b.BackupName);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>mementoList.Add(b.CreateBackup());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> RestoreBackup(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> backupName,
</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>b.RestoreBackup(mementoList.Find(x
=> x.b.BackupName.Equals(backupName)));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} restored."</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, b.BackupName);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ListAllBackups()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"\nBackups available for restore - {0}\n"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Concat(mementoList.Select(x
=> x.b.BackupName.ToString() + </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">","</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Client entry point</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BackupManager</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> bm = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BackupManager</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Backup1"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DateTime</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Now.AddDays(-10));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(b.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>bm.CreateBackup(b);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b1 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Backup2"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DateTime</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Now.AddDays(-8));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(b1.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>bm.CreateBackup(b1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b2 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Backup3"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DateTime</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Now.AddDays(-5));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(b2.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>bm.CreateBackup(b2);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> b3 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Backup</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Backup4"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">DateTime</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Now.AddDays(-2));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(b3.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>bm.CreateBackup(b3);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>bm.ListAllBackups();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>bm.RestoreBackup(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Backup2"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">,b);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(b.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>bm.RestoreBackup(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Backup1"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, b2);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(b2.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Output – </b><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-EhUu8Zp6ZM4/WsTY8sg2JSI/AAAAAAAAA6w/5tkrDLoGepMYTxbFaicvWhW1dE5jw9raQCLcBGAs/s1600/Memento%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="335" data-original-width="560" height="380" src="https://4.bp.blogspot.com/-EhUu8Zp6ZM4/WsTY8sg2JSI/AAAAAAAAA6w/5tkrDLoGepMYTxbFaicvWhW1dE5jw9raQCLcBGAs/s640/Memento%2BPattern.png" width="640" /></a></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span style="mso-ansi-language: EN-IN; mso-bidi-language: HI; mso-fareast-language: EN-IN; mso-no-proof: yes;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_22" o:spid="_x0000_i1025" style="height: 251.25pt; mso-wrap-style: square; visibility: visible; width: 420pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:\Users\Mitesh\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png">
</v:imagedata></v:shape></span><span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">As you can see in this example, Memento
class stores internal structure and data of Backup class (Originator class).
The Backup class uses Memento object to create and restore backup (state of an
object). The BackupManager class holds list of Memento and manages to create
and restore of backups. The client creates an instance of Backup and
BackupManager classes and uses backup manager instances to create and restore
state of objects.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">You can download full code from <a href="https://gist.github.com/miteshsureja/d8cb9858d40ae89d092a22078d5949a6">Gist</a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">I hope this article helps you to know more
about Memento Design Pattern. Please leave your feedback in comments section
below.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">References – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.dofactory.com/net/memento-design-pattern">DoFactory</a><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span lang="EN-US">See Also – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-48306624860958918532018-04-17T19:08:00.001+05:302018-04-17T19:08:53.045+05:30Get-Process cmdlet in PowerShell<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
In this article I’ll explain some cmdlets around how to get
all active processes running on your computer and start/stop process using
PowerShell.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;">Get-Process</span><o:p></o:p></div>
<div class="MsoNormal">
You can get all active and running processes on your
computer using below command.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Process | more</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-GgAc3uHuKus/WpP0AYxSWmI/AAAAAAAAA2g/tVHqwJo7ccALxHBh9SnHUgejT9M3h3a5wCLcBGAs/s1600/PowerShell34.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="220" data-original-width="602" height="232" src="https://4.bp.blogspot.com/-GgAc3uHuKus/WpP0AYxSWmI/AAAAAAAAA2g/tVHqwJo7ccALxHBh9SnHUgejT9M3h3a5wCLcBGAs/s640/PowerShell34.png" width="640" /></a></div>
<br />
<br />
<br />
<div class="MsoNormal">
You can specify names or ID of processes you want to get
information about.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Process -Name Chrome, explorer, powershell</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-Aaiv2RHHtjk/WpP0AVku6yI/AAAAAAAAA2k/PrxLuxXwfgwzY4fhp264L4_3bd33PLZbgCEwYBhgL/s1600/PowerShell35.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="260" data-original-width="602" height="276" src="https://1.bp.blogspot.com/-Aaiv2RHHtjk/WpP0AVku6yI/AAAAAAAAA2k/PrxLuxXwfgwzY4fhp264L4_3bd33PLZbgCEwYBhgL/s640/PowerShell35.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<div class="MsoNormal">
<b>Get-Process -Id 200,1692,7368,6988</b><o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-wTGO6GhD0_M/WpP0AGuTjaI/AAAAAAAAA2c/Q9O-mbTkgeYWXW1C6eyeNNZ4-QJTG9esQCEwYBhgL/s1600/PowerShell36.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="194" data-original-width="602" height="206" src="https://1.bp.blogspot.com/-wTGO6GhD0_M/WpP0AGuTjaI/AAAAAAAAA2c/Q9O-mbTkgeYWXW1C6eyeNNZ4-QJTG9esQCEwYBhgL/s640/PowerShell36.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<br />
<br />
<br />
<div class="MsoNormal">
You can use wildcard characters to search processes.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Process -Name w*</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-VOhLAlvv1bk/WpP0BFtHzTI/AAAAAAAAA2o/CcNgbY1b5y8mZuVWt2Z5MJLWFgIPBTmzQCEwYBhgL/s1600/PowerShell37.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="192" data-original-width="602" height="204" src="https://2.bp.blogspot.com/-VOhLAlvv1bk/WpP0BFtHzTI/AAAAAAAAA2o/CcNgbY1b5y8mZuVWt2Z5MJLWFgIPBTmzQCEwYBhgL/s640/PowerShell37.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<div class="MsoNormal">
You can specify local or remote computer name to get
processes running on local or remote computer.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Process -ComputerName Mitesh-PC -Name chrome,powershell</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-OPJxMO7lHK4/WpP0Bdh1jAI/AAAAAAAAA2s/V-jVwclsDd8ZiFRlDYWAre6QgXrXBd7zQCEwYBhgL/s1600/PowerShell38.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="253" data-original-width="602" height="268" src="https://4.bp.blogspot.com/-OPJxMO7lHK4/WpP0Bdh1jAI/AAAAAAAAA2s/V-jVwclsDd8ZiFRlDYWAre6QgXrXBd7zQCEwYBhgL/s640/PowerShell38.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<div class="MsoNormal">
You can also use below command to check count of how many
processes of single process is running using below command. You can use
Group-Object cmdlets to group on property. Before you use group you need to
sort data using Sort-Object cmdlet.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Process | Sort-Object Name | Group-Object -Property Name</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-s520HvRATrM/WpP0BWM6VaI/AAAAAAAAA2w/UC2-wrpA_CE6WhxYLSh3Il6RKhd-9qpvQCEwYBhgL/s1600/PowerShell39.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="225" data-original-width="602" height="238" src="https://2.bp.blogspot.com/-s520HvRATrM/WpP0BWM6VaI/AAAAAAAAA2w/UC2-wrpA_CE6WhxYLSh3Il6RKhd-9qpvQCEwYBhgL/s640/PowerShell39.png" width="640" /></a></div>
<br />
<br />
<br />
<div class="MsoNormal">
You can send result to output text file using Out-file
cmdlets.</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Get-Process | Sort-Object Name | Group-Object -Property Name
| Select count, name | Out-File -FilePath C:\PowerShell\Processes.txt</b></div>
<div class="MsoNormal">
<b>Get-ChildItem process*.txt</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-5W_ZZC3VYOU/WpP0B3uD4zI/AAAAAAAAA3c/QFwLlw3VujsrDTALMNdRzAsqLCxXCJBXACEwYBhgL/s1600/PowerShell40.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="233" data-original-width="602" height="246" src="https://4.bp.blogspot.com/-5W_ZZC3VYOU/WpP0B3uD4zI/AAAAAAAAA3c/QFwLlw3VujsrDTALMNdRzAsqLCxXCJBXACEwYBhgL/s640/PowerShell40.png" width="640" /></a></div>
<br />
<br />
<br />
<div class="MsoNormal">
You can find a specific process running on computer using
where object and you can stop that processes same time via below cmdlets. In
below example, I search for processes using CPU more than 50 and name starts
with ‘A’ and stop all the processes matching this condition.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Get-Process | Where-Object {$_.CPU -ge 50 -and $_.Name -like
'A*'} | Stop-Process -PassThru</b><o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-IYsbjAWWrLM/WpP0CBjVivI/AAAAAAAAA3U/bMbjdC5lppIK36bOyH3Wg85iuAYNgBRHgCEwYBhgL/s1600/PowerShell41.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="188" data-original-width="602" height="198" src="https://2.bp.blogspot.com/-IYsbjAWWrLM/WpP0CBjVivI/AAAAAAAAA3U/bMbjdC5lppIK36bOyH3Wg85iuAYNgBRHgCEwYBhgL/s640/PowerShell41.png" width="640" /></a></div>
<br />
<br />
<br />
<div class="MsoNormal">
<span style="font-size: large;">Start-Process</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can start process on your local computer using
Start-Process cmdlets.<o:p></o:p></div>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<b>Start-Process
notepad </b></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-k2HjTqmxOq0/WpP0CEj2XLI/AAAAAAAAA3c/ARa441xCw2QAskbqUXkAfSPareTYNFg5wCEwYBhgL/s1600/PowerShell42.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="128" data-original-width="602" height="136" src="https://1.bp.blogspot.com/-k2HjTqmxOq0/WpP0CEj2XLI/AAAAAAAAA3c/ARa441xCw2QAskbqUXkAfSPareTYNFg5wCEwYBhgL/s640/PowerShell42.png" width="640" /></a></div>
<br />
<br />
<br />
<div class="MsoNormal">
You can also specify additional parameters along with
start-process.<o:p></o:p></div>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b><span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
Start-Process
notepad -WindowStyle Maximized</b></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-f4Mf7TtDoys/WpP0CZE_glI/AAAAAAAAA3Y/nrBsCDzeImE5QNXch7qLXd5v2IQLv6AKQCEwYBhgL/s1600/PowerShell43.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="128" data-original-width="602" height="136" src="https://3.bp.blogspot.com/-f4Mf7TtDoys/WpP0CZE_glI/AAAAAAAAA3Y/nrBsCDzeImE5QNXch7qLXd5v2IQLv6AKQCEwYBhgL/s640/PowerShell43.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<div class="MsoNormal">
<span style="font-size: large;">Stop-Process<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can stop any running processes using stop-process
cmdlets. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Stop-Process -Name notepad<o:p></o:p></b></div>
<div class="MsoNormal">
<b>Stop-Process -ID 9964</b></div>
<br />
<a href="https://3.bp.blogspot.com/-MTsYJKJIAoo/WpP0ClrO4YI/AAAAAAAAA3Y/Q_pkmd8GRC0gbmvvmZMK7_iziBkCSWphgCEwYBhgL/s1600/PowerShell44.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="181" data-original-width="602" height="192" src="https://3.bp.blogspot.com/-MTsYJKJIAoo/WpP0ClrO4YI/AAAAAAAAA3Y/Q_pkmd8GRC0gbmvvmZMK7_iziBkCSWphgCEwYBhgL/s640/PowerShell44.png" width="640" /></a><br />
<br />
<br />
<br />
<div class="MsoNormal">
<span style="font-size: large;">Wait-Process</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This cmdlets wait for process to be stopped. If you execute
this cmdlets for specific process, PowerShell prompt will waits until give
process stopped manually.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<a href="https://3.bp.blogspot.com/-T_lFdQbOUsE/WpP0C86YEKI/AAAAAAAAA3c/ybJ07uW4MQU2Fhk6T2PGomD3uN50y07MACEwYBhgL/s1600/PowerShell45.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="181" data-original-width="602" height="192" src="https://3.bp.blogspot.com/-T_lFdQbOUsE/WpP0C86YEKI/AAAAAAAAA3c/ybJ07uW4MQU2Fhk6T2PGomD3uN50y07MACEwYBhgL/s640/PowerShell45.png" width="640" /></a></div>
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">Debug-Process</span><br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This cmdlets debug one or more processes running on local
computer. This cmdlets attaches the debugger to running process. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Get-Process powershell</b></div>
<div class="MsoNormal">
<b>Debug-Process powershell</b><a href="https://2.bp.blogspot.com/-XSiWSbhYuTo/WpP0DK8_rgI/AAAAAAAAA3c/czWQNEnA8206xNuYI-CtR3o2NLn21rhbACEwYBhgL/s1600/PowerShell46.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="181" data-original-width="602" height="192" src="https://2.bp.blogspot.com/-XSiWSbhYuTo/WpP0DK8_rgI/AAAAAAAAA3c/czWQNEnA8206xNuYI-CtR3o2NLn21rhbACEwYBhgL/s640/PowerShell46.png" width="640" /></a></div>
<br />
<br />
I hope you have some knowledge about how to get processes
and perform some actions around it using various PowerShell cmdlets.<br />
<br />
Thank you for reading this article. Please leave your
feedback below.<br />
<b><br /></b>
<b>Reference –</b><br />
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/default.aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/02/introduction-to-windows-powershell.html">Introduction
to Windows PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/03/working-with-files-and-folders-using.html">Working with File and Folders using PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/03/advanced-files-and-folders-techniques.html">Advanced Files and Folders techniques with PowerShell</a><o:p></o:p></div>
<br /></div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-84697086620845291222018-04-09T18:11:00.000+05:302018-12-18T17:21:35.504+05:30Mediator Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<span lang="EN-US">Mediator design pattern promotes loose
coupling between objects and communicate between them with the help of mediator
object. Mediator Pattern falls under behavioral pattern of GOF (Gang of Four)
patterns.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">When to use – </span></h3>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Mediator pattern provides a way to communicate
between objects indirectly via mediator object. The mediator object facilitates
the communication between objects. When you have large number of objects in
your application, which is very hard to maintain you should consider using
Mediator pattern.<span style="mso-spacerun: yes;"> </span>Mediator pattern
avoids direct communication between objects and it uses mediator object that is
responsible to communicate between source and destination object. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">There are so many practical and real life examples
available for Mediator Pattern. In this article, I have used simple Chat
application example in which so many participants can send their messages to
other participants via mediator. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-US">Major components of Mediator pattern – </span></h3>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">Client –</span></b><span lang="EN-US"> This
class creates an instance of multiple participants and sends messages between
them.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Mediator –</span></b><span lang="EN-US">
The Mediator interface for communicating with different participants
(colleagues). <o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">ConcreteMediator – </span></b><span lang="EN-US">This class implements the Mediator interface and maintain
participants.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US">Participant (Colleague) class –</span></b><span lang="EN-US"> This class has reference of Mediator object and uses this object to
communicate with other participants. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s have a look on below example of Mediator
pattern.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Code – </b><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Mediator</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IChatRoom</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Register(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> participant);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> SendMessage(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> from, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> to, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> message);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Concrete Mediator</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ChatRoom</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IChatRoom</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Dictionary</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> participants = <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Dictionary</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Register(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> participant)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (!participants.ContainsValue(participant))<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participants.Add(participant.Name,
participant);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participant.ChatRoom = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> SendMessage(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> from, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> to, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> message)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (participants.Keys.Contains(to))<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> participant = participants[to];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participant.Receive(from, message);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Participant not registered."</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Participant class (Colleague
class) </span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Participant(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> name)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ChatRoom</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> ChatRoom { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Send(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> to, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> message)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>ChatRoom.SendMessage(Name, to,
message);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Receive(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> from, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> message)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} to {1} - '{2}'"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>from, Name, message);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Client class (entry point)</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ChatRoom</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> chatroom = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ChatRoom</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> people
= </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{ </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Mitesh"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sandeep"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Vikram"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Amit"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> participants = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> s </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> people)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participants.Add(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">(s));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Participant</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> p </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> participants)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>chatroom.Register(p);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participants.Find(x =>
x.Name.Equals(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Mitesh"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Send(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sandeep"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Hey Sandeep,
How are you?"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participants.Find(x =>
x.Name.Equals(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Vikram"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Send(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Mitesh"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Where are
you?"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participants.Find(x =>
x.Name.Equals(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sandeep"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Send(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Mitesh"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Hi Mitesh, i
am good. Thanks"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participants.Find(x =>
x.Name.Equals(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Mitesh"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Send(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Vikram"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"I am at
home"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>participants.Find(x =>
x.Name.Equals(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Amit"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Send(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Vikram"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Hi"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><b>Output – </b><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-t_xbGZX_cPk/Wr8hEqNMBlI/AAAAAAAAA6U/l2kaesItcGI1JgyGnnL4p1m692t8y7IfQCLcBGAs/s1600/Mediator%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="172" data-original-width="496" height="220" src="https://1.bp.blogspot.com/-t_xbGZX_cPk/Wr8hEqNMBlI/AAAAAAAAA6U/l2kaesItcGI1JgyGnnL4p1m692t8y7IfQCLcBGAs/s640/Mediator%2BPattern.png" width="640" /></a></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span style="mso-ansi-language: EN-IN; mso-bidi-language: HI; mso-fareast-language: EN-IN; mso-no-proof: yes;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_25" o:spid="_x0000_i1025" style="height: 129pt; mso-wrap-style: square; visibility: visible; width: 372pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:\Users\Mitesh\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png">
</v:imagedata></v:shape></span><span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">As you can see in this example, Participant
class has reference to mediator object (ChatRoom). ChatRoom object knows about
different participants and facilitates communication between them. Client
creates various participants and register them with mediator object and sends
messages between different participants. The sender participant does not have
direct reference to receiver participant and it communicates via Mediator
object. <o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">You can download full code from <a href="https://gist.github.com/miteshsureja/e6290b3001cf5b4ff8a7be0a91f8b113">Gist</a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">I hope this article helps you to know more
about Mediator Design Pattern. Please leave your feedback in comments section
below.<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span lang="EN-US"><br /></span></b></div>
<div class="MsoNormal">
<b><span lang="EN-US">References – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.dofactory.com/net/mediator-design-pattern">DoFactory</a><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span lang="EN-US">See Also – <o:p></o:p></span></b></div>
<div class="MsoNormal">
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 6;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 7;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 8;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 9;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 10;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 11; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-68717134798802854112018-03-17T19:24:00.000+05:302018-03-17T19:24:45.239+05:30Advanced Files and Folders techniques with PowerShell<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
In my previous article, I explained basic files and folder PowerShell cmdlets. In this article I’ll show some advanced features and techniques to
work with Files and Folders using PowerShell.<br />
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;">Get-Member</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
When you execute Get-ChildItem cmdlets it will display only
default columns not all. You can use below command to check how many columns you
can display with Get-ChildItem or any other command. <o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;">Get-ChildItem
| Get-Member |more</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-ueGsnH7ZH2A/WpJnchf5suI/AAAAAAAAA1c/bLgXGYnYzQQ0h5omczCVbppnxB9aQPV7QCLcBGAs/s1600/PowerShell25.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="431" data-original-width="602" height="458" src="https://1.bp.blogspot.com/-ueGsnH7ZH2A/WpJnchf5suI/AAAAAAAAA1c/bLgXGYnYzQQ0h5omczCVbppnxB9aQPV7QCLcBGAs/s640/PowerShell25.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The Get-Member command will list all the Methods and
Properties of given command or object. If you want to get Properties of given
command or object use below command.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Get-ChildItem | Get-Member -MemberType Properties<o:p></o:p></div>
<div class="MsoNormal">
<a href="https://1.bp.blogspot.com/-n4OLQ23uVc0/WpJncBU7aBI/AAAAAAAAA1Y/7FR4cMjkRPcbUtEhmb9_ykmGZrzC_AYuwCEwYBhgL/s1600/PowerShell26.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="358" data-original-width="602" height="380" src="https://1.bp.blogspot.com/-n4OLQ23uVc0/WpJncBU7aBI/AAAAAAAAA1Y/7FR4cMjkRPcbUtEhmb9_ykmGZrzC_AYuwCEwYBhgL/s640/PowerShell26.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If you want to get only Methods of given command or object use
below command.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Get-ChildItem | Get-Member -MemberType Method<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-wGPyaFFAyh0/WpJncOI65aI/AAAAAAAAA1U/KRLYy7_FLAwJBqnOnOmuX4Ix8nwhoPLZgCEwYBhgL/s1600/PowerShell27.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="358" data-original-width="602" height="380" src="https://4.bp.blogspot.com/-wGPyaFFAyh0/WpJncOI65aI/AAAAAAAAA1U/KRLYy7_FLAwJBqnOnOmuX4Ix8nwhoPLZgCEwYBhgL/s640/PowerShell27.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div style="text-align: left;">
<span style="font-size: large;">Get-ChildItem</span></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you know via Get-Member cmdlets you can find all the
methods and properties of given command. So when you execute Get-ChildItem
cmdlets it will display all the default columns but you can customize it via selecting
specific columns to display. You can customize number of columns by specifying
Select-Object or Select with pipe line.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Get-ChildItem -Path c:\Powershell | select Name, Length<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-DbxcVVejy_A/WpJndFABcfI/AAAAAAAAA1g/9b7UUE5V79IrlH31h7-mdJY0w67SS3zNgCEwYBhgL/s1600/PowerShell28.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="215" data-original-width="602" height="228" src="https://4.bp.blogspot.com/-DbxcVVejy_A/WpJndFABcfI/AAAAAAAAA1g/9b7UUE5V79IrlH31h7-mdJY0w67SS3zNgCEwYBhgL/s640/PowerShell28.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can also include other columns which are not displayed
in default result. I included IsReadOnly, CreationTime, BaseName columns in
below command.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Get-ChildItem | select Name, IsReadOnly, CreationTime,
BaseName<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-uPvWeeje4JM/WpJndQ3ZCWI/AAAAAAAAA1k/QfdiZSs8CsMCwXe3cEkxzohLGXmAxVdlACEwYBhgL/s1600/PowerShell29.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="215" data-original-width="602" height="228" src="https://2.bp.blogspot.com/-uPvWeeje4JM/WpJndQ3ZCWI/AAAAAAAAA1k/QfdiZSs8CsMCwXe3cEkxzohLGXmAxVdlACEwYBhgL/s640/PowerShell29.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can also sort list of files and directories based on
column names using Sort-Object or Sort cmdlets.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Get-ChildItem | select Name, IsReadOnly, CreationTime,
Length | Sort Length, CreationTime -Descending <o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-fo5Cgu41Sco/WpJndq2gCdI/AAAAAAAAA14/oRDaQrcuzyI425slHuOL0JsVrde2nwoLQCEwYBhgL/s1600/PowerShell30.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="201" data-original-width="602" height="212" src="https://2.bp.blogspot.com/-fo5Cgu41Sco/WpJndq2gCdI/AAAAAAAAA14/oRDaQrcuzyI425slHuOL0JsVrde2nwoLQCEwYBhgL/s640/PowerShell30.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use below command to count total files or folders
recursively.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
(Get-ChildItem -File -Recurse).count<o:p></o:p></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
(Get-ChildItem -Directory -Recurse).count<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-fzO0Qtg9iSM/WpJndwpatQI/AAAAAAAAA2A/fQJ1d0TfpNQ_wBbt8nDG9ttxQNnxgLTWACEwYBhgL/s1600/PowerShell31.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="155" data-original-width="602" height="164" src="https://4.bp.blogspot.com/-fzO0Qtg9iSM/WpJndwpatQI/AAAAAAAAA2A/fQJ1d0TfpNQ_wBbt8nDG9ttxQNnxgLTWACEwYBhgL/s640/PowerShell31.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use Where cmdlets to search files or folders on specific
conditions.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Get-ChildItem -file | where {$_.Name -eq "app.pdf"
-or $_.Name -eq "Test.doc"} | select Name, Length, LastWriteTime |
sort Length -Descending<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-P4Pdj8dv3ag/WpJneQf7BjI/AAAAAAAAA18/dlNRNQXv9Wsw22ZIQbSTODb_V6yJRsWDACEwYBhgL/s1600/PowerShell32.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="177" data-original-width="602" height="188" src="https://2.bp.blogspot.com/-P4Pdj8dv3ag/WpJneQf7BjI/AAAAAAAAA18/dlNRNQXv9Wsw22ZIQbSTODb_V6yJRsWDACEwYBhgL/s640/PowerShell32.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use below command to get list of all the specified files
and rename it via ForEach object.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Get-ChildItem -File -Filter *.PDF | foreach {Rename-Item
$_.FullName -NewName ($_.BaseName + 1 + $_.Extension)}<o:p></o:p></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
Get-ChildItem -File -Filter *.PDF<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-JtnPdM6hKzU/WpJne2TqHbI/AAAAAAAAA2E/GU-w35kWLvE2TeAvXNyQHy1fm9Yb4TemgCEwYBhgL/s1600/PowerShell33.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="250" data-original-width="602" height="264" src="https://1.bp.blogspot.com/-JtnPdM6hKzU/WpJne2TqHbI/AAAAAAAAA2E/GU-w35kWLvE2TeAvXNyQHy1fm9Yb4TemgCEwYBhgL/s640/PowerShell33.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The similar way you can use cmdlets like Move-Item,
Copy-Item, Remove-Item, New-Item etc. along with Get-ChildItem to get list of files and folders and execute cmdlets on the fly.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope you have now some understanding on advanced
techniques to list files and folders, selecting extra columns, sorting and
counting total files and folders and loop through each files in foreach loop to
perform some actions.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Thank you for reading this article. Please leave your
feedback in comments below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Reference – </b><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/default.aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/02/introduction-to-windows-powershell.html">Introduction
to Windows PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/03/working-with-files-and-folders-using.html">Working with File and Folders using PowerShell</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-67246538821737032562018-03-02T18:14:00.002+05:302018-03-02T18:14:49.759+05:30Working with Files and Folders using PowerShell<div dir="ltr" style="text-align: left;" trbidi="on">
In this article I’ll explain various PowerShell
commands to perform File/Directory operations.<br />
<br />
<div class="MsoNormal">
<b><span style="font-size: large;">Get-ChildItem</span></b></div>
<div class="MsoNormal">
<br />
You can use ‘Get-ChildItem’ command to get list of all the
files and folder of given path.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Get-ChildItem -Path C:\PowerShell </b></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-l1PXJ35-Ueg/WpJDpTZ481I/AAAAAAAAA0I/pnfsl_m_mhISoZyUd2WQmrOV9c4I4cY-wCLcBGAs/s1600/PowerShell13.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="288" data-original-width="602" height="306" src="https://4.bp.blogspot.com/-l1PXJ35-Ueg/WpJDpTZ481I/AAAAAAAAA0I/pnfsl_m_mhISoZyUd2WQmrOV9c4I4cY-wCLcBGAs/s640/PowerShell13.png" width="640" /></a></div>
<div>
<br /></div>
<div class="MsoNormal">
You can use below commands to list either files or folders
only of given path<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Get-ChildItem -Path C:\PowerShell -File</b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-aF-In2D1t58/WpJDpfg6JSI/AAAAAAAAA0A/ctTa_vFJOmAk93Gn_j_juxfVMWCc3_ucACEwYBhgL/s1600/PowerShell14.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="288" data-original-width="602" height="306" src="https://1.bp.blogspot.com/-aF-In2D1t58/WpJDpfg6JSI/AAAAAAAAA0A/ctTa_vFJOmAk93Gn_j_juxfVMWCc3_ucACEwYBhgL/s640/PowerShell14.png" width="640" /></a></div>
<div>
<br /></div>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b>Get-ChildItem
-Path C:\PowerShell -Directory</b></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-rfKk-PoWY2A/WpJDpenyfGI/AAAAAAAAA0E/aN6byQ4m4l4RloxEV7BYVQNemTBtWA6AgCEwYBhgL/s1600/PowerShell15.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="241" data-original-width="602" height="256" src="https://4.bp.blogspot.com/-rfKk-PoWY2A/WpJDpenyfGI/AAAAAAAAA0E/aN6byQ4m4l4RloxEV7BYVQNemTBtWA6AgCEwYBhgL/s640/PowerShell15.png" width="640" /></a></div>
<div>
<span style="font-family: "calibri" , sans-serif;"><span style="font-size: 14.6667px;"><br /></span></span></div>
<div>
<div class="MsoNormal">
You can use below command to filter files and folders as per
your requirement.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Get-ChildItem -Path C:\PowerShell -Filter *.pdf</b><o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-8lQICCqzVXo/WpJDqePnr8I/AAAAAAAAA0M/YL8iijHXOF86j1VIkh0AZDYBvSGb9pH7gCEwYBhgL/s1600/PowerShell16.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="252" data-original-width="602" height="267" src="https://4.bp.blogspot.com/-8lQICCqzVXo/WpJDqePnr8I/AAAAAAAAA0M/YL8iijHXOF86j1VIkh0AZDYBvSGb9pH7gCEwYBhgL/s640/PowerShell16.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<br />
<br />
<div class="MsoNormal">
You can you below command to list all the files and folders
of give path recursively.<o:p></o:p></div>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b>Get-ChildItem -Path
C:\PowerShell -Recurse</b></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-gx9iIQxP2k8/WpJDqh1UC0I/AAAAAAAAA0Q/A9T2sm05_ZondY4V7k9z3Ox9OY0zMJRVACEwYBhgL/s1600/PowerShell17.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="411" data-original-width="602" height="436" src="https://3.bp.blogspot.com/-gx9iIQxP2k8/WpJDqh1UC0I/AAAAAAAAA0Q/A9T2sm05_ZondY4V7k9z3Ox9OY0zMJRVACEwYBhgL/s640/PowerShell17.png" width="640" /></a></div>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: "calibri" , sans-serif; line-height: 115%;"></span><br />
<div class="MsoNormal" style="font-size: 11pt;">
You can also use Include or Exclude parameters with
Get-ChildItem command to filter files.<o:p></o:p></div>
<div class="MsoNormal" style="font-size: 11pt;">
<br /></div>
<div class="MsoNormal" style="font-size: 11pt;">
<b>Get-ChildItem -Path C:\PowerShell -Include "*.pdf"
-Exclude "s*.PDF" –Recurse</b><o:p></o:p></div>
<div class="MsoNormal" style="font-size: 11pt;">
<a href="https://1.bp.blogspot.com/-O5VdGyy8-8s/WpJDqz0nYhI/AAAAAAAAA0w/Yeqd5zmaMkwbv37CUj6caU3sh1N1_Q-vQCEwYBhgL/s1600/PowerShell18.png" imageanchor="1" style="clear: left; display: inline !important; font-family: Calibri, sans-serif; font-size: 11pt; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="245" data-original-width="602" height="260" src="https://1.bp.blogspot.com/-O5VdGyy8-8s/WpJDqz0nYhI/AAAAAAAAA0w/Yeqd5zmaMkwbv37CUj6caU3sh1N1_Q-vQCEwYBhgL/s640/PowerShell18.png" width="640" /></a></div>
<div class="MsoNormal" style="font-size: 11pt;">
<b style="font-family: Calibri, sans-serif; font-size: 11pt;"><br /></b></div>
<div class="MsoNormal">
<b style="font-family: Calibri, sans-serif;"><span style="font-size: large;"><br /></span></b></div>
<div class="MsoNormal">
<b style="font-family: Calibri, sans-serif;"><span style="font-size: large;">New-Item</span></b></div>
<div class="MsoNormal">
<span style="font-family: "calibri" , sans-serif; font-size: 11pt;">You can use ‘New-Item’ command to create new file or
directory.</span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>New-Item -Type File "C:\PowerShell\First1.txt"</b></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<a href="https://3.bp.blogspot.com/-qiTyKiP4joI/WpJDrJtgWRI/AAAAAAAAA04/GTAIGyXpBKo974WOp-h2QWNQJyxPU_TegCEwYBhgL/s1600/PowerShell19.png" imageanchor="1" style="clear: left; display: inline !important; font-family: Calibri, sans-serif; font-size: 11pt; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="220" data-original-width="602" height="233" src="https://3.bp.blogspot.com/-qiTyKiP4joI/WpJDrJtgWRI/AAAAAAAAA04/GTAIGyXpBKo974WOp-h2QWNQJyxPU_TegCEwYBhgL/s640/PowerShell19.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<span style="font-family: calibri, sans-serif; font-size: 11pt;"><b>New-Item
-Type Directory "C:\PowerShell\MyFolder"</b></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-Kt3zW9mHRus/WpJDrc5JcKI/AAAAAAAAA00/wVtzfc0XC7It_2WVR32OcrQVqz0T88oUgCEwYBhgL/s1600/PowerShell20.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="220" data-original-width="602" height="233" src="https://2.bp.blogspot.com/-Kt3zW9mHRus/WpJDrc5JcKI/AAAAAAAAA00/wVtzfc0XC7It_2WVR32OcrQVqz0T88oUgCEwYBhgL/s640/PowerShell20.png" width="640" /></a></div>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: "calibri" , sans-serif; line-height: 115%;"></span><br />
<h3 style="text-align: left;">
<b><span style="font-size: large;">Copy-Item</span></b></h3>
<div class="MsoNormal" style="font-size: 11pt;">
<span style="font-family: "calibri" , sans-serif; font-size: 11pt;">You can use Copy-Item command to copy files and directories.</span></div>
<div class="MsoNormal" style="font-size: 11pt;">
<o:p></o:p></div>
<div class="MsoNormal" style="font-size: 11pt; line-height: normal; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="font-size: 11pt; line-height: normal; margin-bottom: 0.0001pt;">
<b>Copy-Item First1.txt Mitesh\First2.txt<o:p></o:p></b></div>
<div class="MsoNormal" style="font-size: 11pt; line-height: normal; margin-bottom: 0.0001pt;">
<b>Get-ChildItem -Path C:\PowerShell\Mitesh</b><o:p></o:p></div>
<div class="MsoNormal" style="font-size: 11pt; line-height: normal; margin-bottom: 0.0001pt;">
<a href="https://3.bp.blogspot.com/--59SyJEmHuo/WpJDrqO62UI/AAAAAAAAA08/BzQ7GRks9E0aVqvvNiqGN-CFMxYJyTIkgCEwYBhgL/s1600/PowerShell21.png" imageanchor="1" style="clear: left; display: inline !important; font-family: Calibri, sans-serif; font-size: 11pt; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="256" data-original-width="602" height="272" src="https://3.bp.blogspot.com/--59SyJEmHuo/WpJDrqO62UI/AAAAAAAAA08/BzQ7GRks9E0aVqvvNiqGN-CFMxYJyTIkgCEwYBhgL/s640/PowerShell21.png" width="640" /></a></div>
<div class="MsoNormal" style="font-size: 11pt; line-height: normal; margin-bottom: 0.0001pt;">
<br /></div>
<br />
<h3>
<b><span style="font-size: large;">Remove-Item</span></b></h3>
<div style="text-align: left;">
<span style="font-family: "calibri" , sans-serif; font-size: 11pt;">You can use Remove-Item command to delete files and
directories.</span></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Remove-Item c:\PowerShell\Mitesh\First2.txt<o:p></o:p></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Get-ChildItem -Path C:\PowerShell\Mitesh<o:p></o:p></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<a href="https://3.bp.blogspot.com/-e0wtPRlr_2U/WpJDrzwc-PI/AAAAAAAAA04/c429_oT8xmw8PO3tYQ51S_GNlLkDKEdWACEwYBhgL/s1600/PowerShell22.png" imageanchor="1" style="clear: left; display: inline !important; font-family: Calibri, sans-serif; font-size: 11pt; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="255" data-original-width="602" height="270" src="https://3.bp.blogspot.com/-e0wtPRlr_2U/WpJDrzwc-PI/AAAAAAAAA04/c429_oT8xmw8PO3tYQ51S_GNlLkDKEdWACEwYBhgL/s640/PowerShell22.png" width="640" /></a></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<br />
<h3>
<b><span style="font-size: large;">Rename-Item</span></b></h3>
<div style="text-align: left;">
You can use ‘Rename-Item’ command to rename file and
directories.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Rename-Item First1.txt First3.txt<o:p></o:p></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Get-ChildItem -Path C:\PowerShell\Mitesh</b><o:p></o:p></div>
<a href="https://2.bp.blogspot.com/-W1F8EyEwfFg/WpJDsDjnSrI/AAAAAAAAA04/dtCTxaO4SrUFbVLnGuk19aFz_ZFOcd2wACEwYBhgL/s1600/PowerShell23.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="292" data-original-width="602" height="310" src="https://2.bp.blogspot.com/-W1F8EyEwfFg/WpJDsDjnSrI/AAAAAAAAA04/dtCTxaO4SrUFbVLnGuk19aFz_ZFOcd2wACEwYBhgL/s640/PowerShell23.png" width="640" /></a><br />
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><br /></span>
<br />
<div style="text-align: left;">
<span style="font-size: large;"><b>Move-Item</b></span></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
You can use ‘Move-Item’ command to move files and
directories.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Move-Item c:\PowerShell\First3.txt C:\PowerShell\Mitesh\First1.txt<o:p></o:p></b></div>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><b>
<span style="font-size: 11pt; line-height: 115%;">Get-ChildItem
C:\PowerShell\Mitesh</span></b></span><br />
<a href="https://1.bp.blogspot.com/-_BYQd23gOB0/WpJDsu-Gu-I/AAAAAAAAA08/Ox08ioBqoTwnIi9o3PwlPT6hcZ-PizZigCEwYBhgL/s1600/PowerShell24.png" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="279" data-original-width="602" height="296" src="https://1.bp.blogspot.com/-_BYQd23gOB0/WpJDsu-Gu-I/AAAAAAAAA08/Ox08ioBqoTwnIi9o3PwlPT6hcZ-PizZigCEwYBhgL/s640/PowerShell24.png" width="640" /></a><br />
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><span style="font-size: 11pt; line-height: 115%;"><br /></span></span>
<br />
<h3>
<b><span style="font-size: large;">Summary</span></b></h3>
<span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><span style="font-size: 11pt; line-height: 115%;">
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 69.2pt;" valign="top" width="92"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Alias<o:p></o:p></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 114.5pt;" valign="top" width="153"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Cmdlet<o:p></o:p></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 236.0pt;" valign="top" width="315"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b>Description<o:p></o:p></b></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 69.2pt;" valign="top" width="92"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
dir<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 114.5pt;" valign="top" width="153"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Get-ChildItem<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 236.0pt;" valign="top" width="315"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
List files and folders<o:p></o:p></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 69.2pt;" valign="top" width="92"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
ni<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 114.5pt;" valign="top" width="153"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
New-Item<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 236.0pt;" valign="top" width="315"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Create new files and folders<o:p></o:p></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 69.2pt;" valign="top" width="92"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
copy<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 114.5pt;" valign="top" width="153"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Copy-Item<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 236.0pt;" valign="top" width="315"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Copy files and folders<o:p></o:p></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 69.2pt;" valign="top" width="92"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
del/rmdir<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 114.5pt;" valign="top" width="153"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Remove-Item<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 236.0pt;" valign="top" width="315"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Delete files and folders<o:p></o:p></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 69.2pt;" valign="top" width="92"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
ren<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 114.5pt;" valign="top" width="153"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Rename-Item<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 236.0pt;" valign="top" width="315"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Rename files and folders<o:p></o:p></div>
</td>
</tr>
<tr style="mso-yfti-irow: 6; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 69.2pt;" valign="top" width="92"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
move<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 114.5pt;" valign="top" width="153"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Move-Item<o:p></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 236.0pt;" valign="top" width="315"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Move files and folders<o:p></o:p></div>
</td>
</tr>
</tbody></table>
</span></span><br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope you now have some basic understanding about
PowerShell file and folder cmdlets to play around it. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Thank you
for reading this article. Please leave your feedback in comments below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Reference – </b><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/default.aspx">MSDN</a></div>
<div class="MsoNormal">
<b style="font-family: Calibri, sans-serif; font-size: 11pt;"><br /></b></div>
<div class="MsoNormal">
<b style="font-family: Calibri, sans-serif; font-size: 11pt;">See Also –</b></div>
<div class="MsoNormal">
<a href="https://miteshsureja.blogspot.in/2018/02/introduction-to-windows-powershell.html" style="font-family: Calibri, sans-serif; font-size: 11pt;">Introduction
to Windows PowerShell</a></div>
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-28200330577674220532018-02-24T19:18:00.000+05:302018-04-04T20:04:58.803+05:30Introduction to Windows PowerShell<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
As name suggest, Windows PowerShell is powerful command
line shell. Windows PowerShell is built on top of .Net framework and internally
uses .Net framework objects to run commands. PowerShell uses cmdlets (“command-let”)
to perform various actions. PowerShell ships with hundreds of cmdlets and you
can also create your own cmdlets as per your need. Windows PowerShell is
installed by default with Windows 7 SP1 onwards editions.<br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
PowerShell is a tool and can be used to perform day to day
activities. This tool is very useful to developers and administrators to do
their activities and they can write scripts or programs to ease their work. Like
many other shells, Windows PowerShell allows you to access file/directory
system and other computer/network/remoting related information.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In this article, I’ll explain how to start PowerShell and introduce
some basic commands for you to start with PowerShell.<o:p></o:p></div>
<h3 style="text-align: left;">
<b><br /></b><b>How to start PowerShell </b></h3>
<div class="MsoNormal" style="text-align: left;">
<br /></div>
<div class="MsoNormal">
For windows 10 – search PowerShell on search bar. (Pre-installed with windows 10)</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Once you launch PowerShell below window will appear.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-PDvSfabJuDo/WpFi1bKTDfI/AAAAAAAAAyo/I4gnXQl21DUx9tzXI0NalM6IW4f_mgsPQCEwYBhgL/s1600/PowerShell1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="173" data-original-width="602" src="https://4.bp.blogspot.com/-PDvSfabJuDo/WpFi1bKTDfI/AAAAAAAAAyo/I4gnXQl21DUx9tzXI0NalM6IW4f_mgsPQCEwYBhgL/s1600/PowerShell1.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use directory commands like DIR, CD, CD\, CD..,
MKDIR, RMDIR, COPY, DEL etc with PowerShell that you already using
with Command Prompt.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
When you execute these commands on PowerShell, internally PowerShell
uses other cmdlets. For example, when you type DIR command on PowerShell
window, it will internally use Get-ChildItem command to list all directories and files. <o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<h3 style="text-align: left;">
<b>Get-Alias</b></h3>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use Get-Alias command to check which underlying command
is mapped with alias.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div style="text-align: center;">
Get-Alias dir</div>
</div>
<div class="MsoNormal">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-igFB2EOSBdw/WpFi2YJEQ9I/AAAAAAAAAy4/KUDmKCEp5xwZ00f6a-BklZHmu2ZY2VliACEwYBhgL/s1600/PowerShell2.png" imageanchor="1" style="display: inline; margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="173" data-original-width="602" src="https://2.bp.blogspot.com/-igFB2EOSBdw/WpFi2YJEQ9I/AAAAAAAAAy4/KUDmKCEp5xwZ00f6a-BklZHmu2ZY2VliACEwYBhgL/s1600/PowerShell2.png" /></a></div>
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
To check the entire alias you can type Get-Alias command
without parameter. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div style="text-align: center;">
Get-Alias</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-X-Jh18ZYqxA/WpFi2l77pLI/AAAAAAAAAzg/o9yAB7idtfQJ66AT_r4rRV1DPF19xJeEACEwYBhgL/s1600/PowerShell3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="173" data-original-width="602" src="https://3.bp.blogspot.com/-X-Jh18ZYqxA/WpFi2l77pLI/AAAAAAAAAzg/o9yAB7idtfQJ66AT_r4rRV1DPF19xJeEACEwYBhgL/s1600/PowerShell3.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
<b>Get-Command</b></h3>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use ‘Get-Command’ to check what all commands
available in PowerShell. <o:p></o:p>You can use ‘|more’ to view details in page wise manner.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div style="text-align: center;">
Get-Command | more</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-TKk4GNXknmg/WpFi2tTPKKI/AAAAAAAAAzg/umrRBwiEFooJW0wU5NZ3fQq7J6fobOK0wCEwYBhgL/s1600/PowerShell4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="173" data-original-width="602" src="https://1.bp.blogspot.com/-TKk4GNXknmg/WpFi2tTPKKI/AAAAAAAAAzg/umrRBwiEFooJW0wU5NZ3fQq7J6fobOK0wCEwYBhgL/s1600/PowerShell4.png" /></a></div>
<div class="MsoNormal">
<span style="mso-fareast-language: EN-IN; mso-no-proof: yes;"><br /></span></div>
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can also search available command using wildcards like
below.<o:p></o:p></div>
<div>
<br /></div>
<div style="text-align: center;">
Get-Command *process</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-nJ_317FWnS4/WpFi3OnTDmI/AAAAAAAAAzg/5zQYCirsuLo9NCcnL7YyIJ0PEhM8YNUHACEwYBhgL/s1600/PowerShell5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="173" data-original-width="602" src="https://4.bp.blogspot.com/-nJ_317FWnS4/WpFi3OnTDmI/AAAAAAAAAzg/5zQYCirsuLo9NCcnL7YyIJ0PEhM8YNUHACEwYBhgL/s1600/PowerShell5.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can also use verb or noun parameters to get all the
commands with specified verb or noun<o:p></o:p></div>
<div>
<br /></div>
<div style="text-align: center;">
Get-Command -Verb get | more</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-6BoagWBtbIs/WpFi3uppR6I/AAAAAAAAAzg/qQiUFp27288qcb7K00Zno1yJVHeDIN0XwCEwYBhgL/s1600/PowerShell6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="173" data-original-width="602" src="https://1.bp.blogspot.com/-6BoagWBtbIs/WpFi3uppR6I/AAAAAAAAAzg/qQiUFp27288qcb7K00Zno1yJVHeDIN0XwCEwYBhgL/s1600/PowerShell6.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<span style="text-align: left;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<span style="text-align: left;">Get-Command -noun process</span><a href="https://2.bp.blogspot.com/-ZfJXcIrCefw/WpFi3t-ECOI/AAAAAAAAAzg/O-IHBtceFBMcVxzzloSPCUkCocYv9WwIgCEwYBhgL/s1600/PowerShell7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="173" data-original-width="602" src="https://2.bp.blogspot.com/-ZfJXcIrCefw/WpFi3t-ECOI/AAAAAAAAAzg/O-IHBtceFBMcVxzzloSPCUkCocYv9WwIgCEwYBhgL/s1600/PowerShell7.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use below command to check total commands currently available
in PowerShell.<o:p></o:p></div>
<div>
<br /></div>
<div style="text-align: center;">
(Get-Command).Count</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-QNQ6iXdUK8M/WpFi39fhGnI/AAAAAAAAAzg/5alaKlaAZscGAjYvDBz0Luc0ha6WVO5EQCEwYBhgL/s1600/PowerShell8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="113" data-original-width="602" src="https://2.bp.blogspot.com/-QNQ6iXdUK8M/WpFi39fhGnI/AAAAAAAAAzg/5alaKlaAZscGAjYvDBz0Luc0ha6WVO5EQCEwYBhgL/s1600/PowerShell8.png" /></a></div>
<div class="MsoNormal">
<b><br /></b></div>
<h3 style="text-align: left;">
<b>Get-Help </b></h3>
<div class="MsoNormal">
You can use Get-Help command to get help about any commands
in PowerShell.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div style="text-align: center;">
Get-Help Get-ChildItem | more</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-QYdruSm0CuE/WpFi4Q7GAPI/AAAAAAAAAzk/yMtN3nKZ1gQIAfKMnB0L7MqNvxLm0kMyACEwYBhgL/s1600/PowerShell9.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="261" data-original-width="602" src="https://4.bp.blogspot.com/-QYdruSm0CuE/WpFi4Q7GAPI/AAAAAAAAAzk/yMtN3nKZ1gQIAfKMnB0L7MqNvxLm0kMyACEwYBhgL/s1600/PowerShell9.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If you would like to know help only about specific parameter of that command then you can use below command. <o:p></o:p></div>
<div>
<br /></div>
<div style="text-align: center;">
Get-Help Get-ChildItem -Parameter filter</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-wSbLZYVCC9o/WpFi1oVWTtI/AAAAAAAAAzg/4DcPXcCsMtExr4-6U2lrP32Y1aIhyfu3QCEwYBhgL/s1600/PowerShell10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="193" data-original-width="602" src="https://2.bp.blogspot.com/-wSbLZYVCC9o/WpFi1oVWTtI/AAAAAAAAAzg/4DcPXcCsMtExr4-6U2lrP32Y1aIhyfu3QCEwYBhgL/s1600/PowerShell10.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can get help of any command in all the details with example
using below command.<o:p></o:p></div>
<div>
<br /></div>
<div style="text-align: center;">
Get-Help Get-ChildItem -Full |more</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-X6JIH543nno/WpFi1mqQvmI/AAAAAAAAAzc/VFvatHYJwLAUDBt5kt0motpmHtQ3u0pmQCEwYBhgL/s1600/PowerShell11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="193" data-original-width="602" src="https://2.bp.blogspot.com/-X6JIH543nno/WpFi1mqQvmI/AAAAAAAAAzc/VFvatHYJwLAUDBt5kt0motpmHtQ3u0pmQCEwYBhgL/s1600/PowerShell11.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If your PowerShell help is not updated since long time then
you can use below command to update help. This command will take some time to
execute and download help.<o:p></o:p></div>
<div>
<br /></div>
<div style="text-align: center;">
Update-Help -Force</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-OsmeYMo09tU/WpFi2N2Z51I/AAAAAAAAAzg/k608iZ1AXUAKgjaM5ici5q3Wbce0nb5vgCEwYBhgL/s1600/PowerShell12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="193" data-original-width="602" src="https://2.bp.blogspot.com/-OsmeYMo09tU/WpFi2N2Z51I/AAAAAAAAAzg/k608iZ1AXUAKgjaM5ici5q3Wbce0nb5vgCEwYBhgL/s1600/PowerShell12.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope you now have some basic knowledge about how to start PowerShell and how to search various commands and get help about them. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Thank you for reading this article. Please leave your
feedback in comments below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Reference – <o:p></o:p></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/default.aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p><br /></o:p></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com2tag:blogger.com,1999:blog-3223444668744241586.post-74486920754931850792017-08-27T18:26:00.000+05:302018-12-18T17:22:03.224+05:30Iterator Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Interpreter design pattern is used to access elements
of collection sequentially. Iterator Pattern falls under behavioral pattern of
GOF (Gang of Four) patterns.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
When to use – </h3>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Iterator pattern provides a way to access elements of
collection sequentially without exposing its representation. The client code
should not be concerned about the actual implementation of traversing the
elements of collection. The iterator pattern encapsulates such implementation
and provides common way to traverse different types of collection. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
Major components of Iterator pattern – </h3>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Client –</b> This class creates an instance of collection
object (ConcreteAggreate) and iterator object to perform next operation on
collection to retrieve next items until all items are retrieved.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Iterator –</b> The Iterator interface defines all the
methods to traverse the collection. <o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>ConcreteIterator – </b>This class implements the Iterator
interface and provides actual implementation of how the elements are traverse
in collection.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Aggregate –</b> This is an abstract class. This class
defines method to create an Iterator. <o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>ConcreteAggregate –</b> This class will be derived from
abstract class Aggregate and implements and creates an iterator object.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Let’s have a look on below example of Iterator pattern.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Client</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Create myObject collection and items to it.</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyObjectCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;"> aggr = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyObjectCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
aggr.Add(10);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
aggr.Add(-20);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> aggr.Add(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Test"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
aggr.Add(33);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> aggr.Add(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Iterator"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
aggr.Add(52.6);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Get Iterator and iterator all items</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Iterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"> itor = aggr.GetIterator();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">while</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (!itor.IsDone())<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(itor.CurrentItem().ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
itor.Next();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Iterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Iterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> First();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Next();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> CurrentItem();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">bool</span><span style="font-family: "consolas"; font-size: 9.5pt;"> IsDone();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//ConcreteIterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyIterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Iterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyObjectCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;"> aggregate;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> index;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> MyIterator(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyObjectCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;"> aggr)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">.aggregate = aggr;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> index = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> CurrentItem()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> aggregate[index];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> First()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> aggregate[0];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">bool</span><span style="font-family: "consolas"; font-size: 9.5pt;"> IsDone()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> index >= aggregate.Count ;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Next()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> retVal = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (index < aggregate.Count)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> retVal
= aggregate[index++];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> retVal;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Aggregate</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Aggregate</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Iterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"> GetIterator();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//ConcreteAggregate</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyObjectCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Aggregate</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ArrayList</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myList = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ArrayList</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Iterator</span><span style="font-family: "consolas"; font-size: 9.5pt;"> GetIterator()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyIterator</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span style="font-family: "consolas"; font-size: 9.5pt;">[</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> index]<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;"> { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myList[index]; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Count<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;"> { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myList.Count; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Add (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> obj)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
myList.Add(obj);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output – </b><o:p></o:p></div>
<div class="MsoNormal">
<v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_21" o:spid="_x0000_i1026" style="height: 226.5pt; mso-wrap-style: square; visibility: visible; width: 294pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/Mitesh/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape><o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-yjLSorhn060/WTzzDavFlfI/AAAAAAAAAxQ/hsdcD2qh_iUGMSbjE-TQvhJUed-5fiZvgCLcB/s1600/IteratorPattern1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="302" data-original-width="392" height="307" src="https://4.bp.blogspot.com/-yjLSorhn060/WTzzDavFlfI/AAAAAAAAAxQ/hsdcD2qh_iUGMSbjE-TQvhJUed-5fiZvgCLcB/s400/IteratorPattern1.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in this example, I created an instance of MyObjectCollection
class and added few data items to it. I used ArrayList to add any object to the
collection. You can use any type as per your requirement. You can
also use generic type here. I get instance of iterator from MyObjectCollection
instance and I’m iterating all the items of my collection using iterator instance. I’m
using Iterator.Next() method to iterate to next item and Iterator.CurrentItem()
to get current item of collection. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The Iterator patterns are available inbuilt with Microsoft .Net.
It provides two interfaces IEnumerable
and IEnumerator. You can make your Iterator collection implementing IEnumerable
interface. Almost all majorly used collections available in .Net implements
IEnumerable interface. The few examples of inbuilt collections are
List<T>, Dictionary<T>, ArrayList, SortedList etc. See below definition
of both the interfaces.<o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IEnumerable</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IEnumerator</span><span style="font-family: "consolas"; font-size: 9.5pt;">
GetEnumerator();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IEnumerator</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Current { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">bool</span><span style="font-family: "consolas"; font-size: 9.5pt;"> MoveNext();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Reset();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Let’s have a look on below example of custom collection implementing
IEnumerable interface.<o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;"> coll = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> item </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="font-family: "consolas"; font-size: 9.5pt;"> coll)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(item.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyCollection</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IEnumerable</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;">> myList = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;">>() { 2, 5, 3, 6, 4, 9 };<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IEnumerator</span><span style="font-family: "consolas"; font-size: 9.5pt;"> GetEnumerator()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> item </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myList)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">yield</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> item;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b>Output – </b><o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-S3pv9vBM5IM/WTzzDZ_lrzI/AAAAAAAAAxM/mzO4cqTkLO87Bt0-7lTy-m2gMCCbvc1TwCEw/s1600/IteratorPattern2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="243" data-original-width="306" height="317" src="https://1.bp.blogspot.com/-S3pv9vBM5IM/WTzzDZ_lrzI/AAAAAAAAAxM/mzO4cqTkLO87Bt0-7lTy-m2gMCCbvc1TwCEw/s400/IteratorPattern2.png" width="400" /></a></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal">
<v:shape id="Picture_x0020_24" o:spid="_x0000_i1025" style="height: 182.25pt; mso-wrap-style: square; visibility: visible; width: 229.5pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/Mitesh/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png">
</v:imagedata></v:shape><o:p></o:p></div>
<div class="MsoNormal">
As per above code, IEnumerable provides GetEnumerator method
which will return IEnumerator list. This list iterated through foreach loop in
main class. The <b>yield return</b> keyword can be used to provide a value to
the enumerator object.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can download full code of Iterator Design Pattern from <a href="https://gist.github.com/miteshsureja/7e1cad49c704a6b40c4510771a93b1ec">here</a>. You can download code for Iterator Pattern implementing IEnumerable interface from <a href="https://gist.github.com/miteshsureja/d66004de5029d9d590818baf565bf464">here</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope this article helps you to know more about Iterator
Design Pattern. Please leave your feedback in comments section below.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>References – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="http://www.dofactory.com/net/iterator-design-pattern">DoFactory</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 6;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 7;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 8;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 9;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 10;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 11; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-27606837159402031562017-07-23T09:43:00.000+05:302018-12-18T17:22:19.086+05:30Interpreter Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Interpreter design pattern majorly used with languages and
notations. This pattern can be useful to convert from one language to another.
This pattern allows to write multiple rules or grammar for language to
interpret and extend easily. Interpreter Pattern falls under behavioral pattern
of GOF (Gang of Four) pattern.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
When to use – </h3>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Interpreter Pattern is useful to interpret languages where
rules/grammar are complex part which you can easily extend using this pattern.
The pattern works on hierarchy of expression. The expression can be classified
as Terminal or Non-Terminal. The terminal expression is standalone and can be
Interpreted immediately. The non-terminal expression may contain one or more
terminal or non-terminal expressions. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
Major components of Interpreter pattern – </h3>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Client –</b> This client class responsible for build context,
terminal and non-terminal expression objects. It also responsible to make call
to interpret method of respective expression.<o:p></o:p></div>
<div class="MsoNormal">
<b>Context –</b> This is context class used to store
input/output data and other information required by expression. <o:p></o:p></div>
<div class="MsoNormal">
<b>Expression –</b> This is an interface which defines
Interpret method which will be implemented by sub classes.<o:p></o:p></div>
<div class="MsoNormal">
<b>TerminalExpression –</b> This class will implement
Expression interface and implements Interpret method. <o:p></o:p></div>
<div class="MsoNormal">
<b>NonTerminalExpression –</b> This class will implement
Expression interface and implements Interpret method similar to TerminalExpression
class. This class may contain one or more terminal or non-terminal
expressions and invoke Interpret method respectively.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
There are many examples available for Interpreter design
pattern. I would like to take simple example to understand the intent and
implementation for Interpreter design pattern. In my example I would like
interpret numbers to words. So I’ll provide number as input and that will be
interpreted by pattern to words. Similarly, you can add multiple rules to
interpret as per your requirement.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
See below example of Interpreter pattern.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Client class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;"> ctx = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;">(5642);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"> exp1 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">WordExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
exp1.Interpret(ctx);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;"> ctx1 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;">(397);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SubExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"> subExp = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SubExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
subExp.Expression1 = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">WordExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
subExp.Interpret(ctx1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Context class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Input { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Context(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> inputValue)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Input =
inputValue;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Expression Interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Interpret(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;"> context);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Terminal expression class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">WordExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Dictionary</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">> numberWord = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Dictionary</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">>() {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> { 1, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"One"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 2, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Two"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 3, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Three"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 4, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Four"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 5, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Five"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> },<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> { 6, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Six"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 7, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Seven"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 8, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Eight"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 9, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Nine"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> }, { 0, </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Zero"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> } };<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Interpret(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;"> context)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Write(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Input Value {0} - "</span><span style="font-family: "consolas"; font-size: 9.5pt;">, context.Input);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> value = context.Input.ToString();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (value.Length > 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">char</span><span style="font-family: "consolas"; font-size: 9.5pt;"> chr </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="font-family: "consolas"; font-size: 9.5pt;"> value)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> number = (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;">)</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Char</span><span style="font-family: "consolas"; font-size: 9.5pt;">.GetNumericValue(chr);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Write(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"{0} "</span><span style="font-family: "consolas"; font-size: 9.5pt;">, numberWord[number]);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Non-terminal expression class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SubExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IExpression</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Expression1 { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Interpret(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Context</span><span style="font-family: "consolas"; font-size: 9.5pt;"> context)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Running
Sub Expression 1"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
Expression1.Interpret(context);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output – </b><o:p></o:p></div>
<div class="MsoNormal">
<v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_19" o:spid="_x0000_i1025" style="height: 157.5pt; mso-wrap-style: square; visibility: visible; width: 468pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/Mitesh/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape><o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-pxbqW5gjAJA/WRMlG6prhiI/AAAAAAAAAvw/iyvxfKIMaF4N5lT7c5Y0ADUg_DzkIdLFgCLcB/s1600/InterpreterDesignPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="132" src="https://2.bp.blogspot.com/-pxbqW5gjAJA/WRMlG6prhiI/AAAAAAAAAvw/iyvxfKIMaF4N5lT7c5Y0ADUg_DzkIdLFgCLcB/s400/InterpreterDesignPattern.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in this example, user set context value to
5642 and Interpret it to words via calling WordExpression which is terminal expression.
User also set value another context object ctx1 and set 397 value to Interpret
to word via calling SubExpression which is non-terminal expression and contains
reference of terminal expression. You can add multiple conversion rules and
interpret methods via adding multiple expressions as per your requirement.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can download full code from <a href="https://gist.github.com/miteshsureja/6cc86c51ae3eb75f7ad75aa5246f7a95">Gist</a>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope this article helps you to know more about Interpreter
Design Pattern. Please leave your feedback in comments section below.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>References – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="http://www.dofactory.com/net/interpreter-design-pattern">DoFactory</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 6;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 7;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 8;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 9;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 10;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 11; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-40387532722943602022017-06-10T10:42:00.001+05:302017-06-10T10:42:38.937+05:30Async and Await - Asynchronous Programming<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Microsoft
introduced Async and Await keyword with .Net framework 4.5. Async and Await
keyword can be used to improve your application’s overall responsiveness by
using asynchronous programming. Asynchronous programming is essential when your
application is doing some activity which makes UI unresponsive. You can move
that activity to execute asynchronously so it doesn’t block UI and your application will be
responsive.</div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN">You should
consider using Async and Await when you want to keep your application
responsive. Async method provides easier way to do long running work in
background without blocking thread. Ideally method is decorated with Async
keyword should contain one await statement. If you don’t write await statement
in Async method, it won’t give compile error but execute as synchronous method.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<h3 style="text-align: left;">
<span lang="EN-IN">Below are
few things to remember about Async and Await method.</span></h3>
<div class="MsoListParagraphCxSpFirst" style="text-align: left; text-indent: -18pt;">
</div>
<ul style="text-align: left;">
<li><span lang="EN-IN" style="text-indent: -18pt;">The method should contain async
modifier.</span></li>
<li><span lang="EN-IN" style="text-indent: -18pt;">Async method return type should be</span></li>
<ul>
<li><span lang="EN-IN" style="text-indent: -18pt;">Task</span></li>
<li><span lang="EN-IN" style="text-indent: -18pt;">Task<T></span></li>
<li><span lang="EN-IN" style="text-indent: -18pt;">Void</span></li>
</ul>
<li><span lang="EN-IN" style="text-indent: -18pt;">The method usually includes at least
one await expression.</span></li>
</ul>
<!--[if !supportLists]--><br />
<div class="MsoNormal">
<span lang="EN-IN">Let’s have
a look on below example.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<b>Code –</b></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">partial</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MainWindow</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Window</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> MainWindow()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> InitializeComponent();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">async</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> DoSum(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> number)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Task started"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> result = Calculate(number);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Task completed"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (!result.IsCompleted)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">await</span><span style="font-family: "consolas"; font-size: 9.5pt;"> result;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Answer.Text = result.Result.ToString();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Format(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Answer is {0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, result.Result.ToString()));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">async</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Task</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;">> Calculate(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> number)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> sum = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Task</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myTask = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Task</span><span style="font-family: "consolas"; font-size: 9.5pt;">(() =><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">for</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> i = 1; i
<= number; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> System.Threading.</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Sleep(1000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> sum += i;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> });<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> myTask.Start();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">await</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myTask;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> sum;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;">
Button_Click(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">object</span><span style="font-family: "consolas"; font-size: 9.5pt;"> sender, </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">RoutedEventArgs</span><span style="font-family: "consolas"; font-size: 9.5pt;"> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (!</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">.IsNullOrEmpty(Input.Text))<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sum process started"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> number = </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Convert</span><span style="font-family: "consolas"; font-size: 9.5pt;">.ToInt32(Input.Text);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> DoSum(number);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sum process completed"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">}</span><span lang="EN-IN"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><b>Output – </b><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">Sum
process started<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">Task started<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">Task
completed<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">Sum
process completed<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">Answer is 55</span><span lang="EN-IN"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-kEOEnzYP9gw/WSwgGBP8-TI/AAAAAAAAAww/v5KdV_Ei2G4lyCGqiaLAWD0KfQ3ZvlxYACLcB/s1600/AsyncAwait.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="343" data-original-width="511" height="267" src="https://2.bp.blogspot.com/-kEOEnzYP9gw/WSwgGBP8-TI/AAAAAAAAAww/v5KdV_Ei2G4lyCGqiaLAWD0KfQ3ZvlxYACLcB/s400/AsyncAwait.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN">As you can
see in output, the UI doesn’t block and cursor immediately return to UI. The sum is calculated and updated asynchronously after all execution completed.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN">Now let’s
make little change to this programme and use Task.Wait() method to complete task instead
of await. I added <i>myTask.wait()</i> instead of <i>await myTask</i>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">async</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Task</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;">> Calculate(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> number)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> sum = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Task</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myTask = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Task</span><span style="font-family: "consolas"; font-size: 9.5pt;">(() =><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">for</span><span style="font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> i = 1; i
<= number; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> System.Threading.</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="font-family: "consolas"; font-size: 9.5pt;">.Sleep(1000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> sum += i;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> });<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> myTask.Start();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <b>myTask.Wait();<o:p></o:p></b></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> sum;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">}</span><span lang="EN-IN"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><b>Output – </b><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">Sum
process started<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">Task
started<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">Task
completed<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="font-family: "consolas"; font-size: 9.5pt;">Answer is 55<o:p></o:p></span></b></div>
<div class="MsoNormal">
<b><span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">Sum process completed</span></b><b><span lang="EN-IN"><o:p></o:p></span></b></div>
<div class="MsoNormal">
<b><span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"><br /></span></b></div>
<div class="MsoNormal">
As you can
see in output, till the sum is calculated UI is blocked and after that sum process is completed. That means myTask.wait() blocks the thread until task completed.</div>
<div class="MsoNormal">
<b><span lang="EN-IN"><br /></span></b></div>
<h3 style="text-align: left;">
<b><span lang="EN-IN">Difference
between await and task.wait() - </span></b></h3>
<div class="MsoNormal">
Task.wait
blocks the thread and await doesn’t block thread and UI.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope this
article helps you to know more about async and await. Please leave your
feedback in comments below.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can download full code from <a href="https://gist.github.com/miteshsureja/bd8afe77094f06282f8c6cdd9a0b6f94">Gist</a>.</div>
<div>
<br /></div>
<div class="MsoNormal">
<b><span lang="EN-IN">See also
– <o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/11/parallelfor-and-parallelforeach-task.html">Parallel.For andParallel.Foreach</a><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/04/parallelinvoke-task-parallel-library.html">Parallel.Invoke</a><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/12/how-to-handle-exception-in-parallelfor.html">How to handleexception while working with Threading?</a><o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-89129743196097944802017-05-28T10:06:00.000+05:302018-12-18T17:22:33.603+05:30Command Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
Command design pattern can be used while designing Menu
Items, Tool Bar, Status Bar etc functionalities in application. This pattern
can also be useful to provide Undo/Redo like functionalities in application. Command Pattern falls under behavioral pattern of GOF (Gang of
Four) pattern.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
When to use –</h3>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Command Pattern encapsulates request under an object and
pass it to the invoker object. The invoker object will pass it to proper
command object which will execute the command. This pattern commonly used in UI
Buttons, Menu, Tool Bar, Status Bar, Progress Bar etc in many applications.
This pattern can also be used to provide multiple level Undo/Redo
functionalities for applications. </div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
Major components of command pattern –</h3>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Command –</b> This is an interface which specifies
execute action. <o:p></o:p></div>
<div class="MsoNormal">
<b>Concrete Command –</b> This is concrete class which provides
implementation for execute operation. <o:p></o:p></div>
<div class="MsoNormal">
<b>Client –</b> This class creates and executes commands. <o:p></o:p></div>
<div class="MsoNormal">
<b>Invoker –</b> This class will ask the command to execute their actions. <o:p></o:p></div>
<div class="MsoNormal">
<b>Receiver –</b> This class knows how to perform action
attached with request.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
See below example of Command pattern.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Command interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">interface</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ICommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Execute();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Concrete Command class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BuyCommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ICommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Product</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Product;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Buy Command"</span><span style="font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> BuyCommand(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Product</span><span style="font-family: "consolas"; font-size: 9.5pt;"> product)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Product =
product;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Execute()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Executing
{0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, Name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
Product.Buy();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Concrete Command class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SellCommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ICommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Product</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Product;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sell Command"</span><span style="font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> SellCommand(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Product</span><span style="font-family: "consolas"; font-size: 9.5pt;"> product)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Product =
product;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Execute()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Executing
{0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, Name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
Product.Sell();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Receiver class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Product</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Quantity { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Product(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> prodName, </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> qty)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Name =
prodName;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Quantity =
qty;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Buy()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Purchased {0} quantities of {1}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, Quantity, Name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Sell()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sold
{0} quantities of {1}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">,
Quantity, Name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Invoker class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Portal</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> PortalName { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Portal(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> name)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> PortalName
= name;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ICommand</span><span style="font-family: "consolas"; font-size: 9.5pt;">> orderList = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="font-family: "consolas"; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ICommand</span><span style="font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> AddOrder(</span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ICommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"> cmd)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
orderList.Add(cmd);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> ExecuteOrder()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Executing
all orders from {0}"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, PortalName);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> order </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span><span style="font-family: "consolas"; font-size: 9.5pt;"> orderList)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
order.Execute();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Client class</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Product</span><span style="font-family: "consolas"; font-size: 9.5pt;"> laptop = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Product</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Laptop"</span><span style="font-family: "consolas"; font-size: 9.5pt;">, 2);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BuyCommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"> buyLaptop = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">BuyCommand</span><span style="font-family: "consolas"; font-size: 9.5pt;">(laptop);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SellCommand</span><span style="font-family: "consolas"; font-size: 9.5pt;"> sellLaptop = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SellCommand</span><span style="font-family: "consolas"; font-size: 9.5pt;">(laptop);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Portal</span><span style="font-family: "consolas"; font-size: 9.5pt;"> portal = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Portal</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Amazon.in"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
portal.AddOrder(buyLaptop);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> portal.AddOrder(sellLaptop);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
portal.ExecuteOrder();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output – </b><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-czpSKEcRD1Y/WQcvxc3GAJI/AAAAAAAAAvU/jxUxjhgLwcIG6SVkKy3aiIxOeXebjs2UACLcB/s1600/CommandPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://1.bp.blogspot.com/-czpSKEcRD1Y/WQcvxc3GAJI/AAAAAAAAAvU/jxUxjhgLwcIG6SVkKy3aiIxOeXebjs2UACLcB/s1600/CommandPattern.png" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_17" o:spid="_x0000_i1025" style="height: 129pt; mso-wrap-style: square; visibility: visible; width: 374.25pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/Mitesh/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape><o:p></o:p></div>
<div class="MsoNormal">
As you can see in this example, user can create receiver
object and also create objects for multiple commands. In this example Product
is Receiver class and Portal is invoker class. The portal object will invoke BuyCommand and SellCommand as per their order.
This example can also be extended to add Undo/Redo functionality via using Stack
and modifying invoker class.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can download full code from <a href="https://gist.github.com/miteshsureja/9efcb3011547b89bbebd6aba96787b70">Gist</a>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope this article helps you to know more about Command Design
Pattern. Please leave your feedback in comments section below.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>References – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="http://www.dofactory.com/net/command-design-pattern">DoFactory</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-13939005778403990972017-04-30T19:47:00.000+05:302017-04-30T19:47:43.174+05:30Parallel.Invoke – Task Parallel Library<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
In my previous article, I explained about <a href="http://miteshsureja.blogspot.in/2016/11/parallelfor-and-parallelforeach-task.html">Parallel.For and Parallel.Foreach Loop</a> in detail. In this article I will explain, how to use
Parallel.Invoke to do multiple tasks concurrently.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Parallel.For and Parallel.Foreach can be used to loop
asynchronously, but Parallel.Invoke can be used to do multiple tasks
concurrently. Parallel.Invoke method is part of System.Threading.Tasks and
accept array of action delegates as input and run all action delegates in parallel. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use Parallel.Invoke method when you want to do multiple
tasks in parallel. This will be always faster than calling all
the tasks synchronously. Task parallel library internally manages to divide and
run multiple task in different threads in parallel. It also manages thread
scheduling and scaling automatically as per the number of cores on your
computer. There is no guarantee in which order all your tasks are executed and
completed. See below example.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">namespace</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> ParallelInvoke<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> System.Threading;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> System.Threading.Tasks;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Parallel</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.Invoke(() =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting first task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoFirstTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}, () =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting second task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoSecondTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}, () =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting third task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoThirdTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> );<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoFirstTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>() { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Total numbers -
{0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, numbers.Count());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"First task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoSecondTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>() { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sum of all numbers -
{0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, numbers.Sum());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Second task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoThirdTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>() { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Average of all
numbers - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">,
numbers.Average());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Third task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output –</b></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-z3J71buO9yQ/V5HzF8H061I/AAAAAAAAAtA/gqR7bjHZyAYiIBh2x_5d5eBS5CXcSuuQACLcB/s1600/ParallelInvoke.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="201" src="https://2.bp.blogspot.com/-z3J71buO9yQ/V5HzF8H061I/AAAAAAAAAtA/gqR7bjHZyAYiIBh2x_5d5eBS5CXcSuuQACLcB/s400/ParallelInvoke.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in above example, there are three delegates assigned
to Parallel.Invoke method to execute. As i mentioned earlier that all delegates runs in different threads concurrently hence the order of task execution is different. You
can also check managed thread id for each task on which they are executing.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
Cancelling Parallel.Invoke method – </h3>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can cancel any task which is running via Parallel.Invoke
method based on certain condition. Parallel.Invoke method has overload which
accepts ParallelOptions in which you can specify cancellation token. See
Parallel.Invoke overload method which accept ParallelOptions as parameter.<o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"><br /></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"> Invoke(</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"><b>ParallelOptions</b></span><span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"><b> parallelOptions</b>, </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">params</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">Action</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">[] actions);</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
See below little bit of modified code to accept cancel request.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">namespace</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> ParallelInvoke<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> System.Threading;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> System.Threading.Tasks;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">CancellationTokenSource</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> cancelToken = </span></b><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">CancellationTokenSource</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ParallelOptions</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> options = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ParallelOptions</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><b>
options.CancellationToken = cancelToken.Token;</b><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">try</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Parallel</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.Invoke(options, () =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting first task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoFirstTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }, () =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting second task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoSecondTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> <b>options.CancellationToken.ThrowIfCancellationRequested();<o:p></o:p></b></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }, () =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting third task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoThirdTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">catch</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (</span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">OperationCanceledException</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> ex)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.ForegroundColor = </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ConsoleColor</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.Red;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span></b><b><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Error Message -
{0}"</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">,
ex.Message);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoFirstTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>() { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Total numbers -
{0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, numbers.Count());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"First task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoSecondTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (numbers.Count <= 0)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> cancelToken.Cancel();<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sum of all numbers -
{0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, numbers.Sum());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Second task completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoThirdTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>() { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Average of all
numbers - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">,
numbers.Average());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Third task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output –</b></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-8cnwV65RSNw/V5HzF9KAicI/AAAAAAAAAtE/7m1tbzInBDEuyTUSFPRTBWOO1vc0sHjKgCEw/s1600/ParallelInvokeCancel.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="267" src="https://4.bp.blogspot.com/-8cnwV65RSNw/V5HzF9KAicI/AAAAAAAAAtE/7m1tbzInBDEuyTUSFPRTBWOO1vc0sHjKgCEw/s400/ParallelInvokeCancel.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in above example, CancellationTokenSource and
ParallelOptions used to cancel Parallel.Invoke method. This cancellation process
is similar to the cancellation process for Parallel.For and Parallel.Foreach.
In above example, DoSecondTask method has condition to cancel the
Parallel.Invoke method. Once this condition is true, the parallel options will
throw OperationCanceledException and Parallel.Invoke method will be cancelled.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
Exception Handling – </h3>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Exception handling in Parallel.Invoke is also similar to
Parallel.For and Parallel.Foreach. There is no special mechanism provided by
Task Parallel Library to handle exceptions. We can use try catch block to catch
exception inside parallel.invoke method. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
See below modified code to handle
exceptions.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">namespace</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> ParallelInvoke<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> System.Collections.Concurrent;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> System.Threading;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> System.Threading.Tasks;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ConcurrentQueue</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Exception</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> exceptionQueue = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ConcurrentQueue</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Exception</span></b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><b>>();</b><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">try</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Parallel</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.Invoke(() =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting first task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoFirstTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }, () =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting second task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoSecondTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (exceptionQueue.Count >
0) </span></b><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">throw</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">AggregateException</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">(exceptionQueue);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }, () =><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Starting third task"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Thread ID - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Thread</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.CurrentThread.ManagedThreadId);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoThirdTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> );<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">catch</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (</span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">AggregateException</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> ex)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">foreach</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (</span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Exception</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> e </span></b><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">in</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> ex.InnerExceptions)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.ForegroundColor = </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ConsoleColor</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.Red;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span></b><b><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Error Message -
{0}"</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">,
e.InnerException.Message);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoFirstTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>() { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Total numbers -
{0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, numbers.Count());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"First task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoSecondTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (numbers.Count <= 0)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> exceptionQueue.Enqueue(</span></b><b><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Exception</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">(</span></b><b><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"numbers list is
empty"</span></b><b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">));<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Sum of all numbers -
{0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, numbers.Sum());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Second task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> DoThirdTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">> numbers = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">List</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">>() { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Average of all
numbers - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">,
numbers.Average());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Third task
completed."</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output –</b></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-QYdzhkQSbB8/V5HzF0E0RUI/AAAAAAAAAtI/lrIm2J0pc4Ir7CG-7ZeilqG892fk5NYrQCEw/s1600/ParallelInvokeException.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="267" src="https://3.bp.blogspot.com/-QYdzhkQSbB8/V5HzF0E0RUI/AAAAAAAAAtI/lrIm2J0pc4Ir7CG-7ZeilqG892fk5NYrQCEw/s400/ParallelInvokeException.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in above example, exception thrown from
DoSecondTask method. ConcurrentQueue has been used to safely add multiple
exception thrown from multiple threads. Parallel.Invoke method covered with try catch block. So if any exception thrown from delegate, this will enqueue inside ConcurrentQueue and once all tasks are
completed, you can check which exceptions occurred by iterating all the InnerException.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br />
You can download full code from <a href="https://gist.github.com/miteshsureja/d89a6faf323f5b9fff35d7a72bd00d1e">Gist</a>.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope this article helps you to know more about
Parallel.Invoke and Task Parallel Library. Please leave your feedback in
comments below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>References – </b><o:p></o:p></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/library/dd460705(v=vs.110).aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>See Also –</b></div>
<div class="MsoNormal">
<a href="http://miteshsureja.blogspot.in/2016/11/parallelfor-and-parallelforeach-task.html">Parallel.For and Parallel.Foreach – Task Parallel Library</a><br />
<a href="http://miteshsureja.blogspot.in/2017/02/how-to-cancel-parallelfor-and.html">How to cancel a Parallel.For and Parallel.Foreach loop – Task Parallel Library</a><br />
<a href="http://miteshsureja.blogspot.in/2016/12/how-to-handle-exception-in-parallelfor.html">How to handle exception in Parallel.For and Parallel.Foreach loop? – Task Parallel Library</a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-70473836037987565072017-03-26T11:29:00.000+05:302018-12-18T17:22:52.591+05:30Chain of Responsibility Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Chain of Responsibility design pattern can be used to
process multiple types of request and each type of request can be handled by
multiple handlers. Chain of Responsibility design pattern falls under behavioral pattern of
GOF (Gang of Four) pattern.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<h3 style="text-align: left;">
<b>When to use – </b></h3>
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Chain of Responsibility pattern can be used when multiple types
of request are handled by multiple handlers. If one handler is not able to
process your request, then it will pass to another handler. In this pattern,
each handler has reference to next handler. If one handler can’t process request,
then it passes it to next handler. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<h3 style="text-align: left;">
Major components of Chain of Responsibility pattern – </h3>
<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>Handler –</b> This is an abstract class. This holds
reference of next handler and also has abstract method to handler request. <o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
<b>Concrete Handler –</b> This is concrete class which inherits
from Handler class and implements abstract method to handle request. <o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
<b>Client –</b> This is client class and create requests and
passes to different handlers in the chain of responsibility pattern.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
See below example of Chain of responsibility pattern.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><o:p></o:p></div>
<div class="MsoNormal">
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Handler class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">abstract</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
HandleRequest(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> requestAmount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">protected</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
NextApprover;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
SetNextApprover(</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> approver)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> NextApprover = approver;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}</span><span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Concrete Handler
class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Manager</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
HandleRequest(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> requestAmount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (requestAmount < 100000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Manager
has approved amount - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, requestAmount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
(NextApprover != </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
NextApprover.HandleRequest(requestAmount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">}</span><span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Concrete Handler
class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SeniorManager</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
HandleRequest(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> requestAmount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (requestAmount < 200000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Senior
Manager has approved amount - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, requestAmount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
(NextApprover != </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
NextApprover.HandleRequest(requestAmount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Concrete Handler
class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Director</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> : </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">override</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
HandleRequest(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> requestAmount)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (requestAmount < 300000)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Director
has approved amount - {0}"</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">, requestAmount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">else</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">if</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
(NextApprover != </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
NextApprover.HandleRequest(requestAmount);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Client Class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Program</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Main(</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Create classes of chain of
responsibility</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Manager
= </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Manager</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
SeniorManager = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SeniorManager</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Approver</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
Director = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Director</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Set next request approver or
handler</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Manager.SetNextApprover(SeniorManager);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;">
SeniorManager.SetNextApprover(Director);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//Create
requests.</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Manager.HandleRequest(75000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Manager.HandleRequest(150000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> Manager.HandleRequest(250000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Console</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: white; font-family: "consolas"; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal">
<o:p></o:p></div>
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Output – </b><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-Qahb39up1cE/WMYyew7c33I/AAAAAAAAAu0/PyGjXWMcTDEkGcMC0xx-JLshIp9q7X-_QCLcB/s1600/ChainOfResponsibility%2BPattern.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="92" src="https://1.bp.blogspot.com/-Qahb39up1cE/WMYyew7c33I/AAAAAAAAAu0/PyGjXWMcTDEkGcMC0xx-JLshIp9q7X-_QCLcB/s400/ChainOfResponsibility%2BPattern.png" width="400" /></a></div>
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in above example, Approver abstract class is
inherited to respective approvers. In client class, instance of each
concrete handler class created as handler class and set its next approver or handler for
each concrete handler class. At last manager class handle request method called all
the times. If manager class not able to process request it will pass
on to its next approver and so on in chain of responsibilities classes.<o:p></o:p></div>
<div class="MsoNormal">
<br />
<div class="MsoNormal">
You can download full code from <a href="https://gist.github.com/miteshsureja/86dc8742c2f23e17e397fa82ee40c2c5">Gist</a>.<o:p></o:p></div>
<br /></div>
<div class="MsoNormal">
I hope this article helps you to know more about Chain of
Responsibility pattern. Please leave your feedback in comments section below.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>References – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="http://www.dofactory.com/net/chain-of-responsibility-design-pattern">DoFactory</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>See Also – <o:p></o:p></b></div>
<div class="MsoNormal">
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Creational Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Structural Patterns<o:p></o:p></span></b></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<b><span lang="EN-IN">Behavioral Patterns<o:p></o:p></span></b></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/05/singleton-pattern-singleton-pattern.html">Singleton
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/adapter-pattern.html">Adapter
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/03/chain-of-responsibility-pattern.html">Chain
of Responsibility Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/thread-safe-singleton-pattern-multi.html">Thread
safe singleton pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/07/bridge-pattern.html">Bridge
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2017/05/command-pattern.html">Command
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/factory-pattern.html">Factory
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/composite-pattern.html">Composite
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/07/interpreter-pattern.html">Interpreter
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/abstract-factory-pattern.html">Abstract
Factory Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/decorator-pattern.html">Decorator
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2017/08/iterator-pattern.html">Iterator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/builder-pattern.html">Builder
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/08/facade-pattern.html">Facade
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.in/2018/04/mediator-pattern.html">Mediator
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 6;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/06/prototype-pattern.html">Prototype
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/09/flyweight-pattern.html">Flyweight
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2018/05/memento-pattern.html">Memento
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 7;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.in/2016/10/proxy-pattern.html">Proxy
Pattern</a><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/06/observer-pattern.html">Observer
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 8;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="http://miteshsureja.blogspot.com/2018/07/state-pattern.html">State
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 9;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/08/strategy-pattern.html">Strategy
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 10;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/template-method-pattern.html">Template
Method Pattern</a><o:p></o:p></span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 11; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 154.25pt;" valign="top" width="206">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;" valign="top" width="170">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.8pt;" valign="top" width="198">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 6.0pt;">
<span lang="EN-IN"><a href="https://miteshsureja.blogspot.com/2018/12/visitor-pattern.html">Visitor
Pattern</a><o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-56137644704011645912017-02-26T09:11:00.000+05:302017-03-08T19:29:38.353+05:30How to cancel a Parallel.For and Parallel.Foreach loop – Task Parallel Library<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
In my previous article, I explained about Parallel.For and
Parallel.Foreach loop in detail. In this article I will explain, how to cancel running
or ongoing Parallel.For and Parallel.Foreach loop on certain event.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Parallel.For and Parallel.Foreach has overload methods to pass ParallelOptions to accept cancel request from user. We need to set CancelationToken
to ParallelOptions and pass it to Parallel.For and Parallel.Foreach methods.
When user request for cancelling parallel loop via cancellation token, then the parallel loop will throw an OperationCanceledException and cancel the parallel loop. See below example for
more information.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><script src="https://gist.github.com/miteshsureja/2a68c6edd7959aeb55c71f01f195b5a7.js"></script><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
<b>Output –</b></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-wuWW3fmQn08/V5CyGjLu6hI/AAAAAAAAAsQ/w-llzP6oPbQaxCNAtRj_iG2xHxB8eY1jQCLcB/s1600/ParallelForCancel.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="267" src="https://1.bp.blogspot.com/-wuWW3fmQn08/V5CyGjLu6hI/AAAAAAAAAsQ/w-llzP6oPbQaxCNAtRj_iG2xHxB8eY1jQCLcB/s400/ParallelForCancel.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in above example, when user request for
cancel the parallel loop, cancellation token of parallel options will throw an OperationCanceledException
and cancel the running parallel loop.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>References – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/library/dd537608(v=vs.110).aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See also – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="http://miteshsureja.blogspot.in/2016/11/parallelfor-and-parallelforeach-task.html">Parallel.For and Parallel.Foreach – TPL</a><br />
<a href="http://miteshsureja.blogspot.in/2016/12/how-to-handle-exception-in-parallelfor.html">How to handle exception in Parallel.For and Parallel.Foreach loop? – TPL</a></div>
<div class="MsoNormal">
<br /></div>
<br /></div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-25510612204019868712017-01-14T16:20:00.000+05:302017-03-08T19:39:28.576+05:30How to find and remove duplicate files in directory using C# code?<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN">In today’s
world It is common to have multiple similar images/videos in your computer and it
takes lots of your disk space without your knowledge. I am inspired to write
this article because I am getting lots of similar images, videos and documents in
WhatsApp daily and it is very difficult for me to find duplicate files from directory.
In this article, I’ll share C# code to find duplicate files from directory and
also remove them.<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
Let’s look
on below folder which contains multiple duplicate images. I want to find all
duplicate images from this folder and delete them.</div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-Xe2cLF45s-c/WHm48UlTCgI/AAAAAAAAAuA/WbC-i73dmFw8R4boHFdSS2Hll0mIfj__QCLcB/s1600/All%2BFiles%2B-%2Bwith%2Bduplicates.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="245" src="https://3.bp.blogspot.com/-Xe2cLF45s-c/WHm48UlTCgI/AAAAAAAAAuA/WbC-i73dmFw8R4boHFdSS2Hll0mIfj__QCLcB/s400/All%2BFiles%2B-%2Bwith%2Bduplicates.png" width="400" /></a></div>
<br />
<div class="MsoNormal">
<span lang="EN-IN"></span></div>
<div class="MsoNormal">
<span lang="EN-IN">See below
code to find and delete duplicate file from this folder.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal">
<span lang="EN-IN">
</span></div>
<div class="MsoNormal">
<span lang="EN-IN"><script src="https://gist.github.com/miteshsureja/904c8ec33b4ca62383241ad06012558d.js"></script><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN">Output -</span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-b-SMRG-ypvc/WHm49J16mpI/AAAAAAAAAuE/rOpzO6zzL7YXcGdPXQVLWFZM1Mrxspu0ACEw/s1600/Output1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="https://2.bp.blogspot.com/-b-SMRG-ypvc/WHm49J16mpI/AAAAAAAAAuE/rOpzO6zzL7YXcGdPXQVLWFZM1Mrxspu0ACEw/s400/Output1.png" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-qoH392TrhMY/WHm49Bo7q9I/AAAAAAAAAuI/i_OaXfrAKfQXxXbsXSPNfPmpq3oiVYXRQCEw/s1600/Output2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="https://2.bp.blogspot.com/-qoH392TrhMY/WHm49Bo7q9I/AAAAAAAAAuI/i_OaXfrAKfQXxXbsXSPNfPmpq3oiVYXRQCEw/s400/Output2.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span lang="EN-IN"></span></div>
<div class="MsoNormal">
<span lang="EN-IN">As you can
see duplicate files are deleted from your directory.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/--M7kaHqYLoU/WHm49RJUe1I/AAAAAAAAAuM/ieMN4w03eBM43T5I-zOnsTTdHah41NlIwCEw/s1600/All%2BFiles%2B-%2BUnique.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="246" src="https://3.bp.blogspot.com/--M7kaHqYLoU/WHm49RJUe1I/AAAAAAAAAuM/ieMN4w03eBM43T5I-zOnsTTdHah41NlIwCEw/s400/All%2BFiles%2B-%2BUnique.png" width="400" /></a></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN">This
programme can be used to delete any duplicate files like images, videos,
documents etc.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN">I hope this
code snippet helps you to find and remove duplicate files in your directory and
make your life easy. Please leave your feedback in comments below.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-IN"><b>See also – </b><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-IN">
</span></div>
<div class="MsoNormal">
<a href="http://miteshsureja.blogspot.in/2012/02/working-with-directory-class-in-net-c.html">Working with Directory class in C#</a></div>
<div class="MsoNormal">
<a href="http://miteshsureja.blogspot.in/2012/01/filestream-in-net-c.html">FileStream in .Net (C#)</a></div>
<div class="MsoNormal">
<a href="http://miteshsureja.blogspot.in/2012/01/performance-counters-in-net.html">Working with Performance Counters in C# .Net</a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0tag:blogger.com,1999:blog-3223444668744241586.post-67965036315191440892016-12-26T18:15:00.001+05:302017-03-08T19:52:28.034+05:30How to handle exception in Parallel.For and Parallel.Foreach loop? – Task Parallel Library<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
In my previous article, I explained about <a href="http://miteshsureja.blogspot.in/2016/11/parallelfor-and-parallelforeach-task.html">Parallel.For andParallel.Foreach loop</a> in detail. In this article I will explain, how to handle
exception in Parallel.For and Parallel.Foreach loop.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Parallel.For and Parallel.Foreach methods doesn’t provide
any special mechanism to handle exception thrown from parallel loops. We can catch unhandled exceptions using try catch block inside parallel loop. When you add any try catch block to catch exception in parallel loop,
there is possibility that the same exception may throw by multiple threads in
parallel. So we need to wrap try catch block inside parallel loop and also on
method from where we call parallel loop to catch all types of exceptions. See
below example.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Code – </b><script src="https://gist.github.com/miteshsureja/f58cc20cf59a57c225c4615d427c6822.js"></script><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
<b>Output – </b><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-mzrOh4Y6lJ4/V5GoSsPeHAI/AAAAAAAAAso/7nlyIt0zrGckVXk5w6ewc0pVSK8JHigDwCLcB/s1600/ParallelForException.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="267" src="https://3.bp.blogspot.com/-mzrOh4Y6lJ4/V5GoSsPeHAI/AAAAAAAAAso/7nlyIt0zrGckVXk5w6ewc0pVSK8JHigDwCLcB/s400/ParallelForException.png" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As you can see in above example, all exception thrown from Parallel.For
loop is captured as ArgumentException and added to ConcurrentQueue from
multiple threads. If parallel loop has any exception occurred, then we need to iterate all
the exception to get exception message.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>References – <o:p></o:p></b></div>
<div class="MsoNormal">
<a href="https://msdn.microsoft.com/en-us/library/dd537608(v=vs.110).aspx">MSDN</a><o:p></o:p></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>See also –</b></div>
<div class="MsoNormal">
<a href="http://miteshsureja.blogspot.in/2016/11/parallelfor-and-parallelforeach-task.html">Parallel.For and Parallel.Foreach – TPL</a><br />
<a href="http://miteshsureja.blogspot.com/2011/04/how-to-use-backgroundworker-to-update.html">Background Worker in WPF</a><br />
<a href="http://miteshsureja.blogspot.com/2011/09/how-to-handle-exceptions-while-working.html">How to handle exception while working with Threading?</a></div>
<div class="MsoNormal">
<br /></div>
</div>
Mitesh Surejahttp://www.blogger.com/profile/03174231275940415680noreply@blogger.com0