<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Vectoring AI</title>
<link>https://vectoringai.com/pages/design-pattern.html</link>
<atom:link href="https://vectoringai.com/pages/design-pattern.xml" rel="self" type="application/rss+xml"/>
<description>Software engineering interview questions covering the most important design patterns — Singleton, Factory, Observer, Strategy, Decorator, Adapter, Builder, Command, and more.</description>
<generator>quarto-1.9.36</generator>
<lastBuildDate>Thu, 21 May 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Design Pattern Interview QA - 1</title>
  <dc:creator>Vectoring AI</dc:creator>
  <link>https://vectoringai.com/posts/design-pattern/Design-Pattern-Interview-QA-1.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>This is <strong>Part 1</strong> of our Design Pattern Interview QA series, covering the <strong>10 most frequently asked design patterns</strong> in software engineering interviews. Each question includes a clear explanation, a UML-style diagram, a practical Python implementation, and guidance on when to use each pattern.</p>
<blockquote class="blockquote">
<p>For Python-specific best practices including design patterns applied in idiomatic Python, see <a href="../../posts/swe-interview/Python-SWE-Interview-QA-3.html">Python SWE Interview QA - 3</a>. For production API patterns, see <a href="../../posts/swe-interview/Python-SWE-Interview-QA-4.html">Python SWE Interview QA - 4</a>.</p>
</blockquote>
<hr>
</section>
<section id="q1-what-is-the-singleton-pattern-and-when-should-you-use-it" class="level2">
<h2 class="anchored" data-anchor-id="q1-what-is-the-singleton-pattern-and-when-should-you-use-it">Q1: What is the Singleton Pattern and when should you use it?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Singleton pattern</strong> ensures a class has only <strong>one instance</strong> and provides a global point of access to it. It solves the problem of controlling access to shared resources like database connections, configuration objects, or logging facilities.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    C1["Client A"] --&gt; S["Singleton Instance&lt;br/&gt;(only one exists)"]
    C2["Client B"] --&gt; S
    C3["Client C"] --&gt; S
    S --&gt; RES["Shared Resource&lt;br/&gt;(DB pool, config, logger)"]

    style S fill:#56cc9d,stroke:#333,color:#fff
    style RES fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="python-implementations" class="level3">
<h3 class="anchored" data-anchor-id="python-implementations">Python Implementations</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Approach 1: Module-level instance (Pythonic — recommended)</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># config.py</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> _AppConfig:</span>
<span id="cb1-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Private class — consumers use the module-level instance."""</span></span>
<span id="cb1-5"></span>
<span id="cb1-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb1-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._settings: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb1-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._loaded <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb1-9"></span>
<span id="cb1-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> load(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, path: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb1-11">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb1-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(path) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb1-13">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._settings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.load(f)</span>
<span id="cb1-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._loaded <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-15"></span>
<span id="cb1-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, key: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, default<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb1-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._settings.get(key, default)</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Module-level singleton — Python imports are cached</span></span>
<span id="cb1-20">config <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _AppConfig()</span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage:</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># from myapp.config import config</span></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># config.load("settings.json")</span></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># db_url = config.get("database_url")</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Approach 2: __new__ override (when you need class-based control)</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DatabasePool:</span>
<span id="cb2-3">    _instance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb2-4"></span>
<span id="cb2-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__new__</span>(cls, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb2-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> cls._instance <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb2-7">            cls._instance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__new__</span>(cls)</span>
<span id="cb2-8">            cls._instance._initialized <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb2-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls._instance</span>
<span id="cb2-10"></span>
<span id="cb2-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, connection_string: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>):</span>
<span id="cb2-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._initialized:</span>
<span id="cb2-13">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb2-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.connection_string <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> connection_string</span>
<span id="cb2-15">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pool <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._initialized <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb2-17"></span>
<span id="cb2-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_connection(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb2-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Return connection from pool</span></span>
<span id="cb2-20">        ...</span>
<span id="cb2-21"></span>
<span id="cb2-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Both variables point to the same object</span></span>
<span id="cb2-23">pool1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> DatabasePool(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"postgres://localhost/db"</span>)</span>
<span id="cb2-24">pool2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> DatabasePool(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignored — already initialized"</span>)</span>
<span id="cb2-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> pool1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> pool2  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># True</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Approach 3: Thread-safe Singleton (for multi-threaded apps)</span></span>
<span id="cb3-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> threading</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ThreadSafeSingleton:</span>
<span id="cb3-5">    _instance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb3-6">    _lock <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> threading.Lock()</span>
<span id="cb3-7"></span>
<span id="cb3-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__new__</span>(cls, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb3-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> cls._instance <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb3-10">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> cls._lock:  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Double-checked locking</span></span>
<span id="cb3-11">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> cls._instance <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb3-12">                    cls._instance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__new__</span>(cls)</span>
<span id="cb3-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls._instance</span></code></pre></div></div>
</section>
<section id="when-to-use-avoid" class="level3">
<h3 class="anchored" data-anchor-id="when-to-use-avoid">When to Use / Avoid</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 46%">
<col style="width: 53%">
</colgroup>
<thead>
<tr class="header">
<th>Use Singleton</th>
<th>Avoid Singleton</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Database connection pools</td>
<td>When objects carry different state</td>
</tr>
<tr class="even">
<td>Application configuration</td>
<td>When testability is critical (hard to mock)</td>
</tr>
<tr class="odd">
<td>Logging services</td>
<td>When you need multiple instances later</td>
</tr>
<tr class="even">
<td>Thread pools</td>
<td>When it introduces hidden global state</td>
</tr>
<tr class="odd">
<td>Hardware interface access</td>
<td>When dependency injection is preferred</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q2-what-is-the-factory-method-pattern-and-how-does-it-differ-from-abstract-factory" class="level2">
<h2 class="anchored" data-anchor-id="q2-what-is-the-factory-method-pattern-and-how-does-it-differ-from-abstract-factory">Q2: What is the Factory Method Pattern and how does it differ from Abstract Factory?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Factory Method</strong> pattern defines an interface for creating objects but lets subclasses decide which class to instantiate. It moves object creation logic to a dedicated place, promoting the Open/Closed Principle.</p>
<p>The <strong>Abstract Factory</strong> goes further — it produces <em>families</em> of related objects without specifying their concrete classes.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    subgraph FactoryMethod["Factory Method"]
        FM_CLIENT["Client"]
        FM_CLIENT --&gt; FM_FACTORY["NotificationFactory"]
        FM_FACTORY --&gt;|"create('email')"| FM_EMAIL["EmailNotification"]
        FM_FACTORY --&gt;|"create('sms')"| FM_SMS["SMSNotification"]
        FM_FACTORY --&gt;|"create('push')"| FM_PUSH["PushNotification"]
    end

    subgraph AbstractFactory["Abstract Factory"]
        AF_CLIENT["Client"]
        AF_CLIENT --&gt; AF_LIGHT["LightThemeFactory"]
        AF_CLIENT --&gt; AF_DARK["DarkThemeFactory"]
        AF_LIGHT --&gt; AF_LB["LightButton"]
        AF_LIGHT --&gt; AF_LI["LightInput"]
        AF_DARK --&gt; AF_DB["DarkButton"]
        AF_DARK --&gt; AF_DI["DarkInput"]
    end

    style FM_FACTORY fill:#56cc9d,stroke:#333,color:#fff
    style AF_LIGHT fill:#ffce67,stroke:#333
    style AF_DARK fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="factory-method-implementation" class="level3">
<h3 class="anchored" data-anchor-id="factory-method-implementation">Factory Method Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Product interface</span></span>
<span id="cb4-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Notification(ABC):</span>
<span id="cb4-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb4-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, recipient: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>: ...</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete products</span></span>
<span id="cb4-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb4-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> EmailNotification(Notification):</span>
<span id="cb4-12">    smtp_host: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"smtp.example.com"</span></span>
<span id="cb4-13"></span>
<span id="cb4-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, recipient: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>:</span>
<span id="cb4-15">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Email to </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>recipient<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>message<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb4-17"></span>
<span id="cb4-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb4-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SMSNotification(Notification):</span>
<span id="cb4-20">    api_key: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb4-21"></span>
<span id="cb4-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, recipient: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>:</span>
<span id="cb4-23">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"SMS to </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>recipient<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>message<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb4-25"></span>
<span id="cb4-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb4-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PushNotification(Notification):</span>
<span id="cb4-28">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, recipient: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>:</span>
<span id="cb4-29">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Push to </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>recipient<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>message<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb4-31"></span>
<span id="cb4-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Factory (Pythonic: use a registry dict, not class hierarchy)</span></span>
<span id="cb4-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> NotificationFactory:</span>
<span id="cb4-34">    _registry: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>[Notification]] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb4-35">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>: EmailNotification,</span>
<span id="cb4-36">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sms"</span>: SMSNotification,</span>
<span id="cb4-37">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"push"</span>: PushNotification,</span>
<span id="cb4-38">    }</span>
<span id="cb4-39"></span>
<span id="cb4-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@classmethod</span></span>
<span id="cb4-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> register(cls, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, notification_cls: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>[Notification]):</span>
<span id="cb4-42">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Open for extension — register new types at runtime."""</span></span>
<span id="cb4-43">        cls._registry[name] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> notification_cls</span>
<span id="cb4-44"></span>
<span id="cb4-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@classmethod</span></span>
<span id="cb4-46">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create(cls, channel: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Notification:</span>
<span id="cb4-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> channel <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cls._registry:</span>
<span id="cb4-48">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Unknown channel: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>channel<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls._registry[channel](<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs)</span>
<span id="cb4-50"></span>
<span id="cb4-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage</span></span>
<span id="cb4-52">notif <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> NotificationFactory.create(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>, smtp_host<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mail.company.com"</span>)</span>
<span id="cb4-53">notif.send(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alice@example.com"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello!"</span>)</span>
<span id="cb4-54"></span>
<span id="cb4-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extend without modifying factory:</span></span>
<span id="cb4-56">NotificationFactory.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slack"</span>, SlackNotification)</span></code></pre></div></div>
</section>
<section id="abstract-factory-implementation" class="level3">
<h3 class="anchored" data-anchor-id="abstract-factory-implementation">Abstract Factory Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Abstract products</span></span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Button(ABC):</span>
<span id="cb5-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb5-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>: ...</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Input(ABC):</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb5-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>: ...</span>
<span id="cb5-11"></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete products — Light theme</span></span>
<span id="cb5-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> LightButton(Button):</span>
<span id="cb5-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb5-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;button class='btn-light'&gt;Click&lt;/button&gt;"</span></span>
<span id="cb5-16"></span>
<span id="cb5-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> LightInput(Input):</span>
<span id="cb5-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb5-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;input class='input-light'/&gt;"</span></span>
<span id="cb5-20"></span>
<span id="cb5-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete products — Dark theme</span></span>
<span id="cb5-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DarkButton(Button):</span>
<span id="cb5-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb5-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;button class='btn-dark'&gt;Click&lt;/button&gt;"</span></span>
<span id="cb5-25"></span>
<span id="cb5-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DarkInput(Input):</span>
<span id="cb5-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb5-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;input class='input-dark'/&gt;"</span></span>
<span id="cb5-29"></span>
<span id="cb5-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Abstract Factory</span></span>
<span id="cb5-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> UIFactory(ABC):</span>
<span id="cb5-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb5-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_button(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Button: ...</span>
<span id="cb5-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb5-35">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_input(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Input: ...</span>
<span id="cb5-36"></span>
<span id="cb5-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> LightThemeFactory(UIFactory):</span>
<span id="cb5-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_button(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Button:</span>
<span id="cb5-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> LightButton()</span>
<span id="cb5-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_input(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Input:</span>
<span id="cb5-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> LightInput()</span>
<span id="cb5-42"></span>
<span id="cb5-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DarkThemeFactory(UIFactory):</span>
<span id="cb5-44">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_button(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Button:</span>
<span id="cb5-45">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> DarkButton()</span>
<span id="cb5-46">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_input(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Input:</span>
<span id="cb5-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> DarkInput()</span>
<span id="cb5-48"></span>
<span id="cb5-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Client code — works with ANY factory</span></span>
<span id="cb5-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> build_form(factory: UIFactory) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb5-51">    button <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> factory.create_button()</span>
<span id="cb5-52">    input_field <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> factory.create_input()</span>
<span id="cb5-53">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>input_field<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>render()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>button<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>render()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb5-54"></span>
<span id="cb5-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Swap themes without changing client code</span></span>
<span id="cb5-56"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(build_form(DarkThemeFactory()))</span></code></pre></div></div>
</section>
<section id="factory-method-vs-abstract-factory" class="level3">
<h3 class="anchored" data-anchor-id="factory-method-vs-abstract-factory">Factory Method vs Abstract Factory</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 19%">
<col style="width: 36%">
<col style="width: 43%">
</colgroup>
<thead>
<tr class="header">
<th>Aspect</th>
<th>Factory Method</th>
<th>Abstract Factory</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Creates</td>
<td>One product type</td>
<td>Family of related products</td>
</tr>
<tr class="even">
<td>Complexity</td>
<td>Simple</td>
<td>More complex</td>
</tr>
<tr class="odd">
<td>Extension</td>
<td>New product → new factory method</td>
<td>New family → new factory class</td>
</tr>
<tr class="even">
<td>Use case</td>
<td>Parse different file formats</td>
<td>Theme systems, cross-platform UI</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q3-what-is-the-observer-pattern-and-how-do-you-implement-it" class="level2">
<h2 class="anchored" data-anchor-id="q3-what-is-the-observer-pattern-and-how-do-you-implement-it">Q3: What is the Observer Pattern and how do you implement it?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Observer pattern</strong> defines a one-to-many dependency so that when one object (the <strong>Subject</strong>) changes state, all its dependents (<strong>Observers</strong>) are notified and updated automatically. It’s the backbone of event-driven systems.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    SUBJECT["Subject (Publisher)"]
    SUBJECT --&gt;|"notify()"| OBS1["Observer A&lt;br/&gt;(Email Service)"]
    SUBJECT --&gt;|"notify()"| OBS2["Observer B&lt;br/&gt;(Analytics)"]
    SUBJECT --&gt;|"notify()"| OBS3["Observer C&lt;br/&gt;(Audit Log)"]

    subgraph Operations["Subject Methods"]
        ATT["subscribe(observer)"]
        DET["unsubscribe(observer)"]
        NOT["notify_all()"]
    end

    style SUBJECT fill:#56cc9d,stroke:#333,color:#fff
    style OBS1 fill:#6cc3d5,stroke:#333,color:#fff
    style OBS2 fill:#ffce67,stroke:#333
    style OBS3 fill:#ff7851,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-with-type-safety" class="level3">
<h3 class="anchored" data-anchor-id="implementation-with-type-safety">Implementation with Type Safety</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Protocol, Any</span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass, field</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observer protocol (structural typing — no inheritance required)</span></span>
<span id="cb6-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> EventHandler(Protocol):</span>
<span id="cb6-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, Any]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Subject (Publisher)</span></span>
<span id="cb6-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> EventBus:</span>
<span id="cb6-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Publish-subscribe event system."""</span></span>
<span id="cb6-11"></span>
<span id="cb6-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._subscribers: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[EventHandler]] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb6-14"></span>
<span id="cb6-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> subscribe(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, handler: EventHandler) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb6-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> event <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._subscribers:</span>
<span id="cb6-17">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._subscribers[event] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb6-18">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._subscribers[event].append(handler)</span>
<span id="cb6-19"></span>
<span id="cb6-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> unsubscribe(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, handler: EventHandler) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb6-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> event <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._subscribers:</span>
<span id="cb6-22">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._subscribers[event].remove(handler)</span>
<span id="cb6-23"></span>
<span id="cb6-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> publish(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>data) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb6-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> handler <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._subscribers.get(event, []):</span>
<span id="cb6-26">            handler.handle(event, data)</span>
<span id="cb6-27"></span>
<span id="cb6-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete observers</span></span>
<span id="cb6-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> EmailNotifier:</span>
<span id="cb6-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, Any]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb6-31">        email <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb6-32">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[Email] Sending notification to </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>email<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> for event: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>event<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb6-33"></span>
<span id="cb6-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> AnalyticsTracker:</span>
<span id="cb6-35">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, Any]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb6-36">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[Analytics] Tracking event: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>event<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, data: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb6-37"></span>
<span id="cb6-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> AuditLogger:</span>
<span id="cb6-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, Any]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb6-40">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[Audit] </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>event<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb6-41"></span>
<span id="cb6-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage</span></span>
<span id="cb6-43">bus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EventBus()</span>
<span id="cb6-44">bus.subscribe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_registered"</span>, EmailNotifier())</span>
<span id="cb6-45">bus.subscribe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_registered"</span>, AnalyticsTracker())</span>
<span id="cb6-46">bus.subscribe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_registered"</span>, AuditLogger())</span>
<span id="cb6-47">bus.subscribe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_placed"</span>, AnalyticsTracker())</span>
<span id="cb6-48"></span>
<span id="cb6-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Publishing an event notifies all relevant observers</span></span>
<span id="cb6-50">bus.publish(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_registered"</span>, email<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alice@example.com"</span>, user_id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb6-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output:</span></span>
<span id="cb6-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [Email] Sending notification to alice@example.com for event: user_registered</span></span>
<span id="cb6-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [Analytics] Tracking event: user_registered, data: {...}</span></span>
<span id="cb6-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [Audit] user_registered: {...}</span></span></code></pre></div></div>
</section>
<section id="async-observer-for-production-apis" class="level3">
<h3 class="anchored" data-anchor-id="async-observer-for-production-apis">Async Observer (for production APIs)</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> asyncio</span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Callable, Awaitable</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> AsyncEventBus:</span>
<span id="cb7-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Async event bus for I/O-bound handlers."""</span></span>
<span id="cb7-6"></span>
<span id="cb7-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb7-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._handlers: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Callable[..., Awaitable]]] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb7-9"></span>
<span id="cb7-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> on(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, handler: Callable[..., Awaitable]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb7-11">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._handlers.setdefault(event, []).append(handler)</span>
<span id="cb7-12"></span>
<span id="cb7-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> emit(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>data) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb7-14">        handlers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._handlers.get(event, [])</span>
<span id="cb7-15">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run all handlers concurrently</span></span>
<span id="cb7-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> asyncio.gather(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(h(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>data) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> handlers))</span>
<span id="cb7-17"></span>
<span id="cb7-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage</span></span>
<span id="cb7-19">bus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AsyncEventBus()</span>
<span id="cb7-20"></span>
<span id="cb7-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send_email(email: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>_):</span>
<span id="cb7-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> asyncio.sleep(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate I/O</span></span>
<span id="cb7-23">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Email sent to </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>email<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-24"></span>
<span id="cb7-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> track_event(user_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>_):</span>
<span id="cb7-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> asyncio.sleep(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb7-27">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Tracked signup for user </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>user_id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-28"></span>
<span id="cb7-29">bus.on(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"signup"</span>, send_email)</span>
<span id="cb7-30">bus.on(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"signup"</span>, track_event)</span>
<span id="cb7-31"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> bus.emit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"signup"</span>, email<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bob@test.com"</span>, user_id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span></code></pre></div></div>
</section>
<section id="real-world-applications" class="level3">
<h3 class="anchored" data-anchor-id="real-world-applications">Real-World Applications</h3>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Application</th>
<th>Subject</th>
<th>Observers</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>GUI frameworks</td>
<td>Button click</td>
<td>Event handlers</td>
</tr>
<tr class="even">
<td>Message queues</td>
<td>Topic/Channel</td>
<td>Subscribers</td>
</tr>
<tr class="odd">
<td>Stock market</td>
<td>Price feed</td>
<td>Trading algorithms</td>
</tr>
<tr class="even">
<td>Django signals</td>
<td>Model save</td>
<td>Signal receivers</td>
</tr>
<tr class="odd">
<td>React state</td>
<td>State store</td>
<td>UI components</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q4-what-is-the-strategy-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q4-what-is-the-strategy-pattern">Q4: What is the Strategy Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Strategy pattern</strong> defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from the clients that use it. In Python, strategies are often just <strong>functions</strong> — no class hierarchy needed.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    CONTEXT["Context&lt;br/&gt;(PaymentProcessor)"]
    CONTEXT --&gt; STRAT["Strategy Interface&lt;br/&gt;(PaymentStrategy)"]
    STRAT --&gt; S1["CreditCardStrategy"]
    STRAT --&gt; S2["PayPalStrategy"]
    STRAT --&gt; S3["CryptoStrategy"]

    NOTE["Client selects strategy&lt;br/&gt;at runtime"]

    style CONTEXT fill:#56cc9d,stroke:#333,color:#fff
    style STRAT fill:#ffce67,stroke:#333
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="class-based-strategy" class="level3">
<h3 class="anchored" data-anchor-id="class-based-strategy">Class-Based Strategy</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb8-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strategy interface</span></span>
<span id="cb8-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CompressionStrategy(ABC):</span>
<span id="cb8-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb8-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>: ...</span>
<span id="cb8-8"></span>
<span id="cb8-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb8-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decompress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>: ...</span>
<span id="cb8-11"></span>
<span id="cb8-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete strategies</span></span>
<span id="cb8-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> GzipStrategy(CompressionStrategy):</span>
<span id="cb8-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb8-15">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> gzip</span>
<span id="cb8-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> gzip.compress(data)</span>
<span id="cb8-17"></span>
<span id="cb8-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decompress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb8-19">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> gzip</span>
<span id="cb8-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> gzip.decompress(data)</span>
<span id="cb8-21"></span>
<span id="cb8-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> LZ4Strategy(CompressionStrategy):</span>
<span id="cb8-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb8-24">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> lz4.frame</span>
<span id="cb8-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lz4.frame.compress(data)</span>
<span id="cb8-26"></span>
<span id="cb8-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decompress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb8-28">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> lz4.frame</span>
<span id="cb8-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lz4.frame.decompress(data)</span>
<span id="cb8-30"></span>
<span id="cb8-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> NoCompression(CompressionStrategy):</span>
<span id="cb8-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb8-33">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data</span>
<span id="cb8-34"></span>
<span id="cb8-35">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decompress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb8-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data</span>
<span id="cb8-37"></span>
<span id="cb8-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Context</span></span>
<span id="cb8-39"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> FileStorage:</span>
<span id="cb8-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, strategy: CompressionStrategy):</span>
<span id="cb8-41">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._strategy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strategy</span>
<span id="cb8-42"></span>
<span id="cb8-43">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> set_strategy(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, strategy: CompressionStrategy) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb8-44">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Switch strategy at runtime."""</span></span>
<span id="cb8-45">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._strategy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strategy</span>
<span id="cb8-46"></span>
<span id="cb8-47">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> save(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, filename: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb8-48">        compressed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._strategy.compress(data)</span>
<span id="cb8-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(filename, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wb"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb8-50">            f.write(compressed)</span>
<span id="cb8-51"></span>
<span id="cb8-52">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> load(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, filename: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb8-53">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(filename, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rb"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb8-54">            compressed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f.read()</span>
<span id="cb8-55">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._strategy.decompress(compressed)</span>
<span id="cb8-56"></span>
<span id="cb8-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage — switch compression at runtime</span></span>
<span id="cb8-58">storage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FileStorage(GzipStrategy())</span>
<span id="cb8-59">storage.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.gz"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"Hello, world!"</span>)</span>
<span id="cb8-60"></span>
<span id="cb8-61">storage.set_strategy(LZ4Strategy())  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Switch strategy</span></span>
<span id="cb8-62">storage.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.lz4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"Hello, world!"</span>)</span></code></pre></div></div>
</section>
<section id="pythonic-strategy-just-use-functions" class="level3">
<h3 class="anchored" data-anchor-id="pythonic-strategy-just-use-functions">Pythonic Strategy (Just Use Functions)</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Callable</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strategies are just functions — no classes needed!</span></span>
<span id="cb9-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sort_by_price(products: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb9-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(products, key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> p: p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>])</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sort_by_rating(products: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb9-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(products, key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> p: p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating"</span>], reverse<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb9-9"></span>
<span id="cb9-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sort_by_name(products: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb9-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(products, key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> p: p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>])</span>
<span id="cb9-12"></span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Context accepts any callable matching the signature</span></span>
<span id="cb9-14">SortStrategy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Callable[[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]]</span>
<span id="cb9-15"></span>
<span id="cb9-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> display_products(products: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>], strategy: SortStrategy) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-17">    sorted_products <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strategy(products)</span>
<span id="cb9-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sorted_products:</span>
<span id="cb9-19">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'name'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'price'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rating'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">★)"</span>)</span>
<span id="cb9-20"></span>
<span id="cb9-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage</span></span>
<span id="cb9-22">products <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb9-23">    {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Widget"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.99</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>},</span>
<span id="cb9-24">    {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gadget"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">49.99</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.8</span>},</span>
<span id="cb9-25">    {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Doohickey"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.99</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.9</span>},</span>
<span id="cb9-26">]</span>
<span id="cb9-27"></span>
<span id="cb9-28">display_products(products, sort_by_price)</span>
<span id="cb9-29">display_products(products, sort_by_rating)</span>
<span id="cb9-30"></span>
<span id="cb9-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lambda as strategy</span></span>
<span id="cb9-32">display_products(products, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> ps: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(ps, key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> p: <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>]))</span></code></pre></div></div>
</section>
<section id="strategy-vs-other-patterns" class="level3">
<h3 class="anchored" data-anchor-id="strategy-vs-other-patterns">Strategy vs Other Patterns</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 37%">
<col style="width: 62%">
</colgroup>
<thead>
<tr class="header">
<th>Pattern</th>
<th>Key Difference</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Strategy</strong></td>
<td>Choose algorithm at runtime (HAS-A)</td>
</tr>
<tr class="even">
<td><strong>Template Method</strong></td>
<td>Define algorithm skeleton, override steps (IS-A)</td>
</tr>
<tr class="odd">
<td><strong>State</strong></td>
<td>Similar structure but transitions between strategies automatically</td>
</tr>
<tr class="even">
<td><strong>Command</strong></td>
<td>Encapsulates a request, not just an algorithm</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q5-what-is-the-decorator-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q5-what-is-the-decorator-pattern">Q5: What is the Decorator Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Decorator pattern</strong> attaches additional responsibilities to an object dynamically. It provides a flexible alternative to subclassing for extending functionality by wrapping objects with new behavior layers.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph LR
    BASE["Base Component&lt;br/&gt;(PlainCoffee)"]
    BASE --&gt; D1["Decorator 1&lt;br/&gt;(MilkDecorator)"]
    D1 --&gt; D2["Decorator 2&lt;br/&gt;(SugarDecorator)"]
    D2 --&gt; D3["Decorator 3&lt;br/&gt;(WhipCreamDecorator)"]
    D3 --&gt; RESULT["Final Object&lt;br/&gt;has all behaviors"]

    style BASE fill:#6cc3d5,stroke:#333,color:#fff
    style RESULT fill:#56cc9d,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="class-based-decorator-gof-pattern" class="level3">
