<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Monir Hossain</title>
    <description>The latest articles on DEV Community by Monir Hossain (@pi3o1416).</description>
    <link>https://dev.to/pi3o1416</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1156669%2F5b90c6a5-6b59-4d73-acfe-ff3919eba119.jpeg</url>
      <title>DEV Community: Monir Hossain</title>
      <link>https://dev.to/pi3o1416</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pi3o1416"/>
    <language>en</language>
    <item>
      <title>SQL Antipatterns: Jaywalking</title>
      <dc:creator>Monir Hossain</dc:creator>
      <pubDate>Sun, 19 Nov 2023 12:19:42 +0000</pubDate>
      <link>https://dev.to/pi3o1416/sql-antipatterns-jaywalking-4ijd</link>
      <guid>https://dev.to/pi3o1416/sql-antipatterns-jaywalking-4ijd</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Antipatterns are techniques that are implemented to solve a specific problem but rather they create new problems.&lt;/p&gt;

&lt;p&gt;Antipatterns are commonly used to solve common development problems. At first glance, they might seem like appropriate and clever solutions but instead, they create more unwanted coincidences than good ones. The term "antipattern" was first coined by &lt;a href="https://en.wikipedia.org/wiki/Andrew_Koenig_(programmer)" rel="noopener noreferrer"&gt;Andrew Koenig&lt;/a&gt; in his paper "Patterns and Antipatterns" in 1995. The concept of design patterns inspired him. In his paper, he refers to antipatterns as&lt;/p&gt;

&lt;p&gt;"An antipattern is just like a pattern, except that instead of a solution, it gives something that looks like a solution but isn’t one."&lt;/p&gt;

&lt;p&gt;SQL Antipatterns refer to antipatterns that we use to solve database design problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Jaywalking
&lt;/h2&gt;

&lt;p&gt;Assume You are working on a Ticketing system where each ticket is handled by one employee. when a Ticket is raised by the customer the customer care department assigns the ticket to the responsible Employee to resolve customer issues. The process is fairly simple however often customer raises a ticket where multiple people are needed to solve the issue. Since your system can only assign one employee to a ticket the work of the other employees is not reflected on their monthly KPI. So you are assigned to fix the issue. You always try to avoid joining since they can increase query time so you came up with a clever solution to use a comma-separated list of user identifiers instead of the single identifier it used before.&lt;/p&gt;

&lt;p&gt;However, soon your boss came up to you complaining that they can't assign more than 5 users. If they try to add more. they get an error. After examining the error you understand the list of IDs has to fit in a string with maximum length, If the maximum length exceeds an exception arises.&lt;/p&gt;

&lt;p&gt;This method of using a comma-separated list to avoid using an intersection table for a many-to-many relationship is called Jaywalking, because jaywalking is also an act of avoiding an intersection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Objective
&lt;/h3&gt;

&lt;p&gt;Initially, You have a Ticketing system where each ticket is assigned to one user. Storing the data of one user is fairly simple. You associate each ticket with a user using the UUID column in the Ticket table. So each user may handle many tickets but each ticket will be assigned to one user. As the product matures you understand that often it is necessary to assign tickets to multiple users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp4p81mn793zd1nl9y81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp4p81mn793zd1nl9y81.png" alt="Image description" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Fig.1 - Initial Design&lt;/b&gt;




&lt;h3&gt;
  
  
  Antipattern: Format Comma Separate List
&lt;/h3&gt;

&lt;p&gt;To minimize changes in the database and avoid joining you decide to alter the "assiged_user" field to a comma-separated list to store the list of assiged users. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7351x3nbxe4zuwzxprg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7351x3nbxe4zuwzxprg.png" alt="Image description" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Fig.2 - Antipattern Design&lt;/b&gt;




