<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Design-Patterns on Java &amp; Software Architecture Knowledge Hub</title><link>https://samirsahoo.github.io/design-patterns-hub/tags/design-patterns/</link><description>Recent content in Design-Patterns on Java &amp; Software Architecture Knowledge Hub</description><generator>Hugo</generator><language>en-US</language><copyright>© 2026 Samir Kumar Sahoo (https://github.com/samirsahoo). All rights reserved.</copyright><atom:link href="https://samirsahoo.github.io/design-patterns-hub/tags/design-patterns/index.xml" rel="self" type="application/rss+xml"/><item><title>Abstract Factory Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/abstract-factory/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/abstract-factory/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Abstract Factory&lt;/strong&gt; pattern provides one interface for creating a &lt;strong&gt;family of related objects&lt;/strong&gt;. Client code asks the factory for each object it needs and never names a concrete class, so a whole family can be swapped by swapping one factory.&lt;/p&gt;</description></item><item><title>Adapter vs Decorator vs Proxy vs Facade</title><link>https://samirsahoo.github.io/design-patterns-hub/comparisons/adapter-decorator-proxy-facade/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/comparisons/adapter-decorator-proxy-facade/</guid><description>&lt;h2 id="the-deciding-question"&gt;The deciding question&lt;a class="heading-anchor" href="#the-deciding-question" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;All four patterns put an object in front of another object. They differ in &lt;strong&gt;why&lt;/strong&gt;:&lt;/p&gt;&#10;&lt;div class="table-wrap" tabindex="0" role="region" aria-label="Table"&gt;&#10;&lt;table&gt;&#10; &lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th&gt;Pattern&lt;/th&gt;&#10; &lt;th&gt;The wrapper exists to…&lt;/th&gt;&#10; &lt;th&gt;Interface towards the client&lt;/th&gt;&#10; &lt;th&gt;Wraps&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Adapter&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;&lt;em&gt;translate&lt;/em&gt; an incompatible interface&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;Different&lt;/strong&gt; from the wrapped object&amp;rsquo;s&lt;/td&gt;&#10; &lt;td&gt;One object&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Decorator&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;&lt;em&gt;add&lt;/em&gt; behaviour while keeping the interface&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;Same&lt;/strong&gt; as the wrapped object&amp;rsquo;s&lt;/td&gt;&#10; &lt;td&gt;One object (stackable)&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Proxy&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;&lt;em&gt;control access&lt;/em&gt; to the real object&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;Same&lt;/strong&gt; as the real object&amp;rsquo;s&lt;/td&gt;&#10; &lt;td&gt;One object&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Facade&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;&lt;em&gt;simplify&lt;/em&gt; a complex subsystem&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;New&lt;/strong&gt;, simpler interface&lt;/td&gt;&#10; &lt;td&gt;Many objects&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;/div&gt;&#10;&lt;aside class="callout callout--key" role="note"&gt;&#10; &lt;p class="callout__title"&gt;&lt;span class="callout__label"&gt;Key concept&lt;/span&gt;&lt;span class="callout__heading"&gt;Ask two questions&lt;/span&gt;&lt;/p&gt;&#10; &lt;div class="callout__body"&gt;&lt;ol&gt;&#10;&lt;li&gt;Does the client see the &lt;strong&gt;same interface&lt;/strong&gt; as the wrapped object? If not, it is an Adapter or a Facade.&lt;/li&gt;&#10;&lt;li&gt;Is the purpose to &lt;strong&gt;add behaviour&lt;/strong&gt; (Decorator), &lt;strong&gt;control access&lt;/strong&gt; (Proxy), &lt;strong&gt;translate&lt;/strong&gt; (Adapter) or &lt;strong&gt;simplify&lt;/strong&gt; (Facade)?&lt;/li&gt;&#10;&lt;/ol&gt;&lt;/div&gt;&#10;&lt;/aside&gt;</description></item><item><title>What Are Design Patterns?</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/what-are-design-patterns/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/what-are-design-patterns/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;A &lt;strong&gt;design pattern&lt;/strong&gt; is a named, proven solution to a problem that keeps recurring in software design. It is described at the level of roles and relationships, not as finished code.&lt;/p&gt;&#10;&lt;p&gt;A pattern is not a library you download. It is closer to a recipe that experienced developers recognise: &amp;ldquo;when you face &lt;em&gt;this&lt;/em&gt; situation, arranging objects like &lt;em&gt;this&lt;/em&gt; tends to work, and here is what it costs.&amp;rdquo;&lt;/p&gt;</description></item><item><title>Factory Method Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/factory-method/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/factory-method/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Factory Method&lt;/strong&gt; pattern declares a method for creating an object in a base class, and lets &lt;strong&gt;subclasses&lt;/strong&gt; decide which concrete class to instantiate. The base class contains the code that &lt;em&gt;uses&lt;/em&gt; the object; subclasses supply only the creation step.&lt;/p&gt;</description></item><item><title>Factory Patterns Compared</title><link>https://samirsahoo.github.io/design-patterns-hub/comparisons/factory-patterns/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/comparisons/factory-patterns/</guid><description>&lt;h2 id="the-five-factories"&gt;The five &amp;ldquo;factories&amp;rdquo;&lt;a class="heading-anchor" href="#the-five-factories" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;div class="table-wrap" tabindex="0" role="region" aria-label="Table"&gt;&#10;&lt;table&gt;&#10; &lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th&gt;Technique&lt;/th&gt;&#10; &lt;th&gt;GoF pattern?&lt;/th&gt;&#10; &lt;th&gt;Mechanism&lt;/th&gt;&#10; &lt;th&gt;Choice made by&lt;/th&gt;&#10; &lt;th&gt;Creates&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Static factory method&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;No (Java idiom, &lt;em&gt;Effective Java&lt;/em&gt; Item 1)&lt;/td&gt;&#10; &lt;td&gt;A named static method instead of a constructor&lt;/td&gt;&#10; &lt;td&gt;The method, from its arguments&lt;/td&gt;&#10; &lt;td&gt;One object&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Simple factory&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;No&lt;/td&gt;&#10; &lt;td&gt;One method with a conditional&lt;/td&gt;&#10; &lt;td&gt;A runtime value&lt;/td&gt;&#10; &lt;td&gt;One object of several possible classes&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Factory Method&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;An overridable method in a creator class&lt;/td&gt;&#10; &lt;td&gt;The &lt;strong&gt;subclass&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;One product&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Abstract Factory&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;An object with several creation methods&lt;/td&gt;&#10; &lt;td&gt;The &lt;strong&gt;factory object&lt;/strong&gt; you pass in&lt;/td&gt;&#10; &lt;td&gt;A &lt;strong&gt;family&lt;/strong&gt; of related products&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;Builder&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Step-by-step construction&lt;/td&gt;&#10; &lt;td&gt;The client (and director)&lt;/td&gt;&#10; &lt;td&gt;One complex object&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;/div&gt;&#10;&lt;h2 id="side-by-side"&gt;Side by side&lt;a class="heading-anchor" href="#side-by-side" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Static factory method: a named constructor.&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;b&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Simple factory: choose a class from data.&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;ReportWriter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ReportWriters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forFormat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;csv&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Factory Method (GoF): the subclass decides; the base class owns the workflow.&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;abstract&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReportExporter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;protected&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;abstract&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ReportWriter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;createWriter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ExportedFile&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;export&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ReportWriter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;createWriter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Abstract Factory (GoF): one object creates a consistent family.&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UiFactory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Button&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;createButton&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Checkbox&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;createCheckbox&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;final&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DarkThemeFactory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;implements&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;UiFactory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// dark button + dark checkbox&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;final&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LightThemeFactory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;implements&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;UiFactory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// light button + light checkbox&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Builder (GoF / Effective Java): assemble one complex object step by step.&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;SearchQuery&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SearchQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;laptop&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;computers&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure class="diagram" aria-label="Factory Method versus Abstract Factory"&gt;&lt;figcaption class="diagram__title"&gt;Factory Method versus Abstract Factory&lt;/figcaption&gt;&#10; &lt;pre class="mermaid"&gt;classDiagram&#10; direction LR&#10; class Creator {&#10; &amp;lt;&amp;lt;abstract&amp;gt;&amp;gt;&#10; #createProduct()* Product&#10; +operation()&#10; }&#10; class ConcreteCreator {&#10; #createProduct() Product&#10; }&#10; Creator &amp;lt;|-- ConcreteCreator&#10; class AbstractFactory {&#10; &amp;lt;&amp;lt;interface&amp;gt;&amp;gt;&#10; +createButton() Button&#10; +createCheckbox() Checkbox&#10; }&#10; class DarkThemeFactory&#10; class LightThemeFactory&#10; AbstractFactory &amp;lt;|.. DarkThemeFactory&#10; AbstractFactory &amp;lt;|.. LightThemeFactory&lt;/pre&gt;&#10;&lt;/figure&gt;&#10;&lt;h2 id="how-to-choose"&gt;How to choose&lt;a class="heading-anchor" href="#how-to-choose" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Do you just need a readable alternative to a constructor, perhaps with caching or a subtype? → &lt;strong&gt;Static factory method.&lt;/strong&gt;&lt;/li&gt;&#10;&lt;li&gt;Does the class depend on a runtime value such as configuration or user input? → &lt;strong&gt;Simple factory&lt;/strong&gt;, or a &lt;code&gt;Map&amp;lt;String, Supplier&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt;, or DI.&lt;/li&gt;&#10;&lt;li&gt;Does a base class own a workflow that needs an object whose class only subclasses know? → &lt;strong&gt;Factory Method.&lt;/strong&gt;&lt;/li&gt;&#10;&lt;li&gt;Must several related objects be created consistently — all from the same family? → &lt;strong&gt;Abstract Factory.&lt;/strong&gt;&lt;/li&gt;&#10;&lt;li&gt;Does one object have many optional parts or complex validation? → &lt;strong&gt;Builder.&lt;/strong&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;aside class="callout callout--mistake" role="note"&gt;&#10; &lt;p class="callout__title"&gt;&lt;span class="callout__label"&gt;Common mistake&lt;/span&gt;&lt;span class="callout__heading"&gt;&amp;ldquo;Factory Method&amp;rdquo; as a label for any creation method&lt;/span&gt;&lt;/p&gt;&#10; &lt;div class="callout__body"&gt;&lt;p&gt;Calling &lt;code&gt;ReportWriters.forFormat(...)&lt;/code&gt; or &lt;code&gt;List.of(...)&lt;/code&gt; a Factory Method is the most common inaccuracy in interviews about creational patterns. Neither involves a subclass overriding a creation step.&lt;/p&gt;&lt;/div&gt;&#10;&lt;/aside&gt;</description></item><item><title>SOLID Principles Explained</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/solid-principles/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/solid-principles/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;SOLID&lt;/strong&gt; is an acronym for five object-oriented design principles. Each one keeps code easy to change:&lt;/p&gt;&#10;&lt;div class="table-wrap" tabindex="0" role="region" aria-label="Table"&gt;&#10;&lt;table&gt;&#10; &lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th&gt;Letter&lt;/th&gt;&#10; &lt;th&gt;Principle&lt;/th&gt;&#10; &lt;th&gt;One-line summary&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;S&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Single Responsibility&lt;/td&gt;&#10; &lt;td&gt;A module should have one reason to change — one actor it answers to.&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;O&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Open/Closed&lt;/td&gt;&#10; &lt;td&gt;Add behaviour by adding code, not by editing working code.&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;L&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Liskov Substitution&lt;/td&gt;&#10; &lt;td&gt;A subtype must be usable anywhere its supertype is expected.&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;I&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Interface Segregation&lt;/td&gt;&#10; &lt;td&gt;Clients should not depend on methods they do not use.&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;D&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Dependency Inversion&lt;/td&gt;&#10; &lt;td&gt;Policy should not depend on details; both depend on abstractions.&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Robert C. Martin collected these principles around 2000. Michael Feathers later arranged them into the SOLID acronym.&lt;/p&gt;</description></item><item><title>OO Design Principles Behind the Patterns</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/oo-design-principles/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/oo-design-principles/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;Design patterns are not a list of tricks to memorise. Almost all of them are &lt;strong&gt;the same handful of principles applied to a particular problem&lt;/strong&gt;. Learn the principles and you can recognise patterns, adapt them, and know when &lt;em&gt;not&lt;/em&gt; to use them.&lt;/p&gt;</description></item><item><title>Strategy vs State vs Template Method</title><link>https://samirsahoo.github.io/design-patterns-hub/comparisons/strategy-state-template-method/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/comparisons/strategy-state-template-method/</guid><description>&lt;h2 id="the-short-answer"&gt;The short answer&lt;a class="heading-anchor" href="#the-short-answer" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;div class="table-wrap" tabindex="0" role="region" aria-label="Table"&gt;&#10;&lt;table&gt;&#10; &lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th&gt;Pattern&lt;/th&gt;&#10; &lt;th&gt;What varies&lt;/th&gt;&#10; &lt;th&gt;How&lt;/th&gt;&#10; &lt;th&gt;Who chooses&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;&lt;a href="https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/strategy/"&gt;Strategy&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;The whole algorithm&lt;/td&gt;&#10; &lt;td&gt;Composition: the context holds an interchangeable object&lt;/td&gt;&#10; &lt;td&gt;The &lt;strong&gt;client&lt;/strong&gt;, usually once&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;&lt;a href="https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/state/"&gt;State&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Behaviour per lifecycle state&lt;/td&gt;&#10; &lt;td&gt;Composition: the context holds the current state object&lt;/td&gt;&#10; &lt;td&gt;The &lt;strong&gt;states themselves&lt;/strong&gt;, on each transition&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;&lt;strong&gt;&lt;a href="https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/template-method/"&gt;Template Method&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td&gt;Some steps of a fixed algorithm&lt;/td&gt;&#10; &lt;td&gt;Inheritance: subclasses override steps&lt;/td&gt;&#10; &lt;td&gt;The &lt;strong&gt;subclass&lt;/strong&gt;, at compile time&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;The deciding questions:&lt;/p&gt;</description></item><item><title>Builder Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/builder/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/builder/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Builder&lt;/strong&gt; pattern constructs a complex object step by step, instead of in one constructor call. The construction logic lives in a separate builder object, so the same steps can produce a valid, often immutable, result.&lt;/p&gt;</description></item><item><title>All 23 GoF Patterns: Overview and Classification</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/gof-overview/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/gof-overview/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Gang of Four (GoF) catalogue&lt;/strong&gt; is the set of 23 object-oriented design patterns published in &lt;em&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/em&gt; (1994). The book sorts them along two axes:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt; — what the pattern does: &lt;em&gt;creational&lt;/em&gt;, &lt;em&gt;structural&lt;/em&gt; or &lt;em&gt;behavioral&lt;/em&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Scope&lt;/strong&gt; — whether the relationships are fixed at compile time through inheritance (&lt;em&gt;class&lt;/em&gt; scope) or set up at runtime through composition (&lt;em&gt;object&lt;/em&gt; scope).&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This page is the map. Each pattern gets its own article; patterns not yet written are listed as plain text so that no page pretends to be finished.&lt;/p&gt;</description></item><item><title>Observer vs Mediator vs Event Bus</title><link>https://samirsahoo.github.io/design-patterns-hub/comparisons/observer-mediator-event-bus/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/comparisons/observer-mediator-event-bus/</guid><description>&lt;h2 id="the-short-answer"&gt;The short answer&lt;a class="heading-anchor" href="#the-short-answer" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;div class="table-wrap" tabindex="0" role="region" aria-label="Table"&gt;&#10;&lt;table&gt;&#10; &lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th&gt;&lt;/th&gt;&#10; &lt;th&gt;&lt;a href="https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/observer/"&gt;Observer&lt;/a&gt;&lt;/th&gt;&#10; &lt;th&gt;&lt;a href="https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/mediator/"&gt;Mediator&lt;/a&gt;&lt;/th&gt;&#10; &lt;th&gt;Event bus (publish/subscribe)&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;Who knows whom&lt;/td&gt;&#10; &lt;td&gt;Subject knows its observers (through an interface)&lt;/td&gt;&#10; &lt;td&gt;Colleagues know the mediator; mediator knows colleagues&lt;/td&gt;&#10; &lt;td&gt;Publishers and subscribers know only the bus and event types&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;Where the logic is&lt;/td&gt;&#10; &lt;td&gt;Each observer decides how to react&lt;/td&gt;&#10; &lt;td&gt;&lt;strong&gt;Centralised&lt;/strong&gt; in the mediator&lt;/td&gt;&#10; &lt;td&gt;Each subscriber decides; nobody coordinates&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;Direction&lt;/td&gt;&#10; &lt;td&gt;One-to-many broadcast&lt;/td&gt;&#10; &lt;td&gt;Many-to-one-to-many, two-way&lt;/td&gt;&#10; &lt;td&gt;Many-to-many broadcast&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;Control flow&lt;/td&gt;&#10; &lt;td&gt;Easy to find: look at the subject&amp;rsquo;s observers&lt;/td&gt;&#10; &lt;td&gt;Easy to find: read the mediator&lt;/td&gt;&#10; &lt;td&gt;Hard to find: search for subscribers of an event type&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;Typical scope&lt;/td&gt;&#10; &lt;td&gt;One object and its dependents&lt;/td&gt;&#10; &lt;td&gt;One dialog, workflow or interaction&lt;/td&gt;&#10; &lt;td&gt;A whole application, or many services via a broker&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;The deciding questions:&lt;/p&gt;</description></item><item><title>Prototype Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/prototype/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/prototype/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Prototype&lt;/strong&gt; pattern creates a new object by &lt;strong&gt;copying an existing one&lt;/strong&gt;, the prototype, and then adjusting the copy. The client does not need to know the concrete class or how the original was configured.&lt;/p&gt;</description></item><item><title>Singleton Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/singleton/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/creational/singleton/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Singleton&lt;/strong&gt; pattern makes sure a class has &lt;strong&gt;exactly one instance&lt;/strong&gt; in a process and gives everyone a way to reach it.&lt;/p&gt;&#10;&lt;p&gt;GoF intent: &lt;em&gt;&amp;ldquo;Ensure a class only has one instance, and provide a global point of access to it.&amp;rdquo;&lt;/em&gt;&lt;/p&gt;</description></item><item><title>Adapter Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/adapter/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/adapter/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Adapter&lt;/strong&gt; pattern lets code work with a class whose interface does not match what the code expects. The adapter implements the interface the client wants and translates every call into the interface the existing class provides.&lt;/p&gt;</description></item><item><title>Bridge Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/bridge/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/bridge/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Bridge&lt;/strong&gt; pattern separates &lt;em&gt;what&lt;/em&gt; something does (the &lt;strong&gt;abstraction&lt;/strong&gt;) from &lt;em&gt;how&lt;/em&gt; it is done (the &lt;strong&gt;implementation&lt;/strong&gt;) into two class hierarchies. The abstraction holds a reference to an implementation object; that reference is the &amp;ldquo;bridge&amp;rdquo;. Both sides can then be extended independently.&lt;/p&gt;</description></item><item><title>Composite Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/composite/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/composite/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Composite&lt;/strong&gt; pattern arranges objects in a &lt;strong&gt;tree&lt;/strong&gt; and gives single objects (leaves) and groups (composites) the &lt;strong&gt;same interface&lt;/strong&gt;. Client code can then call &lt;code&gt;price()&lt;/code&gt; on a single product or on a bundle of bundles without caring which it has.&lt;/p&gt;</description></item><item><title>Decorator Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/decorator/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/decorator/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Decorator&lt;/strong&gt; pattern adds behaviour to an object by &lt;strong&gt;wrapping&lt;/strong&gt; it in another object that implements the &lt;strong&gt;same interface&lt;/strong&gt;. The wrapper does its extra work and forwards the call to the wrapped object. Wrappers can be stacked, and each adds one responsibility.&lt;/p&gt;</description></item><item><title>Facade Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/facade/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/facade/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Facade&lt;/strong&gt; pattern provides &lt;strong&gt;one simple interface&lt;/strong&gt; to a set of classes that together form a subsystem. Clients call the facade; the facade knows which subsystem objects to call and in what order.&lt;/p&gt;&#10;&lt;p&gt;GoF intent: &lt;em&gt;&amp;ldquo;Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.&amp;rdquo;&lt;/em&gt;&lt;/p&gt;</description></item><item><title>State-Dependent Actions: Balking, Guarded Suspension and Timeouts</title><link>https://samirsahoo.github.io/design-patterns-hub/java/advanced/state-dependent-actions/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/java/advanced/state-dependent-actions/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;A &lt;strong&gt;state-dependent action&lt;/strong&gt; is an operation that only makes sense when an object is in a certain state: you cannot take an item from an empty queue, withdraw from an empty account, or start an engine that is already running. In a single-threaded program, the wrong state is an error. In a concurrent program, the state might become right &lt;strong&gt;if you wait&lt;/strong&gt;, because another thread may change it.&lt;/p&gt;</description></item><item><title>Concurrency Design Patterns: Messages, Worker Threads, Futures, Cancellation and Pipelines</title><link>https://samirsahoo.github.io/design-patterns-hub/java/advanced/concurrency-design-patterns/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/java/advanced/concurrency-design-patterns/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;Once objects are safe to share (see &lt;a href="https://samirsahoo.github.io/design-patterns-hub/java/advanced/concurrent-object-design/"&gt;Designing concurrent objects&lt;/a&gt;), the next question is &lt;strong&gt;how work gets onto threads&lt;/strong&gt; and how results and failures come back. Doug Lea&amp;rsquo;s &lt;em&gt;Concurrent Programming in Java&lt;/em&gt; catalogues the recurring answers. Most now have direct support in &lt;code&gt;java.util.concurrent&lt;/code&gt;, so this page names each pattern, explains the forces behind it, and shows the modern API.&lt;/p&gt;</description></item><item><title>Flyweight Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/flyweight/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/flyweight/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Flyweight&lt;/strong&gt; pattern saves memory when a program needs a very large number of similar objects. It splits each object&amp;rsquo;s state into:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;intrinsic state&lt;/strong&gt;: shared, immutable and independent of context, stored once in a shared flyweight object&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;extrinsic state&lt;/strong&gt;: varies per object and is stored (or passed in) by the context.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;A &lt;strong&gt;factory&lt;/strong&gt; makes sure each distinct flyweight exists only once.&lt;/p&gt;</description></item><item><title>Proxy Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/proxy/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/structural/proxy/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Proxy&lt;/strong&gt; pattern puts a &lt;strong&gt;stand-in object&lt;/strong&gt; in front of a real object. The proxy implements the same interface, so clients cannot tell the difference, and it decides when and whether to forward each call: creating the real object lazily, checking permissions, caching results or calling across a network.&lt;/p&gt;</description></item><item><title>Chain of Responsibility Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/chain-of-responsibility/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/chain-of-responsibility/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Chain of Responsibility&lt;/strong&gt; pattern passes a request along a &lt;strong&gt;chain of handler objects&lt;/strong&gt;. Each handler either handles the request or passes it to the next one. The sender does not know which handler will deal with it.&lt;/p&gt;</description></item><item><title>Command Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/command/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/command/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Command&lt;/strong&gt; pattern wraps a request — &amp;ldquo;insert this text at position 5&amp;rdquo; — in an &lt;strong&gt;object&lt;/strong&gt;. Because the request is now a value, it can be stored in a history, undone, redone, queued for later, sent to another thread or logged and replayed.&lt;/p&gt;</description></item><item><title>Interpreter Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/interpreter/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/interpreter/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Interpreter&lt;/strong&gt; pattern defines a class for each rule of a small language&amp;rsquo;s &lt;strong&gt;grammar&lt;/strong&gt;. A sentence in the language is parsed into a tree of these objects (an &lt;strong&gt;abstract syntax tree&lt;/strong&gt;, AST), and calling &lt;code&gt;interpret(context)&lt;/code&gt; on the root evaluates it recursively.&lt;/p&gt;</description></item><item><title>Iterator Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/iterator/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/iterator/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Iterator&lt;/strong&gt; pattern gives a way to visit the elements of a collection &lt;strong&gt;one at a time, without exposing how the collection is stored&lt;/strong&gt;: an array, a linked list, a tree, a database cursor or pages from a remote API.&lt;/p&gt;</description></item><item><title>Mediator Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/mediator/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/mediator/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Mediator&lt;/strong&gt; pattern puts the rules for how a group of objects interact into &lt;strong&gt;one object&lt;/strong&gt;, the mediator. The objects (colleagues) report events to the mediator instead of calling each other; the mediator decides what happens next.&lt;/p&gt;</description></item><item><title>Memento Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/memento/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/memento/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Memento&lt;/strong&gt; pattern lets an object save a &lt;strong&gt;snapshot&lt;/strong&gt; of its internal state and later restore it, while keeping that state &lt;strong&gt;private&lt;/strong&gt;: other objects can store snapshots but cannot read or change what is inside them.&lt;/p&gt;</description></item><item><title>Observer Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/observer/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/observer/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Observer&lt;/strong&gt; pattern lets an object (the &lt;strong&gt;subject&lt;/strong&gt;) keep a list of interested objects (&lt;strong&gt;observers&lt;/strong&gt;) and notify all of them automatically when something happens. The subject knows only that observers implement a small interface; it does not know what they do.&lt;/p&gt;</description></item><item><title>State Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/state/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/state/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;State&lt;/strong&gt; pattern moves state-specific behaviour into separate &lt;strong&gt;state objects&lt;/strong&gt;. The main object (the &lt;strong&gt;context&lt;/strong&gt;) delegates each request to its current state object, and the state object decides what to do and which state comes next. To the outside, the object appears to change its class.&lt;/p&gt;</description></item><item><title>Strategy Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/strategy/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/strategy/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Strategy&lt;/strong&gt; pattern defines a family of algorithms, puts each one in its own class behind a common interface, and lets the client choose which one to use. The code that &lt;em&gt;uses&lt;/em&gt; the algorithm does not change when you add a new one.&lt;/p&gt;</description></item><item><title>Template Method Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/template-method/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/template-method/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Template Method&lt;/strong&gt; pattern puts the &lt;strong&gt;fixed outline of an algorithm&lt;/strong&gt; in a base-class method (the template method) and leaves certain &lt;strong&gt;steps&lt;/strong&gt; for subclasses to implement. Subclasses change the steps, never the order.&lt;/p&gt;&#10;&lt;p&gt;GoF intent: &lt;em&gt;&amp;ldquo;Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm&amp;rsquo;s structure.&amp;rdquo;&lt;/em&gt;&lt;/p&gt;</description></item><item><title>Visitor Pattern</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/visitor/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/behavioral/visitor/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;The &lt;strong&gt;Visitor&lt;/strong&gt; pattern moves an operation out of the classes it works on and into a separate &lt;strong&gt;visitor&lt;/strong&gt; object. Each class in the hierarchy has one generic &lt;code&gt;accept(visitor)&lt;/code&gt; method; adding a new operation means writing a new visitor, not editing every class.&lt;/p&gt;</description></item><item><title>Compound Patterns: How MVC Combines Observer, Strategy and Composite</title><link>https://samirsahoo.github.io/design-patterns-hub/design-patterns/compound-patterns-mvc/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://samirsahoo.github.io/design-patterns-hub/design-patterns/compound-patterns-mvc/</guid><description>&lt;h2 id="simple-definition"&gt;Simple definition&lt;a class="heading-anchor" href="#simple-definition" aria-label="Link to this section"&gt;#&lt;/a&gt;&lt;/h2&gt;&#10;&lt;p&gt;A &lt;strong&gt;compound pattern&lt;/strong&gt; is a set of patterns that work together to solve a recurring, larger problem. The best-known is &lt;strong&gt;Model-View-Controller (MVC)&lt;/strong&gt;, which &lt;em&gt;Head First Design Patterns&lt;/em&gt; calls &amp;ldquo;the king of compound patterns&amp;rdquo;. It is built from three GoF patterns:&lt;/p&gt;</description></item></channel></rss>