91 lines
No EOL
5.6 KiB
JSON
91 lines
No EOL
5.6 KiB
JSON
{
|
|
"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\n"
|
|
}
|
|
],
|
|
"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": "\n <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>\n\n<p>As long as a class implements an Interface, it can be decorated.<\/p>\n\n<p>For example, if I have this PHP interface:<\/p>\n\n<pre><code class=\"language-php\">interface DoesSomething\n{\n public function doSomething(): void;\n}\n<\/code><\/pre>\n\n<p>I could have this class that does something:<\/p>\n\n<pre><code class=\"language-php\">final class FirstClass implements DoesSomething\n{\n public function doSomething(): void\n {\n \/\/ Does something.\n }\n}\n<\/code><\/pre>\n\n<p>If I need to do something else, like caching or logging the result, I can decorate it.<\/p>\n\n<p>To do this, I need another class that implements the same interface and inject the original version.<\/p>\n\n<pre><code class=\"language-php\">final class SecondClass implements DoesSomething\n{\n public function __constuct(\n private DoesSomething $originalClass\n ) {}\n\n public function doSomething()\n {\n \/\/ Do something else before.\n\n $this->originalClass->doSomething();\n\n \/\/ Do something else afterwards.\n }\n}\n<\/code><\/pre>\n\n<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>\n\n<p>As the two classes implement the same interface, I can swap between different versions and decorate multiple times if needed.<\/p>\n\n<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>\n\n<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>\n\n ",
|
|
"format": "full_html",
|
|
"processed": "\n <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>\n\n<p>As long as a class implements an Interface, it can be decorated.<\/p>\n\n<p>For example, if I have this PHP interface:<\/p>\n\n<pre><code class=\"language-php\">interface DoesSomething\n{\n public function doSomething(): void;\n}\n<\/code><\/pre>\n\n<p>I could have this class that does something:<\/p>\n\n<pre><code class=\"language-php\">final class FirstClass implements DoesSomething\n{\n public function doSomething(): void\n {\n \/\/ Does something.\n }\n}\n<\/code><\/pre>\n\n<p>If I need to do something else, like caching or logging the result, I can decorate it.<\/p>\n\n<p>To do this, I need another class that implements the same interface and inject the original version.<\/p>\n\n<pre><code class=\"language-php\">final class SecondClass implements DoesSomething\n{\n public function __constuct(\n private DoesSomething $originalClass\n ) {}\n\n public function doSomething()\n {\n \/\/ Do something else before.\n\n $this->originalClass->doSomething();\n\n \/\/ Do something else afterwards.\n }\n}\n<\/code><\/pre>\n\n<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>\n\n<p>As the two classes implement the same interface, I can swap between different versions and decorate multiple times if needed.<\/p>\n\n<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>\n\n<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>\n\n ",
|
|
"summary": null
|
|
}
|
|
]
|
|
} |