&lt;p&gt;This might seem like a good solution since you avoiding adding new tables and columns but let's look at some of the performance and integrity issues this design will suffer from.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Querying Ticket for a Specific User:&lt;/strong&gt; It is difficult to query Tickets for a specific user when foreign keys are combined into a single field. Straight Forward equality check will not work. You have to use pattern matching and will not get the benefit of indexing. Pattern matching expression is different for each vendor, so your query will not be vendor-natural.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Querying User For a Given Ticket:&lt;/strong&gt; Likewise It is costly to join User and Ticket Table when User IDs are represented as a comma-separated list. You have to use awkward regular expressions to match rows in different tables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Making Aggregate Query:&lt;/strong&gt; Aggregate functions are designed to work over a list of rows, not on comma-separated list, so It will be tricky to use them in code. moreover, these tricks will make queries less clear, time-consuming to develop, and hard to debug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updating User for a Specific Ticket:&lt;/strong&gt; To assign a ticket you can contact the ID end of the string but the list will not be in sorted order.
To Delete a user from a ticket you need to perform two queries one to fetch the current comman-seperated ID list and another to update the row with a new list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validating User IDs:&lt;/strong&gt; Since IDs do not refer to any field it is possible to add garbage data and lose data integrity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choosing a Separator Character:&lt;/strong&gt; It is also necessery to choose separator cautiously since the separator can not appear in any IDs. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;List Length Limitation:&lt;/strong&gt; How many IDs can be added to the list is not fixed since It is based on ID size. If the maximum length is 30 and each ID consist of 2 characters you will manage to add boos10 User to a ticket but if the size of the ID is not fixed the number above will vary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are some legitimate uses of this pattern. like your system might need data in a comma-separated format and you don't need to access individual items in a list. Likewise, if your application receives data in comma-separated format you just need to store them as it is.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;A better solution would be to create an intersection table "TicketAssignee" of the Ticket and User table that indicates the Many-to-Many relation between User and Ticket. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fekojpy83m6hy3r969d16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fekojpy83m6hy3r969d16.png" alt="Image description" width="800" height="398"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Fig.3 - Intersection Table Design&lt;/b&gt;




&lt;p&gt;Let's see how the intersection table solves all the problems we see in Antipattern.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Querying Tickets by User and the Other Way Around:&lt;/strong&gt; With intersection table query on Tickets and TicketAssigneee table is much straight forward. With indexing this query will perform better than the query on a common-separated string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Making Aggregate Queries:&lt;/strong&gt; Aggregate functions can be used to generate more sophisticated reports.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updating Ticket Assignee for a Specific Ticket:&lt;/strong&gt; Add and Delete Ticket Assignee can be possible by inserting and deleting rows in TicketAssignee Table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validating User IDs:&lt;/strong&gt; Invalid user ID add is not possible since ForeignKey validates the value against valid entries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choosing a Separator Character:&lt;/strong&gt; Choosing a separator character is not necessary since each entry is stored in separate rows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;List Length Limitations&lt;/strong&gt; Since entries are stored in separate rows. The list is only limited by the maximum number of rows that can physically exist in one table.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Reference
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pragprog.com/titles/bksqla/sql-antipatterns/" rel="noopener noreferrer"&gt;SQL Antipatterns book chapter 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Design Patterns: Adapter Design Pattern</title>
      <dc:creator>Monir Hossain</dc:creator>
      <pubDate>Sat, 23 Sep 2023 18:08:51 +0000</pubDate>
      <link>https://dev.to/pi3o1416/design-patterns-adapter-design-pattern-4666</link>
      <guid>https://dev.to/pi3o1416/design-patterns-adapter-design-pattern-4666</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The adapter Design pattern also known as the wrapper design pattern converts the interface of a class into another interface the client expects. Adapters let classes work together that couldn't otherwise because of incompatible interfaces. If the adaptee changes over time the adapter should also encapsulate those changes, This ensures that the client doesn't require modification whenever it needs to interact with a different interface. &lt;/p&gt;

&lt;p&gt;It is easy to understand because we frequently see them in our day-to-day lives. Imagine you are traveling to a foreign country and you need to charge your electronic devices but the electronic outlet in the foreign country uses a different plug type and voltage compared to your home country. In this situation, you need an adapter to bridge the gap between your device's plug and the foreign country's outlet. The adapter allows you to connect your device to the foreign electrical system, ensuring that it receives the correct voltage and fits into the different plug types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Assume you are hired as a developer in an E-commerce site and now you are responsible for integrating multiple payment gateway in your system like PayPal, Stax, etc also if necessary the product owner would like to add some more payment gateway in his system. However, each payment gateway has a different interface for payment processing. but we need a standardized interface for processing payments. It is possible to solve this problem without a standardized interface. but this will make the codebase messy and less future-proof.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;To address this problem we can use the adapter design pattern. we will implement an adapter for each payment gateway. These adapters will bridge the gap between the diverse payment gateway interface and your standard payment processing interface.&lt;/p&gt;

&lt;p&gt;Now to implement the adapter design pattern there are two kinds of adapter based on inheritence and object composition.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;Class Adapter: &lt;/b&gt;The class adapter is based on multiple inheritance. where adapter will inherit both the targeted class and adaptee to make the interface compatible with the client interface. class adapter implementation is only possible in programming languages that support multiple inheritance like C++, Python, etc.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;Object Adapter: &lt;/b&gt;The object adapter makes use of the object composition principle. where the adapter will implement the targeted interface and wrap the adaptee class for compatibility. object adapter can be implemented in all popular programming languages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc09hv5mcofd3bjn8xr10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc09hv5mcofd3bjn8xr10.png" alt="Class Adapter Diagram" width="691" height="281"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Fig.1 - Class Adapter Diagram&lt;/b&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctn05ixzg3tltlhg5540.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctn05ixzg3tltlhg5540.png" alt="Object Adapter Diagram" width="671" height="190"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Fig.2 - Object Adapter Diagram&lt;/b&gt;