<h3 class="anchored" data-anchor-id="class-based-decorator-gof-pattern">Class-Based Decorator (GoF Pattern)</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Component interface</span></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DataSource(ABC):</span>
<span id="cb10-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb10-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb10-7"></span>
<span id="cb10-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb10-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> read(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>: ...</span>
<span id="cb10-10"></span>
<span id="cb10-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete component</span></span>
<span id="cb10-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> FileDataSource(DataSource):</span>
<span id="cb10-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, filename: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb10-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._filename <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> filename</span>
<span id="cb10-15"></span>
<span id="cb10-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb10-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._filename, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"w"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb10-18">            f.write(data)</span>
<span id="cb10-19"></span>
<span id="cb10-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> read(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._filename, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb10-22">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> f.read()</span>
<span id="cb10-23"></span>
<span id="cb10-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Base decorator (wraps a DataSource)</span></span>
<span id="cb10-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DataSourceDecorator(DataSource):</span>
<span id="cb10-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, source: DataSource):</span>
<span id="cb10-27">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wrapped <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> source</span>
<span id="cb10-28"></span>
<span id="cb10-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb10-30">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wrapped.write(data)</span>
<span id="cb10-31"></span>
<span id="cb10-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> read(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-33">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wrapped.read()</span>
<span id="cb10-34"></span>
<span id="cb10-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete decorators — each adds one behavior</span></span>
<span id="cb10-36"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> EncryptionDecorator(DataSourceDecorator):</span>
<span id="cb10-37">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb10-38">        encrypted <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._encrypt(data)</span>
<span id="cb10-39">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().write(encrypted)</span>
<span id="cb10-40"></span>
<span id="cb10-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> read(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-42">        data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().read()</span>
<span id="cb10-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._decrypt(data)</span>
<span id="cb10-44"></span>
<span id="cb10-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _encrypt(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-46">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simple Caesar cipher for illustration</span></span>
<span id="cb10-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">chr</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">ord</span>(c) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> data)</span>
<span id="cb10-48"></span>
<span id="cb10-49">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _decrypt(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-50">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">chr</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">ord</span>(c) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> data)</span>
<span id="cb10-51"></span>
<span id="cb10-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CompressionDecorator(DataSourceDecorator):</span>
<span id="cb10-53">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb10-54">        compressed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._compress(data)</span>
<span id="cb10-55">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().write(compressed)</span>
<span id="cb10-56"></span>
<span id="cb10-57">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> read(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-58">        data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().read()</span>
<span id="cb10-59">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._decompress(data)</span>
<span id="cb10-60"></span>
<span id="cb10-61">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _compress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-62">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> zlib, base64</span>
<span id="cb10-63">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> base64.b64encode(zlib.compress(data.encode())).decode()</span>
<span id="cb10-64"></span>
<span id="cb10-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _decompress(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-66">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> zlib, base64</span>
<span id="cb10-67">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zlib.decompress(base64.b64decode(data.encode())).decode()</span>
<span id="cb10-68"></span>
<span id="cb10-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stack decorators: File → Compress → Encrypt</span></span>
<span id="cb10-70">source <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EncryptionDecorator(</span>
<span id="cb10-71">    CompressionDecorator(</span>
<span id="cb10-72">        FileDataSource(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"secret.dat"</span>)</span>
<span id="cb10-73">    )</span>
<span id="cb10-74">)</span>
<span id="cb10-75">source.write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensitive data here"</span>)</span>
<span id="cb10-76"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(source.read())  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Sensitive data here" — decrypted &amp; decompressed</span></span></code></pre></div></div>
</section>
<section id="pythons-native-decorator-function-wrapper" class="level3">
<h3 class="anchored" data-anchor-id="pythons-native-decorator-function-wrapper">Python’s Native Decorator (Function Wrapper)</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functools</span>
<span id="cb11-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb11-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> logging</span>
<span id="cb11-4"></span>
<span id="cb11-5">logger <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> logging.getLogger(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span>)</span>
<span id="cb11-6"></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Python's @ syntax IS the decorator pattern for functions!</span></span>
<span id="cb11-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> retry(max_attempts: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, delay: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>):</span>
<span id="cb11-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Decorator that retries a function on failure."""</span></span>
<span id="cb11-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decorator(func):</span>
<span id="cb11-11">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@functools.wraps</span>(func)</span>
<span id="cb11-12">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> wrapper(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb11-13">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> attempt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max_attempts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb11-14">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb11-15">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> func(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs)</span>
<span id="cb11-16">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb11-17">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> attempt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> max_attempts:</span>
<span id="cb11-18">                        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span></span>
<span id="cb11-19">                    logger.warning(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Attempt </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>attempt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> failed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">. Retrying..."</span>)</span>
<span id="cb11-20">                    time.sleep(delay)</span>
<span id="cb11-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> wrapper</span>
<span id="cb11-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> decorator</span>
<span id="cb11-23"></span>
<span id="cb11-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cache(ttl_seconds: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>):</span>
<span id="cb11-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Decorator that caches results with a TTL."""</span></span>
<span id="cb11-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decorator(func):</span>
<span id="cb11-27">        _cache: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb11-28"></span>
<span id="cb11-29">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@functools.wraps</span>(func)</span>
<span id="cb11-30">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> wrapper(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb11-31">            key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (args, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(kwargs.items())))</span>
<span id="cb11-32">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> key <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> _cache:</span>
<span id="cb11-33">                value, timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _cache[key]</span>
<span id="cb11-34">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> time.time() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> ttl_seconds:</span>
<span id="cb11-35">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> value</span>
<span id="cb11-36">            result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> func(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs)</span>
<span id="cb11-37">            _cache[key] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (result, time.time())</span>
<span id="cb11-38">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result</span>
<span id="cb11-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> wrapper</span>
<span id="cb11-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> decorator</span>
<span id="cb11-41"></span>
<span id="cb11-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stack multiple decorators (like wrapping layers)</span></span>
<span id="cb11-43"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@retry</span>(max_attempts<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb11-44"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@cache</span>(ttl_seconds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb11-45"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fetch_user(user_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb11-46">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""First call fetches, subsequent calls use cache, failures retry."""</span></span>
<span id="cb11-47">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> api_client.get(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"/users/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>user_id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</section>
<section id="gof-decorator-vs-python-decorator" class="level3">
<h3 class="anchored" data-anchor-id="gof-decorator-vs-python-decorator">GoF Decorator vs Python <code>@decorator</code></h3>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>GoF Decorator (Object Wrapping)</th>
<th>Python <code>@decorator</code> (Function Wrapping)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Wraps objects, adds object behavior</td>
<td>Wraps functions, adds function behavior</td>
</tr>
<tr class="even">
<td>Uses inheritance + composition</td>
<td>Uses closures + <code>functools.wraps</code></td>
</tr>
<tr class="odd">
<td>Multiple layers via nesting</td>
<td>Multiple layers via stacking <code>@</code></td>
</tr>
<tr class="even">
<td>Runtime flexibility (swap wrappers)</td>
<td>Applied at definition time</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q6-what-is-the-adapter-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q6-what-is-the-adapter-pattern">Q6: What is the Adapter Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Adapter pattern</strong> allows incompatible interfaces to work together. It acts as a bridge between two objects by wrapping one interface and translating calls to match what the client expects.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph LR
    CLIENT["Client Code&lt;br/&gt;(expects PaymentGateway)"]
    CLIENT --&gt; ADAPTER["StripeAdapter&lt;br/&gt;(implements PaymentGateway)"]
    ADAPTER --&gt; ADAPTEE["Stripe SDK&lt;br/&gt;(incompatible interface)"]

    CLIENT2["Client Code"] --&gt; ADAPTER2["PayPalAdapter"]
    ADAPTER2 --&gt; ADAPTEE2["PayPal SDK"]

    style ADAPTER fill:#56cc9d,stroke:#333,color:#fff
    style ADAPTER2 fill:#56cc9d,stroke:#333,color:#fff
    style ADAPTEE fill:#ff7851,stroke:#333,color:#fff
    style ADAPTEE2 fill:#ff7851,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation" class="level3">
<h3 class="anchored" data-anchor-id="implementation">Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Protocol</span>
<span id="cb12-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass</span>
<span id="cb12-3"></span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Target interface — what your application expects</span></span>
<span id="cb12-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PaymentGateway(Protocol):</span>
<span id="cb12-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> charge(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount_cents: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, currency: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, token: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb12-7">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Returns transaction ID."""</span></span>
<span id="cb12-8">        ...</span>
<span id="cb12-9"></span>
<span id="cb12-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> refund(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, transaction_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>: ...</span>
<span id="cb12-11"></span>
<span id="cb12-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adaptee 1: Stripe (uses dollars, different method names)</span></span>
<span id="cb12-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> StripeAPI:</span>
<span id="cb12-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Third-party Stripe SDK — incompatible interface."""</span></span>
<span id="cb12-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_charge(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, cur: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, source: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb12-16">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Stripe: charging $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>amount<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cur<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ch_stripe_123"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"succeeded"</span>}</span>
<span id="cb12-18"></span>
<span id="cb12-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_refund(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, charge_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb12-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"re_456"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"succeeded"</span>}</span>
<span id="cb12-21"></span>
<span id="cb12-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adaptee 2: PayPal (completely different API)</span></span>
<span id="cb12-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PayPalSDK:</span>
<span id="cb12-24">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Third-party PayPal SDK — incompatible interface."""</span></span>
<span id="cb12-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute_payment(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, payment_data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb12-26">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"PayPal: executing payment"</span>)</span>
<span id="cb12-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"paymentId"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PAY-789"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"approved"</span>}</span>
<span id="cb12-28"></span>
<span id="cb12-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> void_payment(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, payment_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb12-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"voided"</span>}</span>
<span id="cb12-31"></span>
<span id="cb12-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adapters — translate between your interface and third-party SDKs</span></span>
<span id="cb12-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> StripeAdapter:</span>
<span id="cb12-34">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Adapts Stripe SDK to PaymentGateway interface."""</span></span>
<span id="cb12-35"></span>
<span id="cb12-36">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, stripe: StripeAPI):</span>
<span id="cb12-37">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._stripe <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stripe</span>
<span id="cb12-38"></span>
<span id="cb12-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> charge(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount_cents: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, currency: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, token: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb12-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert cents to dollars (Stripe uses dollars)</span></span>
<span id="cb12-41">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._stripe.create_charge(</span>
<span id="cb12-42">            amount<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>amount_cents <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb12-43">            cur<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>currency,</span>
<span id="cb12-44">            source<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>token,</span>
<span id="cb12-45">        )</span>
<span id="cb12-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>]</span>
<span id="cb12-47"></span>
<span id="cb12-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> refund(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, transaction_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>:</span>
<span id="cb12-49">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._stripe.create_refund(transaction_id)</span>
<span id="cb12-50">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"succeeded"</span></span>
<span id="cb12-51"></span>
<span id="cb12-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PayPalAdapter:</span>
<span id="cb12-53">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Adapts PayPal SDK to PaymentGateway interface."""</span></span>
<span id="cb12-54"></span>
<span id="cb12-55">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, paypal: PayPalSDK):</span>
<span id="cb12-56">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._paypal <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> paypal</span>
<span id="cb12-57"></span>
<span id="cb12-58">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> charge(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount_cents: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, currency: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, token: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb12-59">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._paypal.execute_payment({</span>
<span id="cb12-60">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amount"</span>: {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total"</span>: amount_cents <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"currency"</span>: currency},</span>
<span id="cb12-61">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"token"</span>: token,</span>
<span id="cb12-62">        })</span>
<span id="cb12-63">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"paymentId"</span>]</span>
<span id="cb12-64"></span>
<span id="cb12-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> refund(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, transaction_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>:</span>
<span id="cb12-66">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._paypal.void_payment(transaction_id)</span>
<span id="cb12-67">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"voided"</span></span>
<span id="cb12-68"></span>
<span id="cb12-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Client code — works with any adapter uniformly</span></span>
<span id="cb12-70"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> process_order(gateway: PaymentGateway, amount: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, token: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb12-71">    tx_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gateway.charge(amount, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USD"</span>, token)</span>
<span id="cb12-72">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Order processed. Transaction: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tx_id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-73">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> tx_id</span>
<span id="cb12-74"></span>
<span id="cb12-75"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Swap payment providers without changing business logic</span></span>
<span id="cb12-76">stripe_gateway <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StripeAdapter(StripeAPI())</span>
<span id="cb12-77">paypal_gateway <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PayPalAdapter(PayPalSDK())</span>
<span id="cb12-78"></span>
<span id="cb12-79">process_order(stripe_gateway, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4999</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tok_visa"</span>)</span>
<span id="cb12-80">process_order(paypal_gateway, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4999</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tok_paypal"</span>)</span></code></pre></div></div>
</section>
<section id="when-to-use-adapter" class="level3">
<h3 class="anchored" data-anchor-id="when-to-use-adapter">When to Use Adapter</h3>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Scenario</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Integrating third-party libraries</td>
<td>Wrap SDK to match your interface</td>
</tr>
<tr class="even">
<td>Legacy system integration</td>
<td>Adapt old API to new interface</td>
</tr>
<tr class="odd">
<td>Testing with mocks</td>
<td>Adapt test doubles to expected interface</td>
</tr>
<tr class="even">
<td>Multiple vendor support</td>
<td>Uniform interface for Stripe/PayPal/Square</td>
</tr>
<tr class="odd">
<td>Data format conversion</td>
<td>XML ↔︎ JSON adapters</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q7-what-is-the-builder-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q7-what-is-the-builder-pattern">Q7: What is the Builder Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Builder pattern</strong> separates the construction of a complex object from its representation, allowing the same construction process to create different representations. It’s essential when objects have many optional parameters.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    DIRECTOR["Director / Client"]
    DIRECTOR --&gt; BUILDER["Builder"]
    BUILDER --&gt;|"step 1"| S1["Set required fields"]
    BUILDER --&gt;|"step 2"| S2["Set optional fields"]
    BUILDER --&gt;|"step 3"| S3["Configure components"]
    BUILDER --&gt;|"build()"| PRODUCT["Complex Object"]

    style BUILDER fill:#56cc9d,stroke:#333,color:#fff
    style PRODUCT fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="pythonic-builder-method-chaining" class="level3">
<h3 class="anchored" data-anchor-id="pythonic-builder-method-chaining">Pythonic Builder (Method Chaining)</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass, field</span>
<span id="cb13-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Self</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb13-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> HttpRequest:</span>
<span id="cb13-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Complex object with many optional components."""</span></span>
<span id="cb13-7">    method: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb13-8">    url: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb13-9">    headers: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> field(default_factory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>)</span>
<span id="cb13-10">    query_params: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> field(default_factory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>)</span>
<span id="cb13-11">    body: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb13-12">    timeout: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.0</span></span>
<span id="cb13-13">    retries: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb13-14">    auth_token: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb13-15"></span>
<span id="cb13-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> HttpRequestBuilder:</span>
<span id="cb13-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Fluent builder for constructing HttpRequest objects step by step."""</span></span>
<span id="cb13-18"></span>
<span id="cb13-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, method: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, url: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb13-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> method</span>
<span id="cb13-21">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> url</span>
<span id="cb13-22">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._headers: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb13-23">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._query_params: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb13-24">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._body: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb13-25">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._timeout: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.0</span></span>
<span id="cb13-26">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._retries: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb13-27">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._auth_token: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb13-28"></span>
<span id="cb13-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> header(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, key: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, value: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb13-30">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._headers[key] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value</span>
<span id="cb13-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb13-32"></span>
<span id="cb13-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> query(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, key: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, value: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb13-34">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._query_params[key] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value</span>
<span id="cb13-35">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb13-36"></span>
<span id="cb13-37">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> body(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, content: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb13-38">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._body <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> content</span>
<span id="cb13-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb13-40"></span>
<span id="cb13-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> timeout(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, seconds: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb13-42">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._timeout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> seconds</span>
<span id="cb13-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb13-44"></span>
<span id="cb13-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> retries(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, count: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb13-46">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._retries <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> count</span>
<span id="cb13-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb13-48"></span>
<span id="cb13-49">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> auth(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, token: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb13-50">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._auth_token <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> token</span>
<span id="cb13-51">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._headers[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Authorization"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Bearer </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>token<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb13-52">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb13-53"></span>
<span id="cb13-54">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> build(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> HttpRequest:</span>
<span id="cb13-55">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Construct the final immutable object."""</span></span>
<span id="cb13-56">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> HttpRequest(</span>
<span id="cb13-57">            method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._method,</span>
<span id="cb13-58">            url<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._url,</span>
<span id="cb13-59">            headers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._headers,</span>
<span id="cb13-60">            query_params<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._query_params,</span>
<span id="cb13-61">            body<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._body,</span>
<span id="cb13-62">            timeout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._timeout,</span>
<span id="cb13-63">            retries<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._retries,</span>
<span id="cb13-64">            auth_token<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._auth_token,</span>
<span id="cb13-65">        )</span>
<span id="cb13-66"></span>
<span id="cb13-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fluent usage — reads like natural language</span></span>
<span id="cb13-68">request <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb13-69">    HttpRequestBuilder(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POST"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://api.example.com/users"</span>)</span>
<span id="cb13-70">    .header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Content-Type"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span>)</span>
<span id="cb13-71">    .header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X-Request-ID"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abc-123"</span>)</span>
<span id="cb13-72">    .auth(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"my-secret-token"</span>)</span>
<span id="cb13-73">    .body(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{"name": "Alice", "email": "alice@example.com"}'</span>)</span>
<span id="cb13-74">    .timeout(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>)</span>
<span id="cb13-75">    .retries(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb13-76">    .build()</span>
<span id="cb13-77">)</span></code></pre></div></div>
</section>
<section id="python-alternative-dataclass-with-__post_init__" class="level3">
<h3 class="anchored" data-anchor-id="python-alternative-dataclass-with-__post_init__">Python Alternative: dataclass with <code>__post_init__</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass</span>
<span id="cb14-2"></span>
<span id="cb14-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb14-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> QueryConfig:</span>
<span id="cb14-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""For simpler cases, dataclass + defaults = lightweight builder."""</span></span>
<span id="cb14-6">    table: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb14-7">    select: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> field(default_factory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"*"</span>])</span>
<span id="cb14-8">    where: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> field(default_factory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>)</span>
<span id="cb14-9">    order_by: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb14-10">    limit: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb14-11">    offset: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb14-12"></span>
<span id="cb14-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> to_sql(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb14-14">        cols <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>.join(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.select)</span>
<span id="cb14-15">        sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"SELECT </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cols<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> FROM </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>table<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb14-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.where:</span>
<span id="cb14-17">            conditions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" AND "</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>k<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> = '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>v<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k, v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.where.items())</span>
<span id="cb14-18">            sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f" WHERE </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>conditions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb14-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.order_by:</span>
<span id="cb14-20">            sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f" ORDER BY </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>order_by<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb14-21">        sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f" LIMIT </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>limit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> OFFSET </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>offset<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb14-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sql</span>
<span id="cb14-23"></span>
<span id="cb14-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simple construction with keyword args</span></span>
<span id="cb14-25">q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QueryConfig(</span>
<span id="cb14-26">    table<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"users"</span>,</span>
<span id="cb14-27">    select<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>],</span>
<span id="cb14-28">    where<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"role"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admin"</span>},</span>
<span id="cb14-29">    order_by<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"created_at DESC"</span>,</span>
<span id="cb14-30">    limit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb14-31">)</span>
<span id="cb14-32"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(q.to_sql())</span></code></pre></div></div>
</section>
<section id="when-builder-vs-other-approaches" class="level3">
<h3 class="anchored" data-anchor-id="when-builder-vs-other-approaches">When Builder vs Other Approaches</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Approach</th>
<th>Use When</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Builder</strong></td>
<td>Many optional params, validation at build time, immutable result</td>
</tr>
<tr class="even">
<td><strong>dataclass</strong></td>
<td>Moderate params, mutable, simple defaults</td>
</tr>
<tr class="odd">
<td><strong>Factory</strong></td>
<td>Selection among different types, not step-by-step construction</td>
</tr>
<tr class="even">
<td><strong><code>__init__</code> with kwargs</strong></td>
<td>Few optional params, no complex validation</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q8-what-is-the-command-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q8-what-is-the-command-pattern">Q8: What is the Command Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Command pattern</strong> encapsulates a request as an object, letting you parameterize clients with different requests, queue or log requests, and support undoable operations.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    INVOKER["Invoker&lt;br/&gt;(Toolbar / Queue)"]
    INVOKER --&gt; CMD["Command Interface&lt;br/&gt;execute() / undo()"]
    CMD --&gt; C1["CopyCommand"]
    CMD --&gt; C2["PasteCommand"]
    CMD --&gt; C3["DeleteCommand"]

    C1 --&gt; RECEIVER["Receiver&lt;br/&gt;(Text Editor)"]
    C2 --&gt; RECEIVER
    C3 --&gt; RECEIVER

    INVOKER --&gt; HISTORY["Command History&lt;br/&gt;(for undo/redo)"]

    style INVOKER fill:#56cc9d,stroke:#333,color:#fff
    style CMD fill:#ffce67,stroke:#333
    style HISTORY fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-with-undoredo" class="level3">
<h3 class="anchored" data-anchor-id="implementation-with-undoredo">Implementation with Undo/Redo</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb15-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass, field</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Command interface</span></span>
<span id="cb15-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Command(ABC):</span>
<span id="cb15-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb15-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb15-8"></span>
<span id="cb15-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb15-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> undo(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb15-11"></span>
<span id="cb15-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Receiver</span></span>
<span id="cb15-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> TextDocument:</span>
<span id="cb15-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb15-15">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb15-16"></span>
<span id="cb15-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> insert(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, position: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-18">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content[:position] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content[position:]</span>
<span id="cb15-19"></span>
<span id="cb15-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> delete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, position: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, length: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb15-21">        deleted <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content[position:position <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> length]</span>
<span id="cb15-22">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content[:position] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.content[position <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> length:]</span>
<span id="cb15-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> deleted</span>
<span id="cb15-24"></span>
<span id="cb15-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete commands</span></span>
<span id="cb15-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb15-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> InsertCommand(Command):</span>
<span id="cb15-28">    document: TextDocument</span>
<span id="cb15-29">    position: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb15-30">    text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb15-31"></span>
<span id="cb15-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-33">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.document.insert(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.position, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.text)</span>
<span id="cb15-34"></span>
<span id="cb15-35">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> undo(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-36">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.document.delete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.position, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.text))</span>
<span id="cb15-37"></span>
<span id="cb15-38"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb15-39"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DeleteCommand(Command):</span>
<span id="cb15-40">    document: TextDocument</span>
<span id="cb15-41">    position: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb15-42">    length: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb15-43">    _deleted_text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> field(default<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, init<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb15-44"></span>
<span id="cb15-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-46">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._deleted_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.document.delete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.position, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.length)</span>
<span id="cb15-47"></span>
<span id="cb15-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> undo(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-49">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.document.insert(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.position, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._deleted_text)</span>
<span id="cb15-50"></span>
<span id="cb15-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Invoker with history</span></span>
<span id="cb15-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CommandInvoker:</span>
<span id="cb15-53">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb15-54">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._history: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Command] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb15-55">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._redo_stack: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Command] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb15-56"></span>
<span id="cb15-57">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, command: Command) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-58">        command.execute()</span>
<span id="cb15-59">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._history.append(command)</span>
<span id="cb15-60">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._redo_stack.clear()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New command invalidates redo</span></span>
<span id="cb15-61"></span>
<span id="cb15-62">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> undo(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-63">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._history:</span>
<span id="cb15-64">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb15-65">        command <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._history.pop()</span>
<span id="cb15-66">        command.undo()</span>
<span id="cb15-67">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._redo_stack.append(command)</span>
<span id="cb15-68"></span>
<span id="cb15-69">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> redo(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb15-70">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._redo_stack:</span>
<span id="cb15-71">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb15-72">        command <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._redo_stack.pop()</span>
<span id="cb15-73">        command.execute()</span>
<span id="cb15-74">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._history.append(command)</span>
<span id="cb15-75"></span>
<span id="cb15-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage</span></span>
<span id="cb15-77">doc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> TextDocument()</span>
<span id="cb15-78">invoker <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CommandInvoker()</span>
<span id="cb15-79"></span>
<span id="cb15-80">invoker.execute(InsertCommand(doc, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, "</span>))</span>
<span id="cb15-81">invoker.execute(InsertCommand(doc, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"World!"</span>))</span>
<span id="cb15-82"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(doc.content)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Hello, World!"</span></span>
<span id="cb15-83"></span>
<span id="cb15-84">invoker.undo()</span>
<span id="cb15-85"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(doc.content)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Hello, "</span></span>
<span id="cb15-86"></span>
<span id="cb15-87">invoker.redo()</span>
<span id="cb15-88"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(doc.content)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Hello, World!"</span></span>
<span id="cb15-89"></span>
<span id="cb15-90">invoker.execute(DeleteCommand(doc, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))</span>
<span id="cb15-91"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(doc.content)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Hello"</span></span>
<span id="cb15-92"></span>
<span id="cb15-93">invoker.undo()</span>
<span id="cb15-94"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(doc.content)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Hello, World!"</span></span></code></pre></div></div>
</section>
<section id="real-world-applications-1" class="level3">
<h3 class="anchored" data-anchor-id="real-world-applications-1">Real-World Applications</h3>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Application</th>
<th>Command Objects</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Text editors</td>
<td>Insert, Delete, Format commands</td>
</tr>
<tr class="even">
<td>Task queues</td>
<td>Job objects with execute()</td>
</tr>
<tr class="odd">
<td>GUI actions</td>
<td>Menu item / button actions</td>
</tr>
<tr class="even">
<td>Database migrations</td>
<td>Up/down migration commands</td>
</tr>
<tr class="odd">
<td>Game replay</td>
<td>Recorded player actions</td>
</tr>
<tr class="even">
<td>Transaction systems</td>
<td>Operations that can be rolled back</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q9-what-is-the-chain-of-responsibility-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q9-what-is-the-chain-of-responsibility-pattern">Q9: What is the Chain of Responsibility Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Chain of Responsibility</strong> pattern lets you pass requests along a chain of handlers. Each handler decides either to process the request or to pass it to the next handler in the chain.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph LR
    REQ["Request"]
    REQ --&gt; H1["Handler 1&lt;br/&gt;(Authentication)"]
    H1 --&gt;|"pass"| H2["Handler 2&lt;br/&gt;(Rate Limiting)"]
    H2 --&gt;|"pass"| H3["Handler 3&lt;br/&gt;(Validation)"]
    H3 --&gt;|"pass"| H4["Handler 4&lt;br/&gt;(Business Logic)"]
    H4 --&gt; RESP["Response"]

    H1 -.-&gt;|"reject"| ERR1["401 Unauthorized"]
    H2 -.-&gt;|"reject"| ERR2["429 Too Many Requests"]
    H3 -.-&gt;|"reject"| ERR3["400 Bad Request"]

    style REQ fill:#6cc3d5,stroke:#333,color:#fff
    style RESP fill:#56cc9d,stroke:#333,color:#fff
    style ERR1 fill:#ff7851,stroke:#333,color:#fff
    style ERR2 fill:#ff7851,stroke:#333,color:#fff
    style ERR3 fill:#ff7851,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-api-middleware-chain" class="level3">
<h3 class="anchored" data-anchor-id="implementation-api-middleware-chain">Implementation: API Middleware Chain</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb16-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass</span>
<span id="cb16-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Any</span>
<span id="cb16-4"></span>
<span id="cb16-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb16-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Request:</span>
<span id="cb16-7">    path: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb16-8">    method: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb16-9">    headers: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]</span>
<span id="cb16-10">    body: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, Any] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb16-11">    user: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb16-12"></span>
<span id="cb16-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb16-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Response:</span>
<span id="cb16-15">    status_code: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb16-16">    body: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, Any]</span>
<span id="cb16-17"></span>
<span id="cb16-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Abstract handler</span></span>
<span id="cb16-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Middleware(ABC):</span>
<span id="cb16-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb16-21">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._next: Middleware <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb16-22"></span>
<span id="cb16-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> set_next(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, handler: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Middleware"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Middleware"</span>:</span>
<span id="cb16-24">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._next <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> handler</span>
<span id="cb16-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> handler  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enable chaining: a.set_next(b).set_next(c)</span></span>
<span id="cb16-26"></span>
<span id="cb16-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, request: Request) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Response:</span>
<span id="cb16-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._next:</span>
<span id="cb16-29">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._next.handle(request)</span>
<span id="cb16-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Response(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"message"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OK"</span>})</span>
<span id="cb16-31"></span>
<span id="cb16-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete handlers</span></span>
<span id="cb16-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> AuthenticationMiddleware(Middleware):</span>
<span id="cb16-34">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, request: Request) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Response:</span>
<span id="cb16-35">        token <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> request.headers.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Authorization"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb16-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> token.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bearer "</span>):</span>
<span id="cb16-37">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Response(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">401</span>, {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Missing or invalid token"</span>})</span>
<span id="cb16-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract user from token</span></span>
<span id="cb16-39">        request.user <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> token.split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simplified</span></span>
<span id="cb16-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().handle(request)</span>
<span id="cb16-41"></span>
<span id="cb16-42"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> RateLimitMiddleware(Middleware):</span>
<span id="cb16-43">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, max_requests: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>):</span>
<span id="cb16-44">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>()</span>
<span id="cb16-45">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._counts: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb16-46">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> max_requests</span>
<span id="cb16-47"></span>
<span id="cb16-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, request: Request) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Response:</span>
<span id="cb16-49">        user <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> request.user <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anonymous"</span></span>
<span id="cb16-50">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._counts[user] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._counts.get(user, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb16-51">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._counts[user] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._max:</span>
<span id="cb16-52">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Response(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">429</span>, {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rate limit exceeded"</span>})</span>
<span id="cb16-53">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().handle(request)</span>
<span id="cb16-54"></span>
<span id="cb16-55"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ValidationMiddleware(Middleware):</span>
<span id="cb16-56">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, request: Request) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Response:</span>
<span id="cb16-57">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> request.method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POST"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> request.body:</span>
<span id="cb16-58">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Response(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Request body required"</span>})</span>
<span id="cb16-59">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().handle(request)</span>
<span id="cb16-60"></span>
<span id="cb16-61"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> BusinessLogicHandler(Middleware):</span>
<span id="cb16-62">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, request: Request) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Response:</span>
<span id="cb16-63">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final handler — process the request</span></span>
<span id="cb16-64">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Response(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, {</span>
<span id="cb16-65">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"message"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Processed </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>request<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>method<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>request<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>path<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb16-66">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user"</span>: request.user,</span>
<span id="cb16-67">        })</span>
<span id="cb16-68"></span>
<span id="cb16-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build the chain</span></span>
<span id="cb16-70">auth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AuthenticationMiddleware()</span>
<span id="cb16-71">rate_limit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RateLimitMiddleware(max_requests<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb16-72">validation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ValidationMiddleware()</span>
<span id="cb16-73">logic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BusinessLogicHandler()</span>
<span id="cb16-74"></span>
<span id="cb16-75">auth.set_next(rate_limit).set_next(validation).set_next(logic)</span>
<span id="cb16-76"></span>
<span id="cb16-77"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Process requests through the chain</span></span>
<span id="cb16-78">request <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Request(</span>
<span id="cb16-79">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/api/users"</span>,</span>
<span id="cb16-80">    method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POST"</span>,</span>
<span id="cb16-81">    headers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Authorization"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bearer user123"</span>},</span>
<span id="cb16-82">    body<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alice"</span>},</span>
<span id="cb16-83">)</span>
<span id="cb16-84"></span>
<span id="cb16-85">response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> auth.handle(request)</span>
<span id="cb16-86"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>status_code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-87"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 200: {'message': 'Processed POST /api/users', 'user': 'user123'}</span></span>
<span id="cb16-88"></span>
<span id="cb16-89"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Failing request — stopped at authentication</span></span>
<span id="cb16-90">bad_request <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Request(path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/api/users"</span>, method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GET"</span>, headers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{})</span>
<span id="cb16-91">response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> auth.handle(bad_request)</span>
<span id="cb16-92"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>status_code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-93"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 401: {'error': 'Missing or invalid token'}</span></span></code></pre></div></div>
</section>
<section id="chain-of-responsibility-vs-other-patterns" class="level3">
<h3 class="anchored" data-anchor-id="chain-of-responsibility-vs-other-patterns">Chain of Responsibility vs Other Patterns</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 40%">
<col style="width: 59%">
</colgroup>
<thead>
<tr class="header">
<th>Pattern</th>
<th>Relationship</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Chain of Responsibility</strong></td>
<td>Sequential handlers, each may stop the chain</td>
</tr>
<tr class="even">
<td><strong>Decorator</strong></td>
<td>All wrappers execute (add behavior), none skip</td>
</tr>
<tr class="odd">
<td><strong>Command</strong></td>
<td>Single handler executes a specific action</td>
</tr>
<tr class="even">
<td><strong>Middleware</strong> (web)</td>
<td>Practical application of Chain of Responsibility</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q10-what-is-the-proxy-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q10-what-is-the-proxy-pattern">Q10: What is the Proxy Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Proxy pattern</strong> provides a surrogate or placeholder for another object to control access to it. Common types include <strong>virtual proxy</strong> (lazy loading), <strong>protection proxy</strong> (access control), and <strong>caching proxy</strong>.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    CLIENT["Client"]
    CLIENT --&gt; PROXY["Proxy&lt;br/&gt;(same interface as Real Subject)"]
    PROXY --&gt;|"controlled access"| REAL["Real Subject&lt;br/&gt;(expensive resource)"]

    subgraph Types["Proxy Types"]
        VP["Virtual Proxy&lt;br/&gt;Lazy initialization"]
        PP["Protection Proxy&lt;br/&gt;Access control"]
        CP["Caching Proxy&lt;br/&gt;Store results"]
        LP["Logging Proxy&lt;br/&gt;Track access"]
    end

    style PROXY fill:#56cc9d,stroke:#333,color:#fff
    style REAL fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-multiple-proxy-types" class="level3">
<h3 class="anchored" data-anchor-id="implementation-multiple-proxy-types">Implementation: Multiple Proxy Types</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Protocol</span>
<span id="cb17-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Subject interface</span></span>
<span id="cb17-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DatabaseService(Protocol):</span>
<span id="cb17-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> query(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]: ...</span>
<span id="cb17-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>: ...</span>
<span id="cb17-8"></span>
<span id="cb17-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Real subject (expensive to create, sensitive operations)</span></span>
<span id="cb17-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PostgresDatabase:</span>
<span id="cb17-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, connection_string: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb17-12">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[DB] Connecting to database..."</span>)</span>
<span id="cb17-13">        time.sleep(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expensive connection</span></span>
<span id="cb17-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._conn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> connection_string</span>
<span id="cb17-15"></span>
<span id="cb17-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> query(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb17-17">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[DB] Executing query: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sql<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb17-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alice"</span>}]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulated</span></span>
<span id="cb17-19"></span>
<span id="cb17-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb17-21">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[DB] Executing: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sql<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb17-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rows affected</span></span>
<span id="cb17-23"></span>
<span id="cb17-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Virtual Proxy — lazy initialization</span></span>
<span id="cb17-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> LazyDatabaseProxy:</span>
<span id="cb17-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Defers expensive database connection until first use."""</span></span>
<span id="cb17-27"></span>
<span id="cb17-28">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, connection_string: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb17-29">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._connection_string <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> connection_string</span>
<span id="cb17-30">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db: PostgresDatabase <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb17-31"></span>
<span id="cb17-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_db(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> PostgresDatabase:</span>
<span id="cb17-33">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb17-34">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PostgresDatabase(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._connection_string)</span>
<span id="cb17-35">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db</span>
<span id="cb17-36"></span>
<span id="cb17-37">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> query(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb17-38">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_db().query(sql)</span>
<span id="cb17-39"></span>
<span id="cb17-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb17-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_db().execute(sql)</span>
<span id="cb17-42"></span>
<span id="cb17-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Caching Proxy — avoids repeated expensive queries</span></span>
<span id="cb17-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CachingDatabaseProxy:</span>
<span id="cb17-45">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Caches query results to reduce database load."""</span></span>
<span id="cb17-46"></span>
<span id="cb17-47">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, db: DatabaseService, ttl: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">60.0</span>):</span>
<span id="cb17-48">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db</span>
<span id="cb17-49">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._cache: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>]] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb17-50">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._ttl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ttl</span>
<span id="cb17-51"></span>
<span id="cb17-52">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> query(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb17-53">        now <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb17-54">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> sql <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._cache:</span>
<span id="cb17-55">            result, timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._cache[sql]</span>
<span id="cb17-56">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> now <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._ttl:</span>
<span id="cb17-57">                <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[Cache] HIT: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sql<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb17-58">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result</span>
<span id="cb17-59"></span>
<span id="cb17-60">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[Cache] MISS: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sql<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb17-61">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db.query(sql)</span>
<span id="cb17-62">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._cache[sql] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (result, now)</span>
<span id="cb17-63">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result</span>
<span id="cb17-64"></span>
<span id="cb17-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb17-66">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writes invalidate cache</span></span>
<span id="cb17-67">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._cache.clear()</span>
<span id="cb17-68">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db.execute(sql)</span>
<span id="cb17-69"></span>
<span id="cb17-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Protection Proxy — access control</span></span>
<span id="cb17-71"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ProtectedDatabaseProxy:</span>
<span id="cb17-72">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Restricts dangerous operations based on user role."""</span></span>
<span id="cb17-73"></span>
<span id="cb17-74">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, db: DatabaseService, user_role: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb17-75">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db</span>
<span id="cb17-76">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._role <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> user_role</span>
<span id="cb17-77"></span>
<span id="cb17-78">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> query(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb17-79">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All roles can read</span></span>
<span id="cb17-80">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db.query(sql)</span>
<span id="cb17-81"></span>
<span id="cb17-82">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> execute(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, sql: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb17-83">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only admins can write</span></span>
<span id="cb17-84">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._role <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admin"</span>:</span>
<span id="cb17-85">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">PermissionError</span>(</span>
<span id="cb17-86">                <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Role '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>_role<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">' cannot execute write operations"</span></span>
<span id="cb17-87">            )</span>
<span id="cb17-88">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._db.execute(sql)</span>
<span id="cb17-89"></span>
<span id="cb17-90"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compose proxies: Lazy → Caching → Protected</span></span>
<span id="cb17-91">db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ProtectedDatabaseProxy(</span>
<span id="cb17-92">    CachingDatabaseProxy(</span>
<span id="cb17-93">        LazyDatabaseProxy(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"postgres://localhost/mydb"</span>),</span>
<span id="cb17-94">        ttl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb17-95">    ),</span>
<span id="cb17-96">    user_role<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viewer"</span>,</span>
<span id="cb17-97">)</span>
<span id="cb17-98"></span>
<span id="cb17-99"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First query: lazy init + cache miss + actual query</span></span>
<span id="cb17-100">result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db.query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM users"</span>)</span>
<span id="cb17-101"></span>
<span id="cb17-102"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Second query: cache hit (no DB call)</span></span>
<span id="cb17-103">result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db.query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM users"</span>)</span>
<span id="cb17-104"></span>
<span id="cb17-105"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Write attempt: blocked by protection proxy</span></span>
<span id="cb17-106"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb17-107">    db.execute(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DELETE FROM users"</span>)</span>
<span id="cb17-108"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">PermissionError</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb17-109">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Blocked: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</section>
<section id="proxy-types-summary" class="level3">
<h3 class="anchored" data-anchor-id="proxy-types-summary">Proxy Types Summary</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 37%">
<col style="width: 37%">
</colgroup>
<thead>
<tr class="header">
<th>Type</th>
<th>Purpose</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Virtual</strong></td>
<td>Lazy initialization of expensive objects</td>
<td>DB connection on first use</td>
</tr>
<tr class="even">
<td><strong>Protection</strong></td>
<td>Access control / permissions</td>
<td>Role-based operation filtering</td>
</tr>
<tr class="odd">
<td><strong>Caching</strong></td>
<td>Store expensive operation results</td>
<td>Query result cache</td>
</tr>
<tr class="even">
<td><strong>Logging</strong></td>
<td>Record all access to subject</td>
<td>Audit trail for API calls</td>
</tr>
<tr class="odd">
<td><strong>Remote</strong></td>
<td>Represent remote object locally</td>
<td>RPC stub, API client</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="summary-table" class="level2">
<h2 class="anchored" data-anchor-id="summary-table">Summary Table</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 9%">
<col style="width: 28%">
<col style="width: 31%">
<col style="width: 31%">
</colgroup>
<thead>
<tr class="header">
<th>#</th>
<th>Pattern</th>
<th>Category</th>
<th>Key Idea</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td><strong>Singleton</strong></td>
<td>Creational</td>
<td>One instance, global access point</td>
</tr>
<tr class="even">
<td>2</td>
<td><strong>Factory / Abstract Factory</strong></td>
<td>Creational</td>
<td>Delegate object creation; families of objects</td>
</tr>
<tr class="odd">
<td>3</td>
<td><strong>Observer</strong></td>
<td>Behavioral</td>
<td>One-to-many notification on state change</td>
</tr>
<tr class="even">
<td>4</td>
<td><strong>Strategy</strong></td>
<td>Behavioral</td>
<td>Interchangeable algorithms at runtime</td>
</tr>
<tr class="odd">
<td>5</td>
<td><strong>Decorator</strong></td>
<td>Structural</td>
<td>Add behavior dynamically by wrapping</td>
</tr>
<tr class="even">
<td>6</td>
<td><strong>Adapter</strong></td>
<td>Structural</td>
<td>Bridge between incompatible interfaces</td>
</tr>
<tr class="odd">
<td>7</td>
<td><strong>Builder</strong></td>
<td>Creational</td>
<td>Step-by-step complex object construction</td>
</tr>
<tr class="even">
<td>8</td>
<td><strong>Command</strong></td>
<td>Behavioral</td>
<td>Encapsulate requests; support undo/redo</td>
</tr>
<tr class="odd">
<td>9</td>
<td><strong>Chain of Responsibility</strong></td>
<td>Behavioral</td>
<td>Pass request through handler chain</td>
</tr>
<tr class="even">
<td>10</td>
<td><strong>Proxy</strong></td>
<td>Structural</td>
<td>Control access to another object</td>
</tr>
</tbody>
</table>
<hr>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s Next?</h2>
<p>This article covered the foundational design patterns most asked in interviews. For related content:</p>
<ul>
<li><strong>Python design patterns (Pythonic style):</strong> <a href="../../posts/swe-interview/Python-SWE-Interview-QA-3.html">Python SWE Interview QA - 3</a></li>
<li><strong>Production API patterns:</strong> <a href="../../posts/swe-interview/Python-SWE-Interview-QA-4.html">Python SWE Interview QA - 4</a></li>
<li><strong>Python fundamentals:</strong> <a href="../../posts/swe-interview/Python-SWE-Interview-QA-1.html">Python SWE Interview QA - 1</a></li>
<li><strong>Machine learning concepts:</strong> <a href="../../posts/ml-interview/ML-Interview-QA-1.html">ML Interview QA - 1</a></li>
<li><strong>LLM architecture:</strong> <a href="../../posts/llm-interview/LLM-Interview-QA-1.html">LLM Interview QA - 1</a></li>
</ul>


</section>

 ]]></description>
  <guid>https://vectoringai.com/posts/design-pattern/Design-Pattern-Interview-QA-1.html</guid>
  <pubDate>Thu, 21 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://vectoringai.com/images/design-pattern/thumb_design_pattern_interview_qa_300.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Design Pattern Interview QA - 2</title>
  <dc:creator>Vectoring AI</dc:creator>
  <link>https://vectoringai.com/posts/design-pattern/Design-Pattern-Interview-QA-2.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>This is <strong>Part 2</strong> of our Design Pattern Interview QA series, focused on <strong>dependency injection frameworks (Spring)</strong>, <strong>Inversion of Control</strong>, and additional enterprise patterns frequently asked in backend and full-stack interviews.</p>
<blockquote class="blockquote">
<p>For the 10 most commonly asked GoF patterns (Singleton, Factory, Observer, Strategy, Decorator, Adapter, Builder, Command, Chain of Responsibility, Proxy), see <a href="../../posts/design-pattern/Design-Pattern-Interview-QA-1.html">Design Pattern Interview QA - 1</a>.</p>
</blockquote>
<hr>
</section>
<section id="q1-what-is-dependency-injection-and-how-does-the-spring-framework-implement-it" class="level2">
<h2 class="anchored" data-anchor-id="q1-what-is-dependency-injection-and-how-does-the-spring-framework-implement-it">Q1: What is Dependency Injection and how does the Spring Framework implement it?</h2>
<p><strong>Answer:</strong></p>
<p><strong>Dependency Injection (DI)</strong> is a design pattern where an object’s dependencies are provided from the outside rather than created internally. The <strong>Spring Framework</strong> is the most widely used DI container in Java — it manages object creation, wiring, and lifecycle automatically.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    subgraph Without["Without DI (Tight Coupling)"]
        A["OrderService"] --&gt;|"new"| B["MySQLOrderRepo()"]
        A --&gt;|"new"| C["SmtpEmailService()"]
    end

    subgraph With["With DI (Spring Container)"]
        CONTAINER["Spring IoC Container"]
        CONTAINER --&gt;|"injects"| D["OrderService"]
        CONTAINER --&gt;|"creates"| E["OrderRepository"]
        CONTAINER --&gt;|"creates"| F["EmailService"]
        E -.-&gt;|"injected into"| D
        F -.-&gt;|"injected into"| D
    end

    style Without fill:#ff7851,stroke:#333,color:#fff
    style With fill:#56cc9d,stroke:#333,color:#fff
    style CONTAINER fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="spring-di-three-injection-types" class="level3">
<h3 class="anchored" data-anchor-id="spring-di-three-injection-types">Spring DI: Three Injection Types</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 1. CONSTRUCTOR INJECTION (recommended by Spring team)</span></span>
<span id="cb1-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Service</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderService <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-4"></span>
<span id="cb1-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> OrderRepository orderRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> EmailService emailService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Spring auto-injects matching beans</span></span>
<span id="cb1-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Autowired</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Optional in Spring 4.3+ with single constructor</span></span>
<span id="cb1-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OrderService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>OrderRepository orderRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> EmailService emailService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-11">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">orderRepo</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> orderRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emailService</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> emailService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-14"></span>
<span id="cb1-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> Order <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>OrderRequest request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-16">        Order order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> orderRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Order</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb1-17">        emailService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sendConfirmation</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-19">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-20"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 2. SETTER INJECTION (for optional dependencies)</span></span>
<span id="cb1-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Service</span></span>
<span id="cb1-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ReportService <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-25"></span>
<span id="cb1-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> ReportFormatter formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-27"></span>
<span id="cb1-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Autowired</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>required <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Optional dependency</span></span>
<span id="cb1-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setFormatter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ReportFormatter formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-30">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formatter</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-31">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-32"></span>
<span id="cb1-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>formatter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-35">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-36">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Fallback</span></span>
<span id="cb1-38">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-39"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-40"></span>
<span id="cb1-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 3. FIELD INJECTION (discouraged — hard to test)</span></span>
<span id="cb1-42"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Service</span></span>
<span id="cb1-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> UserService <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-44"></span>
<span id="cb1-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Autowired</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Injected directly — no constructor or setter</span></span>
<span id="cb1-46">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> UserRepository userRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-47"></span>
<span id="cb1-48">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Hard to test: can't easily provide a mock</span></span>
<span id="cb1-49"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="spring-bean-scopes-and-lifecycle" class="level3">
<h3 class="anchored" data-anchor-id="spring-bean-scopes-and-lifecycle">Spring Bean Scopes and Lifecycle</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Define beans with different scopes</span></span>
<span id="cb2-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Component</span></span>
<span id="cb2-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Scope</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"singleton"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Default — one instance per container</span></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> AppConfig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Component</span></span>
<span id="cb2-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Scope</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prototype"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// New instance every time it's injected</span></span>
<span id="cb2-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> RequestHandler <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Component</span></span>
<span id="cb2-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Scope</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"request"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// One instance per HTTP request (web apps)</span></span>
<span id="cb2-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> RequestContext <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Bean lifecycle hooks</span></span>
<span id="cb2-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Component</span></span>
<span id="cb2-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DatabasePool <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-17"></span>
<span id="cb2-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@PostConstruct</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Called after dependency injection</span></span>
<span id="cb2-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-20">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">System</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">out</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">println</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Initializing connection pool..."</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb2-21">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-22"></span>
<span id="cb2-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@PreDestroy</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Called before bean is destroyed</span></span>
<span id="cb2-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cleanup</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-25">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">System</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">out</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">println</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Closing connections..."</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb2-26">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-27"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="spring-configuration-approaches" class="level3">
<h3 class="anchored" data-anchor-id="spring-configuration-approaches">Spring Configuration Approaches</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Approach 1: Annotation-based (most common in Spring Boot)</span></span>
<span id="cb3-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Configuration</span></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> AppConfig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-4"></span>
<span id="cb3-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Bean</span></span>
<span id="cb3-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">DataSource</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dataSource</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HikariDataSource</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hikariConfig</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb3-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-9"></span>
<span id="cb3-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Bean</span></span>
<span id="cb3-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> OrderRepository <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">orderRepository</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">DataSource</span> ds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">JdbcOrderRepository</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Spring injects DataSource</span></span>
<span id="cb3-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-15"></span>
<span id="cb3-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Approach 2: Component scanning</span></span>
<span id="cb3-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@SpringBootApplication</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Enables @ComponentScan</span></span>
<span id="cb3-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Application <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">static</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">main</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-20">        SpringApplication<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Application<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-21">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-22"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-23"></span>
<span id="cb3-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Approach 3: Profiles for environment-specific beans</span></span>
<span id="cb3-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Configuration</span></span>
<span id="cb3-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Profile</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"production"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb3-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ProdConfig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Bean</span></span>
<span id="cb3-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> EmailService <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emailService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SmtpEmailService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Real email in production</span></span>
<span id="cb3-31">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-32"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Configuration</span></span>
<span id="cb3-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Profile</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb3-36"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> TestConfig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Bean</span></span>
<span id="cb3-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> EmailService <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emailService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MockEmailService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Fake email in tests</span></span>
<span id="cb3-40">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-41"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="testing-with-spring-di" class="level3">
<h3 class="anchored" data-anchor-id="testing-with-spring-di">Testing with Spring DI</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Unit test — inject mocks manually (no Spring container)</span></span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderServiceTest <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-3"></span>
<span id="cb4-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Test</span></span>
<span id="cb4-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shouldCreateOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-6">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Arrange — construct with mocks</span></span>
<span id="cb4-7">        OrderRepository mockRepo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mock</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>OrderRepository<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-8">        EmailService mockEmail <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mock</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>EmailService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-9">        OrderService service <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OrderService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>mockRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> mockEmail<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-10"></span>
<span id="cb4-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>mockRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">thenReturn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Order</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb4-12"></span>
<span id="cb4-13">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Act</span></span>
<span id="cb4-14">        Order order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> service<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OrderRequest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Widget"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb4-15"></span>
<span id="cb4-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Assert</span></span>
<span id="cb4-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">verify</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>mockRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb4-18">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">verify</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>mockEmail<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sendConfirmation</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-19">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-20"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-21"></span>
<span id="cb4-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Integration test — Spring wires everything</span></span>
<span id="cb4-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@SpringBootTest</span></span>
<span id="cb4-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderServiceIntegrationTest <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-25"></span>
<span id="cb4-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Autowired</span></span>
<span id="cb4-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> OrderService orderService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Spring injects real beans</span></span>
<span id="cb4-28"></span>
<span id="cb4-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@MockBean</span></span>
<span id="cb4-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> EmailService emailService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Replace with mock</span></span>
<span id="cb4-31"></span>
<span id="cb4-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Test</span></span>
<span id="cb4-33">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shouldCreateOrderWithRealDB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-34">        Order order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> orderService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OrderRequest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gadget"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb4-35">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assertNotNull</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getId</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb4-36">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-37"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="why-constructor-injection-is-preferred" class="level3">
<h3 class="anchored" data-anchor-id="why-constructor-injection-is-preferred">Why Constructor Injection Is Preferred</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 55%">
<col style="width: 22%">
<col style="width: 22%">
</colgroup>
<thead>
<tr class="header">
<th>Injection Type</th>
<th>Pros</th>
<th>Cons</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Constructor</strong></td>
<td>Immutable fields; easy to test; fail-fast on missing deps</td>
<td>Verbose with many dependencies</td>
</tr>
<tr class="even">
<td><strong>Setter</strong></td>
<td>Good for optional deps; reconfigurable</td>
<td>Mutable; easy to forget injection</td>
</tr>
<tr class="odd">
<td><strong>Field</strong></td>
<td>Concise</td>
<td>Cannot test without reflection; hides dependencies</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q2-what-is-inversion-of-control-ioc-and-how-does-it-relate-to-di" class="level2">
<h2 class="anchored" data-anchor-id="q2-what-is-inversion-of-control-ioc-and-how-does-it-relate-to-di">Q2: What is Inversion of Control (IoC) and how does it relate to DI?</h2>
<p><strong>Answer:</strong></p>
<p><strong>Inversion of Control (IoC)</strong> is a broad principle where the flow of control is inverted — instead of your code controlling the creation and wiring of objects, a <strong>framework or container</strong> takes over. <strong>Dependency Injection</strong> is one specific implementation of IoC.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    subgraph Traditional["Traditional Control Flow"]
        APP["Your Code"]
        APP --&gt;|"creates"| DEP1["Database"]
        APP --&gt;|"creates"| DEP2["Logger"]
        APP --&gt;|"calls"| FRAMEWORK["Framework"]
    end

    subgraph IoC["Inversion of Control"]
        CONTAINER["IoC Container&lt;br/&gt;(Spring / FastAPI)"]
        CONTAINER --&gt;|"creates &amp; injects"| DEP3["Database"]
        CONTAINER --&gt;|"creates &amp; injects"| DEP4["Logger"]
        CONTAINER --&gt;|"calls"| YOUR_CODE["Your Code"]
    end

    style Traditional fill:#ff7851,stroke:#333,color:#fff
    style IoC fill:#56cc9d,stroke:#333,color:#fff
    style CONTAINER fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="ioc-implementations" class="level3">
<h3 class="anchored" data-anchor-id="ioc-implementations">IoC Implementations</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 36%">
<col style="width: 30%">
</colgroup>
<thead>
<tr class="header">
<th>IoC Type</th>
<th>Mechanism</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Dependency Injection</strong></td>
<td>Container injects dependencies</td>
<td>Spring <code>@Autowired</code>, FastAPI <code>Depends()</code></td>
</tr>
<tr class="even">
<td><strong>Service Locator</strong></td>
<td>Object asks container for deps</td>
<td><code>ServiceLocator.get(UserRepo.class)</code></td>
</tr>
<tr class="odd">
<td><strong>Template Method</strong></td>
<td>Framework calls your overrides</td>
<td><code>HttpServlet.doGet()</code>, Django views</td>
</tr>
<tr class="even">
<td><strong>Event-driven</strong></td>
<td>Framework calls handlers</td>
<td>Spring <code>@EventListener</code>, Observer pattern</td>
</tr>
<tr class="odd">
<td><strong>Strategy via config</strong></td>
<td>Framework picks implementation</td>
<td>Spring profiles, plugin systems</td>
</tr>
</tbody>
</table>
</section>
<section id="spring-ioc-container-architecture" class="level3">
<h3 class="anchored" data-anchor-id="spring-ioc-container-architecture">Spring IoC Container Architecture</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The Spring ApplicationContext IS the IoC container</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// It manages the full lifecycle:</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 1. BEAN DEFINITION: Read configuration (annotations, XML, Java config)</span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 2. INSTANTIATION: Create bean instances</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 3. DEPENDENCY RESOLUTION: Wire dependencies (DI)</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 4. INITIALIZATION: Call @PostConstruct, InitializingBean</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 5. READY: Beans available for use</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 6. DESTRUCTION: Call @PreDestroy on shutdown</span></span>
<span id="cb5-10"></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Two main container types:</span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// BeanFactory — basic DI, lazy initialization</span></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// ApplicationContext — extends BeanFactory with:</span></span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - Event publishing</span></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - Internationalization (i18n)</span></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - Resource loading</span></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - AOP integration</span></span>
<span id="cb5-18"></span>
<span id="cb5-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@SpringBootApplication</span></span>
<span id="cb5-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> MyApp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb5-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">static</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">main</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb5-22">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Creates the IoC container</span></span>
<span id="cb5-23">        ApplicationContext ctx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SpringApplication<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>MyApp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb5-24"></span>
<span id="cb5-25">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Container manages all beans</span></span>
<span id="cb5-26">        OrderService service <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getBean</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>OrderService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb5-27">        service<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OrderRequest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Widget"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb5-28">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb5-29"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="ioc-in-python-fastapi-depends" class="level3">
<h3 class="anchored" data-anchor-id="ioc-in-python-fastapi-depends">IoC in Python (FastAPI Depends)</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> fastapi <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> FastAPI, Depends</span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Annotated</span>
<span id="cb6-3"></span>
<span id="cb6-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FastAPI()</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Dependencies are functions — FastAPI's IoC container calls them</span></span>
<span id="cb6-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_db():</span>
<span id="cb6-8">    db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Database(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"postgres://localhost/mydb"</span>)</span>
<span id="cb6-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb6-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">yield</span> db  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Injected into endpoint</span></span>
<span id="cb6-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">finally</span>:</span>
<span id="cb6-12">        db.close()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cleanup (like @PreDestroy)</span></span>
<span id="cb6-13"></span>
<span id="cb6-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_user_service(db: Database <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Depends(get_db)):</span>
<span id="cb6-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> UserService(db)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Composed dependency</span></span>
<span id="cb6-16"></span>
<span id="cb6-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># FastAPI resolves the entire dependency tree (IoC)</span></span>
<span id="cb6-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/users/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{user_id}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb6-19"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_user(</span>
<span id="cb6-20">    user_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>,</span>
<span id="cb6-21">    service: Annotated[UserService, Depends(get_user_service)],</span>
<span id="cb6-22">):</span>
<span id="cb6-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> service.get_by_id(user_id)</span></code></pre></div></div>
</section>
<section id="di-vs-service-locator" class="level3">
<h3 class="anchored" data-anchor-id="di-vs-service-locator">DI vs Service Locator</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 17%">
<col style="width: 45%">
<col style="width: 36%">
</colgroup>
<thead>
<tr class="header">
<th>Aspect</th>
<th>Dependency Injection</th>
<th>Service Locator</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Coupling</td>
<td>Low — deps are explicit in constructor</td>
<td>Medium — class depends on locator</td>
</tr>
<tr class="even">
<td>Testability</td>
<td>Easy — pass mocks directly</td>
<td>Harder — must configure locator for tests</td>
</tr>
<tr class="odd">
<td>Transparency</td>
<td>Dependencies visible in API</td>
<td>Dependencies hidden inside methods</td>
</tr>
<tr class="even">
<td>Spring approach</td>
<td><code>@Autowired</code> / constructor</td>
<td><code>ApplicationContext.getBean()</code> (discouraged)</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q3-what-is-the-template-method-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q3-what-is-the-template-method-pattern">Q3: What is the Template Method Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Template Method</strong> pattern defines the skeleton of an algorithm in a base class, letting subclasses override specific steps without changing the algorithm’s structure. It’s a key pattern behind many frameworks (Spring, Django, JUnit).</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    ABSTRACT["AbstractClass&lt;br/&gt;(defines template)"]
    ABSTRACT --&gt; STEP1["step1() — fixed"]
    ABSTRACT --&gt; STEP2["step2() — abstract&lt;br/&gt;(subclass overrides)"]
    ABSTRACT --&gt; STEP3["step3() — abstract&lt;br/&gt;(subclass overrides)"]
    ABSTRACT --&gt; STEP4["step4() — fixed"]

    CONCRETE_A["ConcreteClassA&lt;br/&gt;overrides step2, step3"]
    CONCRETE_B["ConcreteClassB&lt;br/&gt;overrides step2, step3"]

    ABSTRACT -.-&gt; CONCRETE_A
    ABSTRACT -.-&gt; CONCRETE_B

    style ABSTRACT fill:#56cc9d,stroke:#333,color:#fff
    style CONCRETE_A fill:#6cc3d5,stroke:#333,color:#fff
    style CONCRETE_B fill:#ffce67,stroke:#333
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="java-implementation" class="level3">
<h3 class="anchored" data-anchor-id="java-implementation">Java Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Template method in an abstract class</span></span>
<span id="cb7-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">abstract</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DataExporter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-3"></span>
<span id="cb7-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TEMPLATE METHOD — defines the algorithm skeleton</span></span>
<span id="cb7-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Marked final so subclasses can't change the overall flow</span></span>
<span id="cb7-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">export</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> query<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fetchData</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>query<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Step 1</span></span>
<span id="cb7-8">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> transformed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Step 2</span></span>
<span id="cb7-9">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> formatted <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>transformed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Step 3</span></span>
<span id="cb7-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>formatted<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>                                     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Step 4</span></span>
<span id="cb7-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cleanup</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>                                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Step 5 (hook)</span></span>
<span id="cb7-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-13"></span>
<span id="cb7-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Concrete step — same for all subclasses</span></span>
<span id="cb7-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fetchData</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> query<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> database<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>query<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-17">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-18"></span>
<span id="cb7-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Abstract steps — subclasses MUST implement</span></span>
<span id="cb7-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">abstract</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span id="cb7-21">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> data</span>
<span id="cb7-22">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">abstract</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">abstract</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-25"></span>
<span id="cb7-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Hook method — optional override (default does nothing)</span></span>
<span id="cb7-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cleanup</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-28"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-29"></span>
<span id="cb7-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Concrete implementation: CSV exporter</span></span>
<span id="cb7-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CsvExporter <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">extends</span> DataExporter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-32"></span>
<span id="cb7-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Override</span></span>
<span id="cb7-34">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Remove sensitive columns</span></span>
<span id="cb7-36">        data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forEach</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">remove</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"password"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb7-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-38">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-39"></span>
<span id="cb7-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Override</span></span>
<span id="cb7-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-42">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">StringBuilder</span> sb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">StringBuilder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb7-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Header</span></span>
<span id="cb7-44">        sb<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">append</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">keySet</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">append</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-45">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Rows</span></span>
<span id="cb7-46">        data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forEach</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span></span>
<span id="cb7-47">            sb<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">append</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-48">                row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stream</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>toString<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toList</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb7-49">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">append</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb7-50">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-51">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sb<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb7-52">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-53"></span>
<span id="cb7-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Override</span></span>
<span id="cb7-55">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-56">        Files<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">of</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"export.csv"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-57">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-58"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-59"></span>
<span id="cb7-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Concrete implementation: JSON exporter</span></span>
<span id="cb7-61"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> JsonExporter <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">extends</span> DataExporter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-62"></span>
<span id="cb7-63">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Override</span></span>
<span id="cb7-64">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-65">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// No transformation needed for JSON</span></span>
<span id="cb7-66">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-67"></span>
<span id="cb7-68">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Override</span></span>
<span id="cb7-69">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-70">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ObjectMapper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeValueAsString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-71">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-72"></span>
<span id="cb7-73">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Override</span></span>
<span id="cb7-74">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">protected</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-75">        Files<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">of</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"export.json"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-76">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-77"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-78"></span>
<span id="cb7-79"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Usage — algorithm structure is fixed, details vary</span></span>
<span id="cb7-80">DataExporter csvExporter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CsvExporter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb7-81">csvExporter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">export</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM users"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-82"></span>
<span id="cb7-83">DataExporter jsonExporter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">JsonExporter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb7-84">jsonExporter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">export</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM users"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span></code></pre></div></div>
</section>
<section id="python-implementation" class="level3">
<h3 class="anchored" data-anchor-id="python-implementation">Python Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DataPipeline(ABC):</span>
<span id="cb8-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Template method: extract → validate → transform → load."""</span></span>
<span id="cb8-5"></span>
<span id="cb8-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> run(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, source: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb8-7">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Template method — defines the pipeline skeleton."""</span></span>
<span id="cb8-8">        raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.extract(source)</span>
<span id="cb8-9">        valid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.validate(raw)</span>
<span id="cb8-10">        transformed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.transform(valid)</span>
<span id="cb8-11">        count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.load(transformed)</span>
<span id="cb8-12">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.on_complete(count)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hook</span></span>
<span id="cb8-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> count</span>
<span id="cb8-14"></span>
<span id="cb8-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb8-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> extract(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, source: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]: ...</span>
<span id="cb8-17"></span>
<span id="cb8-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> validate(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb8-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Default validation — subclasses can override."""</span></span>
<span id="cb8-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [row <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> data <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> row]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove empty rows</span></span>
<span id="cb8-21"></span>
<span id="cb8-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb8-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> transform(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]: ...</span>
<span id="cb8-24"></span>
<span id="cb8-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb8-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> load(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>: ...</span>
<span id="cb8-27"></span>
<span id="cb8-28">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> on_complete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, count: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb8-29">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Hook — optional override."""</span></span>
<span id="cb8-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb8-31"></span>
<span id="cb8-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CsvToPostgresPipeline(DataPipeline):</span>
<span id="cb8-33"></span>
<span id="cb8-34">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> extract(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, source: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb8-35">        <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> csv</span>
<span id="cb8-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(source) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb8-37">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(csv.DictReader(f))</span>
<span id="cb8-38"></span>
<span id="cb8-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> transform(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb8-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> data:</span>
<span id="cb8-41">            row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>].lower().strip()</span>
<span id="cb8-42">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data</span>
<span id="cb8-43"></span>
<span id="cb8-44">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> load(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb8-45">        db.bulk_insert(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"users"</span>, data)</span>
<span id="cb8-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data)</span>
<span id="cb8-47"></span>
<span id="cb8-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> on_complete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, count: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb8-49">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Loaded </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>count<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> records into PostgreSQL"</span>)</span>
<span id="cb8-50"></span>
<span id="cb8-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run the pipeline</span></span>
<span id="cb8-52">pipeline <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CsvToPostgresPipeline()</span>
<span id="cb8-53">pipeline.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"users.csv"</span>)</span></code></pre></div></div>
</section>
<section id="template-method-in-frameworks" class="level3">
<h3 class="anchored" data-anchor-id="template-method-in-frameworks">Template Method in Frameworks</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 27%">
<col style="width: 40%">
<col style="width: 32%">
</colgroup>
<thead>
<tr class="header">
<th>Framework</th>
<th>Template Method</th>
<th>You Override</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Spring MVC</td>
<td><code>DispatcherServlet.doDispatch()</code></td>
<td><code>@Controller</code> methods</td>
</tr>
<tr class="even">
<td>JUnit</td>
<td><code>TestCase.runBare()</code></td>
<td><code>@Test</code>, <code>@BeforeEach</code></td>
</tr>
<tr class="odd">
<td>Django</td>
<td><code>View.dispatch()</code></td>
<td><code>get()</code>, <code>post()</code></td>
</tr>
<tr class="even">
<td>Python <code>unittest</code></td>
<td><code>TestCase.run()</code></td>
<td><code>setUp()</code>, <code>test_*()</code></td>
</tr>
<tr class="odd">
<td>Java Servlets</td>
<td><code>HttpServlet.service()</code></td>
<td><code>doGet()</code>, <code>doPost()</code></td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q4-what-is-the-state-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q4-what-is-the-state-pattern">Q4: What is the State Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>State pattern</strong> allows an object to alter its behavior when its internal state changes. The object appears to change its class. It’s used to replace complex <code>if/else</code> or <code>switch</code> chains for state-dependent behavior.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    CONTEXT["Context&lt;br/&gt;(Order)"]
    CONTEXT --&gt; STATE["Current State"]
    STATE --&gt; S1["PendingState"]
    STATE --&gt; S2["PaidState"]
    STATE --&gt; S3["ShippedState"]
    STATE --&gt; S4["DeliveredState"]
    STATE --&gt; S5["CancelledState"]

    S1 --&gt;|"pay()"| S2
    S2 --&gt;|"ship()"| S3
    S3 --&gt;|"deliver()"| S4
    S1 --&gt;|"cancel()"| S5

    style CONTEXT fill:#56cc9d,stroke:#333,color:#fff
    style STATE fill:#ffce67,stroke:#333
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-order-state-machine" class="level3">
<h3 class="anchored" data-anchor-id="implementation-order-state-machine">Implementation: Order State Machine</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb9-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> TYPE_CHECKING</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> TYPE_CHECKING:</span>
<span id="cb9-5">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> order <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Order</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># State interface</span></span>
<span id="cb9-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderState(ABC):</span>
<span id="cb9-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb9-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb9-11"></span>
<span id="cb9-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb9-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb9-14"></span>
<span id="cb9-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb9-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb9-17"></span>
<span id="cb9-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb9-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb9-20"></span>
<span id="cb9-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete states</span></span>
<span id="cb9-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PendingState(OrderState):</span>
<span id="cb9-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-24">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payment processed. Order is now paid."</span>)</span>
<span id="cb9-25">        order.set_state(PaidState())</span>
<span id="cb9-26"></span>
<span id="cb9-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cannot ship — order not yet paid"</span>)</span>
<span id="cb9-29"></span>
<span id="cb9-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cannot deliver — order not yet shipped"</span>)</span>
<span id="cb9-32"></span>
<span id="cb9-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-34">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order cancelled."</span>)</span>
<span id="cb9-35">        order.set_state(CancelledState())</span>
<span id="cb9-36"></span>
<span id="cb9-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PaidState(OrderState):</span>
<span id="cb9-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order already paid"</span>)</span>
<span id="cb9-40"></span>
<span id="cb9-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-42">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order shipped."</span>)</span>
<span id="cb9-43">        order.set_state(ShippedState())</span>
<span id="cb9-44"></span>
<span id="cb9-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cannot deliver — order not yet shipped"</span>)</span>
<span id="cb9-47"></span>
<span id="cb9-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-49">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Refund issued. Order cancelled."</span>)</span>
<span id="cb9-50">        order.set_state(CancelledState())</span>
<span id="cb9-51"></span>
<span id="cb9-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ShippedState(OrderState):</span>
<span id="cb9-53">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-54">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order already paid"</span>)</span>
<span id="cb9-55"></span>
<span id="cb9-56">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-57">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order already shipped"</span>)</span>
<span id="cb9-58"></span>
<span id="cb9-59">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-60">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order delivered!"</span>)</span>
<span id="cb9-61">        order.set_state(DeliveredState())</span>
<span id="cb9-62"></span>
<span id="cb9-63">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-64">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cannot cancel — order already shipped"</span>)</span>
<span id="cb9-65"></span>
<span id="cb9-66"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DeliveredState(OrderState):</span>
<span id="cb9-67">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order completed"</span>)</span>
<span id="cb9-68">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order completed"</span>)</span>
<span id="cb9-69">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Already delivered"</span>)</span>
<span id="cb9-70">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cannot cancel delivered order"</span>)</span>
<span id="cb9-71"></span>
<span id="cb9-72"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CancelledState(OrderState):</span>
<span id="cb9-73">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order cancelled"</span>)</span>
<span id="cb9-74">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order cancelled"</span>)</span>
<span id="cb9-75">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order cancelled"</span>)</span>
<span id="cb9-76">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> InvalidOperationError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Already cancelled"</span>)</span>
<span id="cb9-77"></span>
<span id="cb9-78"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> InvalidOperationError(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>):</span>
<span id="cb9-79">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb9-80"></span>
<span id="cb9-81"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Context</span></span>
<span id="cb9-82"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Order:</span>
<span id="cb9-83">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, order_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb9-84">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.order_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> order_id</span>
<span id="cb9-85">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._state: OrderState <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PendingState()</span>
<span id="cb9-86"></span>
<span id="cb9-87">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> set_state(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, state: OrderState) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-88">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> state</span>
<span id="cb9-89"></span>
<span id="cb9-90">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@property</span></span>
<span id="cb9-91">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> status(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb9-92">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._state).<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span>.replace(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb9-93"></span>
<span id="cb9-94">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-95">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._state.pay(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb9-96"></span>
<span id="cb9-97">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-98">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._state.ship(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb9-99"></span>
<span id="cb9-100">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-101">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._state.deliver(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb9-102"></span>
<span id="cb9-103">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-104">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._state.cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb9-105"></span>
<span id="cb9-106"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage</span></span>
<span id="cb9-107">order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Order(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ORD-001"</span>)</span>
<span id="cb9-108"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(order.status)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Pending"</span></span>
<span id="cb9-109"></span>
<span id="cb9-110">order.pay()</span>
<span id="cb9-111"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(order.status)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Paid"</span></span>
<span id="cb9-112"></span>
<span id="cb9-113">order.ship()</span>
<span id="cb9-114"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(order.status)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Shipped"</span></span>
<span id="cb9-115"></span>
<span id="cb9-116">order.deliver()</span>
<span id="cb9-117"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(order.status)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Delivered"</span></span>
<span id="cb9-118"></span>
<span id="cb9-119"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Invalid transition raises error</span></span>
<span id="cb9-120"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-121">    order.cancel()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># InvalidOperationError: Cannot cancel delivered order</span></span>
<span id="cb9-122"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> InvalidOperationError <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb9-123">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</section>
<section id="state-vs-strategy" class="level3">
<h3 class="anchored" data-anchor-id="state-vs-strategy">State vs Strategy</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 32%">
<col style="width: 28%">
<col style="width: 40%">
</colgroup>
<thead>
<tr class="header">
<th>Aspect</th>
<th>State</th>
<th>Strategy</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Purpose</td>
<td>Object changes behavior as state changes</td>
<td>Client chooses algorithm</td>
</tr>
<tr class="even">
<td>Transitions</td>
<td>States know about and trigger transitions</td>
<td>Client explicitly sets strategy</td>
</tr>
<tr class="odd">
<td>Awareness</td>
<td>States may know about each other</td>
<td>Strategies are independent</td>
</tr>
<tr class="even">
<td>Example</td>
<td>Order lifecycle, TCP connection</td>
<td>Sorting algorithm, compression</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q5-what-is-the-facade-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q5-what-is-the-facade-pattern">Q5: What is the Facade Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Facade pattern</strong> provides a simplified interface to a complex subsystem. It doesn’t hide the subsystem — it provides a convenient default interface while still allowing direct access when needed.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    CLIENT["Client"]
    CLIENT --&gt; FACADE["Facade&lt;br/&gt;(simple interface)"]
    FACADE --&gt; S1["VideoCodec"]
    FACADE --&gt; S2["AudioCodec"]
    FACADE --&gt; S3["Muxer"]
    FACADE --&gt; S4["FileWriter"]
    FACADE --&gt; S5["MetadataParser"]

    CLIENT2["Advanced Client"]
    CLIENT2 --&gt; S1
    CLIENT2 --&gt; S3

    style FACADE fill:#56cc9d,stroke:#333,color:#fff
    style CLIENT fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation" class="level3">
<h3 class="anchored" data-anchor-id="implementation">Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Complex subsystem classes</span></span>
<span id="cb10-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> VideoCodec:</span>
<span id="cb10-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decode(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb10-4">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Decoding video from </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-5">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"video_data"</span></span>
<span id="cb10-6"></span>
<span id="cb10-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> encode(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb10-8">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Encoding video to </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"encoded_video"</span></span>
<span id="cb10-10"></span>
<span id="cb10-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> AudioCodec:</span>
<span id="cb10-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> extract(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb10-13">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Extracting audio from </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"audio_data"</span></span>
<span id="cb10-15"></span>
<span id="cb10-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> encode(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>, bitrate: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb10-17">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Encoding audio at </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bitrate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">kbps"</span>)</span>
<span id="cb10-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"encoded_audio"</span></span>
<span id="cb10-19"></span>
<span id="cb10-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Muxer:</span>
<span id="cb10-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> mux(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, video: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>, audio: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb10-22">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Multiplexing audio and video streams"</span>)</span>
<span id="cb10-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"muxed_data"</span></span>
<span id="cb10-24"></span>
<span id="cb10-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> FileWriter:</span>
<span id="cb10-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>, output: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb10-27">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Writing to </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-28"></span>
<span id="cb10-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># FACADE — simple interface to the complex subsystem</span></span>
<span id="cb10-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> VideoConverter:</span>
<span id="cb10-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Facade that hides the complexity of video conversion."""</span></span>
<span id="cb10-32"></span>
<span id="cb10-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb10-34">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._video <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> VideoCodec()</span>
<span id="cb10-35">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._audio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AudioCodec()</span>
<span id="cb10-36">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._muxer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Muxer()</span>
<span id="cb10-37">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._writer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FileWriter()</span>
<span id="cb10-38"></span>
<span id="cb10-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> convert(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, input_file: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, output_file: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mp4"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb10-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""One method does everything — clients don't need to know the steps."""</span></span>
<span id="cb10-41">        video_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._video.decode(input_file)</span>
<span id="cb10-42">        audio_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._audio.extract(input_file)</span>
<span id="cb10-43"></span>
<span id="cb10-44">        encoded_video <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._video.encode(video_data, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>)</span>
<span id="cb10-45">        encoded_audio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._audio.encode(audio_data, bitrate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">192</span>)</span>
<span id="cb10-46"></span>
<span id="cb10-47">        muxed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._muxer.mux(encoded_video, encoded_audio)</span>
<span id="cb10-48">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._writer.write(muxed, output_file)</span>
<span id="cb10-49">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Conversion complete: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>output_file<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-50"></span>
<span id="cb10-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Client uses the simple facade</span></span>
<span id="cb10-52">converter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> VideoConverter()</span>
<span id="cb10-53">converter.convert(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input.avi"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"output.mp4"</span>)</span>
<span id="cb10-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># vs manually orchestrating 5 subsystem objects</span></span></code></pre></div></div>
</section>
<section id="java-spring-boot-as-a-facade" class="level3">
<h3 class="anchored" data-anchor-id="java-spring-boot-as-a-facade">Java: Spring Boot as a Facade</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Spring Boot itself is a FACADE over the Spring ecosystem!</span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Instead of manually configuring:</span></span>
<span id="cb11-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - DispatcherServlet</span></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - ViewResolver</span></span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - DataSource + JPA EntityManager</span></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - Transaction manager</span></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   - Embedded server (Tomcat)</span></span>
<span id="cb11-8"></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// You just write:</span></span>
<span id="cb11-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@SpringBootApplication</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Facade annotation — auto-configures everything</span></span>
<span id="cb11-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Application <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">static</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">main</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-13">        SpringApplication<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Application<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb11-14">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-16"></span>
<span id="cb11-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Service facade example:</span></span>
<span id="cb11-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Service</span></span>
<span id="cb11-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CheckoutFacade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-20"></span>
<span id="cb11-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> InventoryService inventory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> PaymentService payment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> ShippingService shipping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> NotificationService notifications<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-25"></span>
<span id="cb11-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CheckoutFacade</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>InventoryService inventory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> PaymentService payment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-27">                          ShippingService shipping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> NotificationService notifications<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-28">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inventory</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inventory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-29">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">payment</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-30">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shipping</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> shipping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-31">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">notifications</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> notifications<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-32">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-33"></span>
<span id="cb11-34">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// One method hides the complexity of checkout</span></span>
<span id="cb11-35">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> OrderResult <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">checkout</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Cart cart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> PaymentInfo paymentInfo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-36">        inventory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reserve</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getItems</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb11-37">        PaymentResult payResult <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">charge</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>paymentInfo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> cart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getTotal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb11-38">        ShipmentTracking tracking <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> shipping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createShipment</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb11-39">        notifications<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sendConfirmation</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getUserEmail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(),</span> tracking<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb11-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OrderResult</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>payResult<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getTransactionId</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(),</span> tracking<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getId</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb11-41">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-42"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="when-to-use-facade" class="level3">
<h3 class="anchored" data-anchor-id="when-to-use-facade">When to Use Facade</h3>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Scenario</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Simplify complex library APIs</td>
<td>Video conversion, PDF generation</td>
</tr>
<tr class="even">
<td>Decouple client from subsystem</td>
<td>Checkout process hiding 5 services</td>
</tr>
<tr class="odd">
<td>Provide default configuration</td>
<td>Spring Boot auto-configuration</td>
</tr>
<tr class="even">
<td>Layer boundaries</td>
<td>Service layer facade over repositories</td>
</tr>
<tr class="odd">
<td>Legacy system wrapping</td>
<td>Clean API over messy legacy code</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q6-what-is-the-flyweight-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q6-what-is-the-flyweight-pattern">Q6: What is the Flyweight Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Flyweight pattern</strong> reduces memory usage by sharing common state across many objects instead of storing it in each instance. It splits object state into <strong>intrinsic</strong> (shared) and <strong>extrinsic</strong> (unique per context) parts.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    FACTORY["Flyweight Factory&lt;br/&gt;(caches shared objects)"]
    FACTORY --&gt; FW1["Flyweight 'A'&lt;br/&gt;(intrinsic: font, size, color)"]
    FACTORY --&gt; FW2["Flyweight 'B'"]
    FACTORY --&gt; FW3["Flyweight 'C'"]

    C1["Context 1&lt;br/&gt;(extrinsic: position x=10, y=20)"]
    C2["Context 2&lt;br/&gt;(extrinsic: position x=50, y=80)"]
    C3["Context 3&lt;br/&gt;(extrinsic: position x=30, y=60)"]

    C1 --&gt; FW1
    C2 --&gt; FW1
    C3 --&gt; FW2

    NOTE["Contexts 1 &amp; 2 share&lt;br/&gt;the same Flyweight"]

    style FACTORY fill:#56cc9d,stroke:#333,color:#fff
    style FW1 fill:#6cc3d5,stroke:#333,color:#fff
    style NOTE fill:#ffce67,stroke:#333
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-text-editor-character-rendering" class="level3">
<h3 class="anchored" data-anchor-id="implementation-text-editor-character-rendering">Implementation: Text Editor Character Rendering</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass</span>
<span id="cb12-2"></span>
<span id="cb12-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Flyweight — stores intrinsic (shared) state</span></span>
<span id="cb12-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span>(frozen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Immutable — safe to share</span></span>
<span id="cb12-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CharacterStyle:</span>
<span id="cb12-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Shared state: font properties that many characters share."""</span></span>
<span id="cb12-7">    font_family: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb12-8">    font_size: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb12-9">    is_bold: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span></span>
<span id="cb12-10">    is_italic: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span></span>
<span id="cb12-11">    color: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb12-12"></span>
<span id="cb12-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Flyweight Factory — caches and reuses styles</span></span>
<span id="cb12-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> StyleFactory:</span>
<span id="cb12-15">    _cache: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>, CharacterStyle] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb12-16"></span>
<span id="cb12-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@classmethod</span></span>
<span id="cb12-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_style(</span>
<span id="cb12-19">        cls,</span>
<span id="cb12-20">        font_family: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arial"</span>,</span>
<span id="cb12-21">        font_size: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,</span>
<span id="cb12-22">        is_bold: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb12-23">        is_italic: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb12-24">        color: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,</span>
<span id="cb12-25">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> CharacterStyle:</span>
<span id="cb12-26">        key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (font_family, font_size, is_bold, is_italic, color)</span>
<span id="cb12-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> key <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cls._cache:</span>
<span id="cb12-28">            cls._cache[key] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CharacterStyle(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>key)</span>
<span id="cb12-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls._cache[key]</span>
<span id="cb12-30"></span>
<span id="cb12-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@classmethod</span></span>
<span id="cb12-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cache_size(cls) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb12-33">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(cls._cache)</span>
<span id="cb12-34"></span>
<span id="cb12-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Context — stores extrinsic (unique) state</span></span>
<span id="cb12-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb12-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Character:</span>
<span id="cb12-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Each character has unique position but shares style."""</span></span>
<span id="cb12-39">    char: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb12-40">    row: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb12-41">    col: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb12-42">    style: CharacterStyle  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reference to shared flyweight</span></span>
<span id="cb12-43"></span>
<span id="cb12-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage: 10,000 characters but only a few style objects</span></span>
<span id="cb12-45">document: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Character] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb12-46"></span>
<span id="cb12-47">normal <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StyleFactory.get_style()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cached</span></span>
<span id="cb12-48">bold <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StyleFactory.get_style(is_bold<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cached</span></span>
<span id="cb12-49">heading <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StyleFactory.get_style(font_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, is_bold<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cached</span></span>
<span id="cb12-50"></span>
<span id="cb12-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All 'normal' characters share the SAME style object</span></span>
<span id="cb12-52"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, char <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is a long document with many characters..."</span>):</span>
<span id="cb12-53">    document.append(Character(char, row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>normal))</span>
<span id="cb12-54"></span>
<span id="cb12-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bold words share the bold style object</span></span>
<span id="cb12-56"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, char <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IMPORTANT"</span>):</span>
<span id="cb12-57">    document.append(Character(char, row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bold))</span>
<span id="cb12-58"></span>
<span id="cb12-59"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Characters: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(document)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 56</span></span>
<span id="cb12-60"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Unique styles: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>StyleFactory<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>cache_size()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 (not 56!)</span></span>
<span id="cb12-61"></span>
<span id="cb12-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Memory saved: 53 fewer CharacterStyle objects</span></span></code></pre></div></div>
</section>
<section id="java-string-pool-as-flyweight" class="level3">
<h3 class="anchored" data-anchor-id="java-string-pool-as-flyweight">Java: String Pool as Flyweight</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Java's String pool IS the Flyweight pattern!</span></span>
<span id="cb13-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// From string pool</span></span>
<span id="cb13-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Same object from pool</span></span>
<span id="cb13-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">System</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">out</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">println</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// true — same reference</span></span>
<span id="cb13-5"></span>
<span id="cb13-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Integer cache is also Flyweight</span></span>
<span id="cb13-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Integer</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Integer</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">valueOf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Cached (-128 to 127)</span></span>
<span id="cb13-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Integer</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Integer</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">valueOf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb13-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">System</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">out</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">println</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// true — same object from cache</span></span>
<span id="cb13-10"></span>
<span id="cb13-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Boolean.TRUE, Boolean.FALSE — only 2 instances ever</span></span></code></pre></div></div>
</section>
<section id="when-to-use-flyweight" class="level3">
<h3 class="anchored" data-anchor-id="when-to-use-flyweight">When to Use Flyweight</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 38%">
<col style="width: 61%">
</colgroup>
<thead>
<tr class="header">
<th>Use When</th>
<th>Don’t Use When</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Millions of similar objects</td>
<td>Few objects in memory</td>
</tr>
<tr class="even">
<td>Objects share most of their state</td>
<td>Each object is unique</td>
</tr>
<tr class="odd">
<td>Memory is a bottleneck</td>
<td>CPU is the bottleneck</td>
</tr>
<tr class="even">
<td>Immutable shared state</td>
<td>Shared state needs modification</td>
</tr>
<tr class="odd">
<td>Game particles, text characters, map tiles</td>
<td>Business entities with distinct state</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q7-what-is-the-mediator-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q7-what-is-the-mediator-pattern">Q7: What is the Mediator Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Mediator pattern</strong> reduces chaotic dependencies between objects by forcing them to communicate only through a mediator object. Instead of N objects knowing about each other (N² connections), they all talk through one mediator (N connections).</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    subgraph Without["Without Mediator (N² connections)"]
        A1["Button"] &lt;--&gt; B1["TextBox"]
        A1 &lt;--&gt; C1["Dropdown"]
        B1 &lt;--&gt; C1
        A1 &lt;--&gt; D1["Checkbox"]
        B1 &lt;--&gt; D1
        C1 &lt;--&gt; D1
    end

    subgraph With["With Mediator (N connections)"]
        M["Mediator&lt;br/&gt;(Dialog)"]
        A2["Button"] --&gt; M
        B2["TextBox"] --&gt; M
        C2["Dropdown"] --&gt; M
        D2["Checkbox"] --&gt; M
        M --&gt; A2
        M --&gt; B2
        M --&gt; C2
        M --&gt; D2
    end

    style Without fill:#ff7851,stroke:#333,color:#fff
    style With fill:#56cc9d,stroke:#333,color:#fff
    style M fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-chat-room" class="level3">
<h3 class="anchored" data-anchor-id="implementation-chat-room">Implementation: Chat Room</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb14-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass, field</span>
<span id="cb14-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime</span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mediator interface</span></span>
<span id="cb14-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ChatMediator(ABC):</span>
<span id="cb14-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb14-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send_message(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, sender: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"User"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb14-9"></span>
<span id="cb14-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb14-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_user(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, user: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"User"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb14-12"></span>
<span id="cb14-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concrete mediator</span></span>
<span id="cb14-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ChatRoom(ChatMediator):</span>
<span id="cb14-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb14-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> name</span>
<span id="cb14-17">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._users: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"User"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb14-18"></span>
<span id="cb14-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_user(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, user: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"User"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb14-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._users.append(user)</span>
<span id="cb14-21">        user.chat_room <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb14-22">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.send_message(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>user<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> joined the room"</span>, user)</span>
<span id="cb14-23"></span>
<span id="cb14-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send_message(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, sender: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"User"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb14-25">        timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now().strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%H:%M"</span>)</span>
<span id="cb14-26">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> user <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._users:</span>
<span id="cb14-27">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> user <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> sender:  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Don't echo to sender</span></span>
<span id="cb14-28">                user.receive(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>timestamp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">] </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sender<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>message<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb14-29"></span>
<span id="cb14-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Colleague</span></span>
<span id="cb14-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb14-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> User:</span>
<span id="cb14-33">    name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb14-34">    chat_room: ChatMediator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb14-35">    messages: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> field(default_factory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>)</span>
<span id="cb14-36"></span>
<span id="cb14-37">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb14-38">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.chat_room:</span>
<span id="cb14-39">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.chat_room.send_message(message, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb14-40"></span>
<span id="cb14-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> receive(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, message: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb14-42">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.messages.append(message)</span>
<span id="cb14-43">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> received: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>message<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb14-44"></span>
<span id="cb14-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage — users don't know about each other, only the mediator</span></span>
<span id="cb14-46">room <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ChatRoom(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Engineering"</span>)</span>
<span id="cb14-47"></span>
<span id="cb14-48">alice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> User(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alice"</span>)</span>
<span id="cb14-49">bob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> User(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bob"</span>)</span>
<span id="cb14-50">charlie <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> User(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Charlie"</span>)</span>
<span id="cb14-51"></span>
<span id="cb14-52">room.add_user(alice)</span>
<span id="cb14-53">room.add_user(bob)</span>
<span id="cb14-54">room.add_user(charlie)</span>
<span id="cb14-55"></span>
<span id="cb14-56">alice.send(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hey team, standup in 5 minutes!"</span>)</span>
<span id="cb14-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bob received: [10:00] Alice: Hey team, standup in 5 minutes!</span></span>
<span id="cb14-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Charlie received: [10:00] Alice: Hey team, standup in 5 minutes!</span></span>
<span id="cb14-59"></span>
<span id="cb14-60">bob.send(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"On my way!"</span>)</span>
<span id="cb14-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Alice received: [10:00] Bob: On my way!</span></span>
<span id="cb14-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Charlie received: [10:00] Bob: On my way!</span></span></code></pre></div></div>
</section>
<section id="mediator-in-enterprise-systems" class="level3">
<h3 class="anchored" data-anchor-id="mediator-in-enterprise-systems">Mediator in Enterprise Systems</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Spring's ApplicationEventPublisher is a Mediator!</span></span>
<span id="cb15-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Service</span></span>
<span id="cb15-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderService <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-4"></span>
<span id="cb15-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Autowired</span></span>
<span id="cb15-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> ApplicationEventPublisher eventPublisher<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Mediator</span></span>
<span id="cb15-7"></span>
<span id="cb15-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> Order <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>OrderRequest request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-9">        Order order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> orderRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Order</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb15-10">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Publish event — OrderService doesn't know who handles it</span></span>
<span id="cb15-11">        eventPublisher<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">publishEvent</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OrderCreatedEvent</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb15-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb15-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-15"></span>
<span id="cb15-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Listeners are decoupled — they don't know about each other</span></span>
<span id="cb15-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Component</span></span>
<span id="cb15-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> InventoryListener <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@EventListener</span></span>
<span id="cb15-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">onOrderCreated</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>OrderCreatedEvent event<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-21">        inventoryService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reserve</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>event<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getItems</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb15-22">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-23"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-24"></span>
<span id="cb15-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Component</span></span>
<span id="cb15-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">NotificationListener</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@EventListener</span></span>
<span id="cb15-28">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">onOrderCreated</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>OrderCreatedEvent event<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-29">        emailService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sendConfirmation</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>event<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getOrder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb15-30">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-31"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="mediator-vs-observer" class="level3">
<h3 class="anchored" data-anchor-id="mediator-vs-observer">Mediator vs Observer</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 28%">
<col style="width: 35%">
<col style="width: 35%">
</colgroup>
<thead>
<tr class="header">
<th>Aspect</th>
<th>Mediator</th>
<th>Observer</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Communication</td>
<td>Bidirectional through mediator</td>
<td>One-way (subject → observers)</td>
</tr>
<tr class="even">
<td>Coupling</td>
<td>Components know the mediator</td>
<td>Observers don’t know each other</td>
</tr>
<tr class="odd">
<td>Complexity</td>
<td>Mediator can become complex (God object)</td>
<td>Simpler, distributed logic</td>
</tr>
<tr class="even">
<td>Example</td>
<td>Chat room, air traffic control</td>
<td>Event bus, pub/sub</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q8-what-is-the-repository-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q8-what-is-the-repository-pattern">Q8: What is the Repository Pattern?</h2>
<p><strong>Answer:</strong></p>
<p>The <strong>Repository pattern</strong> mediates between the domain/business layer and data access, providing a collection-like interface for accessing domain objects. It isolates the domain from persistence concerns and is a cornerstone of both Spring Data and Domain-Driven Design.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    SERVICE["Service Layer&lt;br/&gt;(business logic)"]
    SERVICE --&gt; REPO["Repository Interface&lt;br/&gt;findById(), save(), delete()"]
    REPO --&gt; IMPL1["JPA Implementation"]
    REPO --&gt; IMPL2["MongoDB Implementation"]
    REPO --&gt; IMPL3["In-Memory (tests)"]

    IMPL1 --&gt; DB1["PostgreSQL"]
    IMPL2 --&gt; DB2["MongoDB"]
    IMPL3 --&gt; DB3["HashMap"]

    style REPO fill:#56cc9d,stroke:#333,color:#fff
    style SERVICE fill:#6cc3d5,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="spring-data-jpa-repository" class="level3">
<h3 class="anchored" data-anchor-id="spring-data-jpa-repository">Spring Data JPA Repository</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Spring Data auto-generates the implementation!</span></span>
<span id="cb16-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Repository</span></span>
<span id="cb16-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">interface</span> UserRepository <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">extends</span> JpaRepository<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>User<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-4"></span>
<span id="cb16-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Method name IS the query — Spring generates SQL</span></span>
<span id="cb16-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>User<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findByEmail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> email<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-7"></span>
<span id="cb16-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>User<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findByRoleAndActiveTrue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> role<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-9"></span>
<span id="cb16-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Query</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT u FROM User u WHERE u.createdAt &gt; :since"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb16-11">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>User<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findRecentUsers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Param</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"since"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> LocalDateTime since<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-12"></span>
<span id="cb16-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Pagination built in</span></span>
<span id="cb16-14">    Page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>User<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findByRole</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> role<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pageable</span> pageable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb16-16"></span>
<span id="cb16-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Entity</span></span>
<span id="cb16-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Entity</span></span>
<span id="cb16-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Table</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"users"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb16-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> User <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Id</span></span>
<span id="cb16-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@GeneratedValue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>strategy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GenerationType<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">IDENTITY</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb16-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-24"></span>
<span id="cb16-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Column</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>nullable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> unique <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb16-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> email<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-27"></span>
<span id="cb16-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Column</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>nullable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb16-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-30"></span>
<span id="cb16-31">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> role<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">boolean</span> active<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> LocalDateTime createdAt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-34"></span>
<span id="cb16-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Getters, setters, constructors...</span></span>
<span id="cb16-36"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb16-37"></span>
<span id="cb16-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Service uses repository — doesn't know about JPA/SQL</span></span>
<span id="cb16-39"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Service</span></span>
<span id="cb16-40"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> UserService <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-41"></span>
<span id="cb16-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> UserRepository userRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-43"></span>
<span id="cb16-44">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">UserService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>UserRepository userRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-45">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">userRepo</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> userRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-46">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb16-47"></span>
<span id="cb16-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> User <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createUser</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>CreateUserRequest request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>userRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findByEmail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getEmail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isEmpty</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-50">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">throw</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">DuplicateEmailException</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getEmail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb16-51">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb16-52">        User user <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">User</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getName</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(),</span> request<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getEmail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb16-53">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> userRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>user<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-54">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb16-55"></span>
<span id="cb16-56">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> Page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>User<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listAdmins</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-57">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> userRepo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findByRole</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admin"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> PageRequest<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">of</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb16-58">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb16-59"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="python-repository-pattern" class="level3">
<h3 class="anchored" data-anchor-id="python-repository-pattern">Python Repository Pattern</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Protocol, TypeVar, Generic</span>
<span id="cb17-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass</span>
<span id="cb17-3"></span>
<span id="cb17-4">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> TypeVar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>)</span>
<span id="cb17-5"></span>
<span id="cb17-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generic repository interface</span></span>
<span id="cb17-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Repository(Protocol[T]):</span>
<span id="cb17-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_by_id(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>: ...</span>
<span id="cb17-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> list_all(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[T]: ...</span>
<span id="cb17-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> save(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, entity: T) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> T: ...</span>
<span id="cb17-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> delete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>: ...</span>
<span id="cb17-12"></span>
<span id="cb17-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Domain entity</span></span>
<span id="cb17-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span></span>
<span id="cb17-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> User:</span>
<span id="cb17-16">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb17-17">    name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb17-18">    email: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb17-19">    role: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user"</span></span>
<span id="cb17-20"></span>
<span id="cb17-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PostgreSQL implementation</span></span>
<span id="cb17-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PostgresUserRepository:</span>
<span id="cb17-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, db_pool):</span>
<span id="cb17-24">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pool <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db_pool</span>
<span id="cb17-25"></span>
<span id="cb17-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_by_id(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> User <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb17-27">        row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pool.fetchrow(</span>
<span id="cb17-28">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM users WHERE id = $1"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span></span>
<span id="cb17-29">        )</span>
<span id="cb17-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> User(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(row)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> row <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb17-31"></span>
<span id="cb17-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> list_all(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[User]:</span>
<span id="cb17-33">        rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pool.fetch(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM users"</span>)</span>
<span id="cb17-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [User(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(r)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> rows]</span>
<span id="cb17-35"></span>
<span id="cb17-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> save(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, user: User) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> User:</span>
<span id="cb17-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> user.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb17-38">            row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pool.fetchrow(</span>
<span id="cb17-39">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INSERT INTO users (name, email, role) VALUES ($1, $2, $3) RETURNING id"</span>,</span>
<span id="cb17-40">                user.name, user.email, user.role,</span>
<span id="cb17-41">            )</span>
<span id="cb17-42">            user.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>]</span>
<span id="cb17-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb17-44">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pool.execute(</span>
<span id="cb17-45">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UPDATE users SET name=$1, email=$2, role=$3 WHERE id=$4"</span>,</span>
<span id="cb17-46">                user.name, user.email, user.role, user.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>,</span>
<span id="cb17-47">            )</span>
<span id="cb17-48">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> user</span>
<span id="cb17-49"></span>
<span id="cb17-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> delete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>:</span>
<span id="cb17-51">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pool.execute(</span>
<span id="cb17-52">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DELETE FROM users WHERE id = $1"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span></span>
<span id="cb17-53">        )</span>
<span id="cb17-54">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DELETE 1"</span></span>
<span id="cb17-55"></span>
<span id="cb17-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In-memory implementation (for testing)</span></span>
<span id="cb17-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> InMemoryUserRepository:</span>
<span id="cb17-58">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb17-59">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._store: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, User] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb17-60">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._next_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb17-61"></span>
<span id="cb17-62">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_by_id(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> User <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb17-63">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._store.get(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>)</span>
<span id="cb17-64"></span>
<span id="cb17-65">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> list_all(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[User]:</span>
<span id="cb17-66">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._store.values())</span>
<span id="cb17-67"></span>
<span id="cb17-68">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> save(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, user: User) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> User:</span>
<span id="cb17-69">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> user.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb17-70">            user.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._next_id</span>
<span id="cb17-71">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._next_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb17-72">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._store[user.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> user</span>
<span id="cb17-73">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> user</span>
<span id="cb17-74"></span>
<span id="cb17-75">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> delete(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>:</span>
<span id="cb17-76">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._store.pop(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb17-77"></span>
<span id="cb17-78"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Service works with either implementation</span></span>
<span id="cb17-79"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> UserService:</span>
<span id="cb17-80">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, repo: Repository[User]):</span>
<span id="cb17-81">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> repo</span></code></pre></div></div>
</section>
<section id="repository-vs-dao-vs-active-record" class="level3">
<h3 class="anchored" data-anchor-id="repository-vs-dao-vs-active-record">Repository vs DAO vs Active Record</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 29%">
<col style="width: 41%">
<col style="width: 29%">
</colgroup>
<thead>
<tr class="header">
<th>Pattern</th>
<th>Description</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Repository</strong></td>
<td>Domain-oriented collection interface</td>
<td>Spring Data, custom repos</td>
</tr>
<tr class="even">
<td><strong>DAO</strong></td>
<td>Table-oriented data access</td>
<td>JDBC DAOs, lower-level</td>
</tr>
<tr class="odd">
<td><strong>Active Record</strong></td>
<td>Entity knows how to persist itself</td>
<td>Django ORM, Rails</td>
</tr>
<tr class="even">
<td><strong>Data Mapper</strong></td>
<td>Separate mapper objects</td>
<td>SQLAlchemy (mapper mode)</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q9-what-is-the-mvc-pattern-and-how-does-spring-implement-it" class="level2">
<h2 class="anchored" data-anchor-id="q9-what-is-the-mvc-pattern-and-how-does-spring-implement-it">Q9: What is the MVC Pattern and how does Spring implement it?</h2>
<p><strong>Answer:</strong></p>
<p><strong>Model-View-Controller (MVC)</strong> separates an application into three concerns: the <strong>Model</strong> (data + business logic), the <strong>View</strong> (presentation), and the <strong>Controller</strong> (handles input and coordinates model/view).</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    USER["User (Browser)"]
    USER --&gt;|"HTTP Request"| CONTROLLER["Controller&lt;br/&gt;(handles input)"]
    CONTROLLER --&gt;|"calls"| MODEL["Model / Service&lt;br/&gt;(business logic)"]
    MODEL --&gt;|"returns data"| CONTROLLER
    CONTROLLER --&gt;|"selects + populates"| VIEW["View&lt;br/&gt;(renders response)"]
    VIEW --&gt;|"HTTP Response"| USER

    subgraph Spring["Spring MVC"]
        DS["DispatcherServlet&lt;br/&gt;(Front Controller)"]
        DS --&gt; HM["Handler Mapping&lt;br/&gt;(route → controller)"]
        DS --&gt; HA["Handler Adapter&lt;br/&gt;(invoke controller)"]
        DS --&gt; VR["View Resolver&lt;br/&gt;(template → HTML)"]
    end

    style CONTROLLER fill:#56cc9d,stroke:#333,color:#fff
    style MODEL fill:#6cc3d5,stroke:#333,color:#fff
    style VIEW fill:#ffce67,stroke:#333
    style DS fill:#ff7851,stroke:#333,color:#fff
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="spring-mvc-implementation" class="level3">
<h3 class="anchored" data-anchor-id="spring-mvc-implementation">Spring MVC Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// MODEL — Entity + Service</span></span>
<span id="cb18-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Entity</span></span>
<span id="cb18-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Product <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Id</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@GeneratedValue</span></span>
<span id="cb18-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">BigDecimal</span> price<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> stock<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Getters, setters...</span></span>
<span id="cb18-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-11"></span>
<span id="cb18-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Service</span></span>
<span id="cb18-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ProductService <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> ProductRepository repo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-15"></span>
<span id="cb18-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ProductService</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ProductRepository repo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-17">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repo</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> repo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-18">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-19"></span>
<span id="cb18-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Product<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listAll</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> repo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findAll</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> Product <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getById</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> repo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findById</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-23">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">orElseThrow</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ProductNotFoundException</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb18-24">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> Product <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Product product<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> repo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>product<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-26"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-27"></span>
<span id="cb18-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// CONTROLLER — handles HTTP requests</span></span>
<span id="cb18-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@RestController</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Returns JSON (no view resolution)</span></span>
<span id="cb18-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@RequestMapping</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/api/v1/products"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ProductController <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-32"></span>
<span id="cb18-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">final</span> ProductService productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-34"></span>
<span id="cb18-35">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ProductController</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ProductService productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-36">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">productService</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-37">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-38"></span>
<span id="cb18-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@GetMapping</span></span>
<span id="cb18-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Product<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listProducts</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listAll</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb18-42">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-43"></span>
<span id="cb18-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@GetMapping</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/{id}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> Product <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getProduct</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@PathVariable</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getById</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-47">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-48"></span>
<span id="cb18-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@PostMapping</span></span>
<span id="cb18-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@ResponseStatus</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>HttpStatus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CREATED</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-51">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> Product <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">createProduct</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Valid</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@RequestBody</span> Product product<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-52">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>product<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-53">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-54"></span>
<span id="cb18-55">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Exception handling</span></span>
<span id="cb18-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@ExceptionHandler</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ProductNotFoundException<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@ResponseStatus</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>HttpStatus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NOT_FOUND</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-58">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">handleNotFound</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ProductNotFoundException ex<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-59">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">of</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ex<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMessage</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb18-60">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-61"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-62"></span>
<span id="cb18-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// VIEW — for server-side rendering (Thymeleaf)</span></span>
<span id="cb18-64"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Controller</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Returns view names (not @RestController)</span></span>
<span id="cb18-65"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@RequestMapping</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/products"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-66"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ProductViewController <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-67"></span>
<span id="cb18-68">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@GetMapping</span></span>
<span id="cb18-69">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listProducts</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Model model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-70">        model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addAttribute</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"products"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listAll</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb18-71">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"products/list"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// → templates/products/list.html</span></span>
<span id="cb18-72">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-73"></span>
<span id="cb18-74">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@GetMapping</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/{id}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb18-75">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">showProduct</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@PathVariable</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Model model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-76">        model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addAttribute</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> productService<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getById</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb18-77">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"products/detail"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-78">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-79"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="mvc-variants" class="level3">
<h3 class="anchored" data-anchor-id="mvc-variants">MVC Variants</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 18%">
<col style="width: 22%">
<col style="width: 39%">
<col style="width: 18%">
</colgroup>
<thead>
<tr class="header">
<th>Variant</th>
<th>View Layer</th>
<th>Controller Returns</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>MVC</strong> (classic)</td>
<td>Server-side templates</td>
<td>View name + Model</td>
<td>Spring MVC + Thymeleaf</td>
</tr>
<tr class="even">
<td><strong>REST API</strong> (no view)</td>
<td>Client (React, mobile)</td>
<td>JSON directly</td>
<td><code>@RestController</code></td>
</tr>
<tr class="odd">
<td><strong>MVP</strong></td>
<td>Passive view</td>
<td>Updates view directly</td>
<td>Android (legacy)</td>
</tr>
<tr class="even">
<td><strong>MVVM</strong></td>
<td>Data-bound view</td>
<td>ViewModel + bindings</td>
<td>WPF, Vue.js, SwiftUI</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="q10-what-is-the-cqrs-pattern" class="level2">
<h2 class="anchored" data-anchor-id="q10-what-is-the-cqrs-pattern">Q10: What is the CQRS Pattern?</h2>
<p><strong>Answer:</strong></p>
<p><strong>Command Query Responsibility Segregation (CQRS)</strong> splits the data model into separate <strong>command</strong> (write) and <strong>query</strong> (read) models. Writes and reads often have different requirements — CQRS lets you optimize each independently.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    CLIENT["Client"]
    CLIENT --&gt;|"Write (Command)"| CMD["Command Handler&lt;br/&gt;(validates, applies rules)"]
    CLIENT --&gt;|"Read (Query)"| QUERY["Query Handler&lt;br/&gt;(optimized for reads)"]

    CMD --&gt; WRITE_DB["Write Model&lt;br/&gt;(normalized, consistent)"]
    QUERY --&gt; READ_DB["Read Model&lt;br/&gt;(denormalized, fast)"]

    WRITE_DB --&gt;|"Events / Sync"| READ_DB

    style CMD fill:#ff7851,stroke:#333,color:#fff
    style QUERY fill:#56cc9d,stroke:#333,color:#fff
    style WRITE_DB fill:#6cc3d5,stroke:#333,color:#fff
    style READ_DB fill:#ffce67,stroke:#333
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<section id="implementation-1" class="level3">
<h3 class="anchored" data-anchor-id="implementation-1">Implementation</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dataclasses <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dataclass, field</span>
<span id="cb19-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime</span>
<span id="cb19-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb19-4"></span>
<span id="cb19-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># COMMANDS (writes)</span></span>
<span id="cb19-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span>(frozen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb19-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CreateOrderCommand:</span>
<span id="cb19-8">    user_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb19-9">    items: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]</span>
<span id="cb19-10">    shipping_address: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb19-11"></span>
<span id="cb19-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span>(frozen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb19-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CancelOrderCommand:</span>
<span id="cb19-14">    order_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb19-15">    reason: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb19-16"></span>
<span id="cb19-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Command handler — enforces business rules</span></span>
<span id="cb19-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderCommandHandler:</span>
<span id="cb19-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, write_repo, event_bus):</span>
<span id="cb19-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> write_repo</span>
<span id="cb19-21">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._events <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> event_bus</span>
<span id="cb19-22"></span>
<span id="cb19-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle_create(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, cmd: CreateOrderCommand) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb19-24">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Validate business rules</span></span>
<span id="cb19-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> cmd.items:</span>
<span id="cb19-26">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order must have at least one item"</span>)</span>
<span id="cb19-27"></span>
<span id="cb19-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create in write model (normalized)</span></span>
<span id="cb19-29">        order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Order(</span>
<span id="cb19-30">            user_id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cmd.user_id,</span>
<span id="cb19-31">            items<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cmd.items,</span>
<span id="cb19-32">            address<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cmd.shipping_address,</span>
<span id="cb19-33">            status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pending"</span>,</span>
<span id="cb19-34">            created_at<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>datetime.now(),</span>
<span id="cb19-35">        )</span>
<span id="cb19-36">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo.save(order)</span>
<span id="cb19-37"></span>
<span id="cb19-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Publish event to sync read model</span></span>
<span id="cb19-39">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._events.publish(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_created"</span>, order_id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>order.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>, data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>order.to_dict())</span>
<span id="cb19-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> order.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span></span>
<span id="cb19-41"></span>
<span id="cb19-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle_cancel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, cmd: CancelOrderCommand) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb19-43">        order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo.get(cmd.order_id)</span>
<span id="cb19-44">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> order.status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shipped"</span>:</span>
<span id="cb19-45">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cannot cancel shipped order"</span>)</span>
<span id="cb19-46">        order.status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cancelled"</span></span>
<span id="cb19-47">        order.cancel_reason <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cmd.reason</span>
<span id="cb19-48">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo.save(order)</span>
<span id="cb19-49">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._events.publish(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_cancelled"</span>, order_id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>order.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>)</span>
<span id="cb19-50"></span>
<span id="cb19-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># QUERIES (reads)</span></span>
<span id="cb19-52"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span>(frozen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb19-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> GetOrderQuery:</span>
<span id="cb19-54">    order_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb19-55"></span>
<span id="cb19-56"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@dataclass</span>(frozen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb19-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ListUserOrdersQuery:</span>
<span id="cb19-58">    user_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span></span>
<span id="cb19-59">    page: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb19-60">    page_size: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb19-61"></span>
<span id="cb19-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Query handler — optimized for read performance</span></span>
<span id="cb19-63"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderQueryHandler:</span>
<span id="cb19-64">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, read_repo):</span>
<span id="cb19-65">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_repo  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Denormalized read model</span></span>
<span id="cb19-66"></span>
<span id="cb19-67">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle_get(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, query: GetOrderQuery) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb19-68">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read from denormalized view (fast, no JOINs)</span></span>
<span id="cb19-69">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo.get_order_view(query.order_id)</span>
<span id="cb19-70"></span>
<span id="cb19-71">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> handle_list(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, query: ListUserOrdersQuery) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb19-72">        orders, total <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo.list_user_orders(</span>
<span id="cb19-73">            query.user_id, query.page, query.page_size</span>
<span id="cb19-74">        )</span>
<span id="cb19-75">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"items"</span>: orders, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total"</span>: total, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"page"</span>: query.page}</span>
<span id="cb19-76"></span>
<span id="cb19-77"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read model sync (event listener)</span></span>
<span id="cb19-78"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderReadModelUpdater:</span>
<span id="cb19-79">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Listens to write events and updates the denormalized read model."""</span></span>
<span id="cb19-80"></span>
<span id="cb19-81">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, read_repo):</span>
<span id="cb19-82">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_repo</span>
<span id="cb19-83"></span>
<span id="cb19-84">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> on_order_created(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, event: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb19-85">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Denormalize: pre-join user name, item details, totals</span></span>
<span id="cb19-86">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._repo.upsert_order_view({</span>
<span id="cb19-87">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_id"</span>: event[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_id"</span>],</span>
<span id="cb19-88">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_name"</span>: user_service.get_name(event[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_id"</span>]),</span>
<span id="cb19-89">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"items_summary"</span>: summarize_items(event[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"items"</span>]),</span>
<span id="cb19-90">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total"</span>: calculate_total(event[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"items"</span>]),</span>
<span id="cb19-91">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pending"</span>,</span>
<span id="cb19-92">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"created_at"</span>: event[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"created_at"</span>],</span>
<span id="cb19-93">        })</span>
<span id="cb19-94"></span>
<span id="cb19-95"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># API layer</span></span>
<span id="cb19-96"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.post</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/api/v1/orders"</span>, status_code<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">201</span>)</span>
<span id="cb19-97"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_order(cmd: CreateOrderCommand):</span>
<span id="cb19-98">    order_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> command_handler.handle_create(cmd)</span>
<span id="cb19-99">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_id"</span>: order_id}</span>
<span id="cb19-100"></span>
<span id="cb19-101"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/api/v1/orders/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{order_id}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-102"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_order(order_id: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>):</span>
<span id="cb19-103">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> query_handler.handle_get(GetOrderQuery(order_id))</span></code></pre></div></div>
</section>
<section id="spring-cqrs-with-separate-models" class="level3">
<h3 class="anchored" data-anchor-id="spring-cqrs-with-separate-models">Spring CQRS with Separate Models</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode java code-with-copy"><code class="sourceCode java"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Write model — normalized, validated</span></span>
<span id="cb20-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Entity</span></span>
<span id="cb20-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Table</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orders"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb20-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Id</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@GeneratedValue</span></span>
<span id="cb20-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> userId<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@OneToMany</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cascade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CascadeType<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ALL</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb20-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>OrderItem<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> items<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> LocalDateTime createdAt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-13"></span>
<span id="cb20-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Read model — denormalized for fast queries</span></span>
<span id="cb20-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Entity</span></span>
<span id="cb20-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Table</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"order_views"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb20-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> OrderView <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Id</span></span>
<span id="cb20-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span> orderId<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> userName<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Pre-joined</span></span>
<span id="cb20-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> itemsSummary<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Pre-computed</span></span>
<span id="cb20-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">BigDecimal</span> total<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Pre-calculated</span></span>
<span id="cb20-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> LocalDateTime createdAt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-26"></span>
<span id="cb20-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Separate repositories</span></span>
<span id="cb20-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Repository</span></span>
<span id="cb20-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">interface</span> OrderRepository <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">extends</span> JpaRepository<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-30"></span>
<span id="cb20-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@Repository</span></span>
<span id="cb20-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">interface</span> OrderViewRepository <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">extends</span> JpaRepository<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>OrderView<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Long</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-33">    Page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>OrderView<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findByUserNameContaining</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span> name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pageable</span> pageable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb20-34"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="when-to-use-cqrs" class="level3">
<h3 class="anchored" data-anchor-id="when-to-use-cqrs">When to Use CQRS</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 38%">
<col style="width: 61%">
</colgroup>
<thead>
<tr class="header">
<th>Use CQRS</th>
<th>Don’t Use CQRS</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Read and write workloads differ significantly</td>
<td>Simple CRUD applications</td>
</tr>
<tr class="even">
<td>Read model needs denormalization</td>
<td>Reads and writes have same shape</td>
</tr>
<tr class="odd">
<td>Scaling reads and writes independently</td>
<td>Small teams / simple domains</td>
</tr>
<tr class="even">
<td>Event sourcing architecture</td>
<td>When eventual consistency is unacceptable</td>
</tr>
<tr class="odd">
<td>Complex reporting / search requirements</td>
<td>When added complexity isn’t justified</td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="summary-table" class="level2">
<h2 class="anchored" data-anchor-id="summary-table">Summary Table</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 9%">
<col style="width: 28%">
<col style="width: 31%">
<col style="width: 31%">
</colgroup>
<thead>
<tr class="header">
<th>#</th>
<th>Pattern</th>
<th>Category</th>
<th>Key Idea</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td><strong>Spring DI</strong></td>
<td>IoC / Creational</td>
<td>Container injects dependencies via <code>@Autowired</code> / constructor</td>
</tr>
<tr class="even">
<td>2</td>
<td><strong>Inversion of Control</strong></td>
<td>Principle</td>
<td>Framework controls object creation and flow</td>
</tr>
<tr class="odd">
<td>3</td>
<td><strong>Template Method</strong></td>
<td>Behavioral</td>
<td>Algorithm skeleton in base class, steps in subclasses</td>
</tr>
<tr class="even">
<td>4</td>
<td><strong>State</strong></td>
<td>Behavioral</td>
<td>Object behavior changes with internal state transitions</td>
</tr>
<tr class="odd">
<td>5</td>
<td><strong>Facade</strong></td>
<td>Structural</td>
<td>Simplified interface to a complex subsystem</td>
</tr>
<tr class="even">
<td>6</td>
<td><strong>Flyweight</strong></td>
<td>Structural</td>
<td>Share common state across many objects to save memory</td>
</tr>
<tr class="odd">
<td>7</td>
<td><strong>Mediator</strong></td>
<td>Behavioral</td>
<td>Central hub reduces N² dependencies to N</td>
</tr>
<tr class="even">
<td>8</td>
<td><strong>Repository</strong></td>
<td>Data Access</td>
<td>Collection-like interface isolating domain from persistence</td>
</tr>
<tr class="odd">
<td>9</td>
<td><strong>MVC</strong></td>
<td>Architectural</td>
<td>Separate Model, View, Controller concerns</td>
</tr>
<tr class="even">
<td>10</td>
<td><strong>CQRS</strong></td>
<td>Architectural</td>
<td>Separate read and write models for independent optimization</td>
</tr>
</tbody>
</table>
<hr>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s Next?</h2>
<p>This article covered enterprise and framework-level patterns. For related content:</p>
<ul>
<li><strong>Core GoF patterns:</strong> <a href="../../posts/design-pattern/Design-Pattern-Interview-QA-1.html">Design Pattern Interview QA - 1</a></li>
<li><strong>Python design patterns (Pythonic style):</strong> <a href="../../posts/swe-interview/Python-SWE-Interview-QA-3.html">Python SWE Interview QA - 3</a></li>
<li><strong>Production API patterns:</strong> <a href="../../posts/swe-interview/Python-SWE-Interview-QA-4.html">Python SWE Interview QA - 4</a></li>
<li><strong>Python fundamentals:</strong> <a href="../../posts/swe-interview/Python-SWE-Interview-QA-1.html">Python SWE Interview QA - 1</a></li>
<li><strong>LLM architecture:</strong> <a href="../../posts/llm-interview/LLM-Interview-QA-1.html">LLM Interview QA - 1</a></li>
</ul>


</section>

 ]]></description>
  <guid>https://vectoringai.com/posts/design-pattern/Design-Pattern-Interview-QA-2.html</guid>
  <pubDate>Thu, 21 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://vectoringai.com/images/design-pattern/thumb_design_pattern_interview_qa_300.png" medium="image" type="image/png" height="96" width="144"/>
</item>
</channel>
</rss>
