{"id":17,"date":"2023-09-24T16:12:53","date_gmt":"2023-09-24T16:12:53","guid":{"rendered":"http:\/\/kurtgrung.com\/blog\/?p=17"},"modified":"2023-12-09T15:06:08","modified_gmt":"2023-12-09T15:06:08","slug":"design-patterns","status":"publish","type":"post","link":"https:\/\/kurtgrung.com\/blog\/design-patterns\/","title":{"rendered":"design patterns"},"content":{"rendered":"\n<p><br>Design Patterns are reusable solutions to common problems that developers encounter while designing and structuring their code. These patterns help in writing maintainable, scalable, and efficient code. Here are some popular JavaScript design patterns:<br><br>These design patterns are essential tools for any developer to improve code structure, reusability, and maintainability. The choice of pattern depends on the specific problem you are trying to solve and the design goals you want to achieve in your project.<\/p>\n\n\n\n<p><br><br><strong>Module Pattern<\/strong><\/p>\n\n\n\n<p>Encapsulates private members and provides a way to expose a public API while keeping certain parts of the code private.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Module = (function() {\n    let privateVariable = 10;\n    \n    function privateFunction() {\n        \/\/ Private logic\n    }\n    \n    return {\n        publicVariable: 20,\n        publicFunction: function() {\n            \/\/ Access privateVariable and privateFunction\n        }\n    };\n})();<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p><strong>Singleton Pattern<\/strong><\/p>\n\n\n\n<p>Ensures a class has only one instance and provides a global point of access to that instance.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Singleton = (function() {\n    let instance;\n    \n    function createInstance() {\n        \/\/ Private logic\n        return {\n            \/\/ Public API\n        };\n    }\n    \n    return {\n        getInstance: function() {\n            if (!instance) {\n                instance = createInstance();\n            }\n            return instance;\n        }\n    };\n})();<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p><strong>Factory Pattern<\/strong><\/p>\n\n\n\n<p>Creates objects without specifying the exact class of object that will be created.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Product(name, price) {\n    this.name = name;\n    this.price = price;\n}\n\nfunction createProduct(name, price) {\n    return new Product(name, price);\n}<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p><strong>Observer Pattern<\/strong><\/p>\n\n\n\n<p>Defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Subject {\n    constructor() {\n        this.observers = &#91;];\n    }\n    \n    addObserver(observer) {\n        this.observers.push(observer);\n    }\n    \n    notify(data) {\n        this.observers.forEach(observer =&gt; observer.update(data));\n    }\n}\n\nclass Observer {\n    update(data) {\n        \/\/ Handle updated data\n    }\n}<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p><strong>Decorator Pattern<\/strong><\/p>\n\n\n\n<p>Adds new functionality to an object without altering its structure.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Coffee {\n    cost() {\n        return 5;\n    }\n}\n\nfunction withMilk(coffee) {\n    const cost = coffee.cost();\n    return cost + 2;\n}<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p><strong>Prototype Pattern<\/strong><\/p>\n\n\n\n<p>Creates new objects by copying an existing object, known as the prototype.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const carPrototype = {\n    wheels: 4,\n    start() {\n        console.log('Car started.');\n    }\n};\n\nfunction createCar() {\n    const car = Object.create(carPrototype);\n    return car;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><br><strong>Command Pattern<\/strong><\/p>\n\n\n\n<p>Encapsulates a request as an object, allowing parameterization of clients with queues, requests, and operations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Command {\n    constructor(receiver) {\n        this.receiver = receiver;\n    }\n    \n    execute() {\n        this.receiver.action();\n    }\n}\n\nclass Receiver {\n    action() {\n        \/\/ Perform action\n    }\n}\n\nconst receiver = new Receiver();\nconst command = new Command(receiver);\ncommand.execute();<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Design Patterns are reusable solutions to common problems that developers encounter while designing and structuring their code. These patterns help in writing maintainable, scalable, and efficient code. Here are some popular JavaScript design patterns: These design patterns are essential tools for any developer to improve code structure, reusability, and maintainability. The choice of pattern depends [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[9,19,17,10,14,11,13,16,18,15],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-code","tag-coding","tag-command-pattern","tag-decorator-pattern","tag-design-patterns","tag-factory-pattern","tag-javascript","tag-module-pattern","tag-observer-pattern","tag-prototype-pattern","tag-singleton-pattern"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":17,"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/264"}],"wp:attachment":[{"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kurtgrung.com\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}