&lt;h2&gt;
  
  
  Sample Code
&lt;/h2&gt;

&lt;p&gt;It is time to see the adapter in action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;abc&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StandardPGInterface&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metaclass&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;abc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ABCMeta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nd"&gt;@abc.abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;NotImplemented&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalPG&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initiate_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Implement Payment code
&lt;/span&gt;        &lt;span class="k"&gt;pass&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StaxPG&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Implement Payment code
&lt;/span&gt;        &lt;span class="k"&gt;pass&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalPGAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StandardPGInterface&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paypal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PayPalPG&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paypal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initiate_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StaxPGAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StandardPGInterface&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StaxPG&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;paypal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PayPalPGAdapter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;paypal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;process_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StaxPGAdapter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;stax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;process_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Applicability
&lt;/h2&gt;

&lt;p&gt;Use the adapter pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to use an existing class but its interface does not match the one you need.&lt;/li&gt;
&lt;li&gt;You aim to develop a reusable class that can collaborate with unrelated and unforeseen classes.&lt;/li&gt;
&lt;li&gt;(object adapter only) You need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The trade-off between class and object adapter
&lt;/h2&gt;

&lt;p&gt;&lt;b&gt;Class Adapter:&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class adapter won't work when we want to adapt a class and all its subclasses.&lt;/li&gt;
&lt;li&gt;Class adapters can override the behavior of the adaptee. since the class adapter is a subclass of adaptee.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Object Adapter:&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;lets a single adapter have many adaptees, that is the adaptee and all of its subclasses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pros and Cons
&lt;/h2&gt;

&lt;p&gt;&lt;b&gt;Pros&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain Single Responsibility Principle. You can divide the interface from the main business logic of the program.&lt;/li&gt;
&lt;li&gt;Maintain Open/Closed Principle. You can introduce new types of adapters without breaking the client code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Cons&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Overall code complexity increases because of new interfaces and classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"Head First Design Patterns" by Eric Freeman, Bert Bates, Kathy Sierra, Elisabeth Robson, &lt;a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124" rel="noopener noreferrer"&gt;https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;"Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma Erich, Helm Richard, Johnson Ralph, Vlissides John, &lt;a href="https://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional-ebook/dp/B000SEIBB8" rel="noopener noreferrer"&gt;https://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional-ebook/dp/B000SEIBB8&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Refactoring Guru article on Adapter Design Pattern, &lt;a href="https://refactoring.guru/design-patterns/adapter" rel="noopener noreferrer"&gt;https://refactoring.guru/design-patterns/adapter&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Design Patterns: Introduction</title>
      <dc:creator>Monir Hossain</dc:creator>
      <pubDate>Wed, 20 Sep 2023 08:56:28 +0000</pubDate>
      <link>https://dev.to/pi3o1416/design-patterns-introduction-5c6</link>
      <guid>https://dev.to/pi3o1416/design-patterns-introduction-5c6</guid>
      <description>&lt;h2&gt;
  
  
  What is a Design Pattern
&lt;/h2&gt;

&lt;p&gt;A design pattern is a reusable solution to commonly occurring problems within a given context. It is not the finished design that can be applied directly to source and machine code. Instead, it provides a description and template for how to solve the problem that can be used in many different situations. It is the best practice that programmers can use to solve common problems when designing an application.&lt;/p&gt;

&lt;p&gt;The use of successful designs and architectures is facilitated by design patterns. The use of established principles as design techniques makes them more readily available to developers of new systems. In other words, design patterns speed up the process of getting a design right.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7g80xkvac1cj6peg0qem.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7g80xkvac1cj6peg0qem.jpg" alt="Code Image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Fig.1 - Photo by &lt;a src="https://pixabay.com/users/fancycrave1-1115284/"&gt;fancycrave1&lt;/a&gt;&lt;/b&gt;



&lt;h2&gt;
  
  
  Design Pattern Structure
&lt;/h2&gt;

&lt;p&gt;A design pattern has four essential elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pattern Name&lt;/li&gt;
&lt;li&gt;Problem&lt;/li&gt;
&lt;li&gt;Solution&lt;/li&gt;
&lt;li&gt;Consequence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;&lt;em&gt;pattern name&lt;/em&gt;&lt;/strong&gt; is a handle we use to describe the design problem, its solutions, and its consequences in a word or two. It lets us design at a higher level of abstraction. It makes it easier to think about design and to communicate them and their trade-offs to others.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;em&gt;problem&lt;/em&gt;&lt;/strong&gt; describes when to apply the pattern. It explains the problem and its context. Sometimes the pain will include a list of conditions that must be met before it makes sense to apply the pattern.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;em&gt;solution&lt;/em&gt;&lt;/strong&gt; describes the elements that make up the design, their relationships, responsibilities, and collaborations. The solution doesn't describe a particular concrete design or implementation Instead, It provides an abstract description of a design problem and how a general arrangement of elements (classes and objects in our case) solves it.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;em&gt;consequences&lt;/em&gt;&lt;/strong&gt; are the results and trade-offs of applying the pattern. Though consequences are often unvoiced when we describe design decisions, they are critical for evaluating design alternatives and understanding the cost and benefit of applying the pattern. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Catalog of Design Patterns
&lt;/h2&gt;

