oliverdavies.uk/content/node.166d5baa-74b2-498c-b2cf-a15ddf4df6a0.yml

150 lines
5.1 KiB
YAML
Raw Normal View History

2025-07-10 00:14:12 +01:00
uuid:
- value: 166d5baa-74b2-498c-b2cf-a15ddf4df6a0
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T09:00:50+00:00'
revision_uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
revision_log: { }
status:
- value: true
uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
title:
- value: |
The Decorator design pattern
created:
- value: '2022-12-08T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:50+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2022/12/08/the-decorator-design-pattern
langcode: en
body:
- value: |
<p>Decorator is a structural design pattern that allows you to add extra functionality, such as if you want to add caching or logging to a service, without changing the original class.</p>
<p>As long as a class implements an Interface, it can be decorated.</p>
<p>For example, if I have this PHP interface:</p>
<pre><code class="language-php">interface DoesSomething
{
public function doSomething(): void;
}
</code></pre>
<p>I could have this class that does something:</p>
<pre><code class="language-php">final class FirstClass implements DoesSomething
{
public function doSomething(): void
{
// Does something.
}
}
</code></pre>
<p>If I need to do something else, like caching or logging the result, I can decorate it.</p>
<p>To do this, I need another class that implements the same interface and inject the original version.</p>
<pre><code class="language-php">final class SecondClass implements DoesSomething
{
public function __constuct(
private DoesSomething $originalClass
) {}
public function doSomething()
{
// Do something else before.
$this-&gt;originalClass-&gt;doSomething();
// Do something else afterwards.
}
}
</code></pre>
<p>Within the new class, the methods can be overridden, extra functionality can be added, and the original method can be run to execute the original functionality.</p>
<p>As the two classes implement the same interface, I can swap between different versions and decorate multiple times if needed.</p>
<p>This a pattern that I used recently to extend a service that retrieved some data from an API and saved it to a file, to change some arguments and do more work with it.</p>
<p>The original class was unchanged, the new class was minimal and easy to understand as it only had a single responsibility, and if I needed to switch back to the original version, I could easily do that.</p>
format: full_html
processed: |
<p>Decorator is a structural design pattern that allows you to add extra functionality, such as if you want to add caching or logging to a service, without changing the original class.</p>
<p>As long as a class implements an Interface, it can be decorated.</p>
<p>For example, if I have this PHP interface:</p>
<pre><code class="language-php">interface DoesSomething
{
public function doSomething(): void;
}
</code></pre>
<p>I could have this class that does something:</p>
<pre><code class="language-php">final class FirstClass implements DoesSomething
{
public function doSomething(): void
{
// Does something.
}
}
</code></pre>
<p>If I need to do something else, like caching or logging the result, I can decorate it.</p>
<p>To do this, I need another class that implements the same interface and inject the original version.</p>
<pre><code class="language-php">final class SecondClass implements DoesSomething
{
public function __constuct(
private DoesSomething $originalClass
) {}
public function doSomething()
{
// Do something else before.
$this-&gt;originalClass-&gt;doSomething();
// Do something else afterwards.
}
}
</code></pre>
<p>Within the new class, the methods can be overridden, extra functionality can be added, and the original method can be run to execute the original functionality.</p>
<p>As the two classes implement the same interface, I can swap between different versions and decorate multiple times if needed.</p>
<p>This a pattern that I used recently to extend a service that retrieved some data from an API and saved it to a file, to change some arguments and do more work with it.</p>
<p>The original class was unchanged, the new class was minimal and easy to understand as it only had a single responsibility, and if I needed to switch back to the original version, I could easily do that.</p>
summary: null
field_daily_email_cta: { }