&lt;p&gt;I've listed some well-known design patterns along with brief descriptions from the &lt;a href="https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_3?qid=1695193321&amp;amp;refinements=p_28%3ADesign+Patterns&amp;amp;s=books&amp;amp;sr=1-3" rel="noopener noreferrer"&gt;GOF Design Patterns book&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Creational Patterns&lt;/em&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Factory Method&lt;/em&gt;&lt;/strong&gt; creates an interface for object creation but leaves it up to subclasses to determine which class should be instantiated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Abstract Factory&lt;/em&gt;&lt;/strong&gt; offers a way to define a blueprint for producing groups of interconnected objects without requiring specific details about the actual classes of those objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Builder&lt;/em&gt;&lt;/strong&gt; decouples the assembly of a complex object from its structure, allowing the same assembly process to generate various representations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Prototype&lt;/em&gt;&lt;/strong&gt; Specify the kinds of objects to create using a prototypical instance,
and create new objects by copying this prototype.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Singleton&lt;/em&gt;&lt;/strong&gt; guarantees that a class has just a single instance and offers a universal means to access that instance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;&lt;em&gt;Structural Patterns&lt;/em&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Adapter&lt;/em&gt;&lt;/strong&gt; transforms a class's interface into a different interface that matches the expectations of its clients.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Bridge&lt;/em&gt;&lt;/strong&gt; separates an abstraction from how it's implemented so that changes in one don't affect the other, allowing them to evolve independently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Composite&lt;/em&gt;&lt;/strong&gt; combines individual objects into hierarchical tree structures to represent relationships between parts and wholes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Decorator&lt;/em&gt;&lt;/strong&gt; dynamically adds extra responsibilities to an object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Facad&lt;/em&gt;&lt;/strong&gt; offers a single, simplified interface that encompasses a group of interfaces within a subsystem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Flyweight&lt;/em&gt;&lt;/strong&gt; uses sharing to support large numbers of fine-grained objects efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Proxy&lt;/em&gt;&lt;/strong&gt; offers a stand-in or substitute for another object, controlling and regulating access to that object.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;&lt;em&gt;Behavioral Patterns&lt;/em&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Chain of Responsibility&lt;/em&gt;&lt;/strong&gt; prevents tight connections between the sender of a request and its recipient by providing multiple objects with the opportunity to handle the request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Command&lt;/em&gt;&lt;/strong&gt; wraps a request as an object, allowing you to customize clients with various requests, queue or record requests, and enable reversible actions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Iterator&lt;/em&gt;&lt;/strong&gt; offers a method to access the elements of a collective object sequentially while hiding the underlying structure of that object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Memento&lt;/em&gt;&lt;/strong&gt; Without breaching encapsulation, record and externalize an object's internal state to enable the object's restoration to that state at a later time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Observer&lt;/em&gt;&lt;/strong&gt; establishes a relationship of one-to-many between objects, ensuring that when one object changes state, all of its dependents are automatically notified and updated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;State&lt;/em&gt;&lt;/strong&gt; enables an object to modify its actions or behavior in response to changes in its internal state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Strategy&lt;/em&gt;&lt;/strong&gt; defines  Design Patterna group of algorithms, encapsulates each of them separately, and enables them to be switched out interchangeably.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Template Method&lt;/em&gt;&lt;/strong&gt; creates the basic structure of an algorithm within a method, delegating specific steps to subclasses for customization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Visitor&lt;/em&gt;&lt;/strong&gt; represents an operation to be performed on the elements of an object
structure.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29ftwuf0uw9megl6glb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29ftwuf0uw9megl6glb0.png" alt="Relation Between Design Patterns"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Fig.2 Relation between design patterns&lt;/b&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;A comprehensive discussion on the use of design patterns should also include some guidance on what not to do when employing them. Design patterns are not focused on designs like straightforward data structures such as linked lists and hash tables that can be represented in classes and directly reused. Likewise, they are not intricate, application-specific designs that pertain to an entire application or subsystem.&lt;/p&gt;

&lt;p&gt;It's important to apply a design pattern only when you genuinely require the flexibility it offers. The Consequences sections become especially valuable when assessing the advantages and drawbacks of a pattern.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
