」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > JavaScript 設計模式指南

JavaScript 設計模式指南

發佈於2024-08-24
瀏覽:202

Written by Hussain Arif✏️

Imagine a situation where a group of architects wants to design a skyscraper. During the design stage, they would have to consider a plethora of factors, for example:

  • The architectural style — should the building be brutalist, minimalist, or something else?
  • The width of the base — what sizing is needed to prevent collapse during windy days?
  • Protection against natural disasters — what preventative structural measures need to be in place based on the location of this building to prevent damage from earthquakes, flooding, etc.?

There can be many factors to consider, but one thing can be known for certain: there’s most likely a blueprint already available to help construct this skyscraper. Without a common design or plan, these architects would have to reinvent the wheel, which can lead to confusion and multiple inefficiencies.

Similarly in the programming world, developers often refer to a set of design patterns to help them build software while following clean code principles. Moreover, these patterns are ubiquitous, thus letting programmers focus on shipping new features instead of reinventing the wheel every time.

In this article, you will learn about a few commonly used JavaScript design patterns, and together we’ll build small Node.js projects to illustrate the usage of each design pattern.

What are design patterns in software engineering?

Design patterns are pre-made blueprints that developers can tailor to solve repetitive design problems during coding. One crucial thing to remember is that these blueprints are not code snippets but rather general concepts to approach incoming challenges.

Design patterns have many benefits:

  • Tried and tested — they solve countless problems in software design. Knowing and applying patterns in code is useful because doing so can help you solve all sorts of problems using principles of object-oriented design
  • Define a common language — design patterns help teams communicate in an efficient manner. For example, a teammate can say, “We should just use the factory method to solve this issue,” and everyone will understand what they mean and the motive behind their suggestion

In this article, we will cover three categories of design patterns:

  • Creational — Used for creating objects
  • Structural — Assembling these objects to form a working structure
  • Behavioral — Assigning responsibilities between those objects

Let’s see these design patterns in action!

Creational design patterns

As the name suggests, creational patterns comprise various methods to help developers create objects.

Factory

The factory method is a pattern for creating objects that allow more control over object creation. This method is suitable for cases where we want to keep the logic for object creation centralized in one place.

Here is some sample code that showcases this pattern in action:

//file name: factory-pattern.js
//use the factory JavaScript design pattern:
//Step 1: Create an interface for our object. In this case, we want to create a car
const createCar = ({ company, model, size }) => ({
//the properties of the car:
  company,
  model,
  size,
  //a function that prints out the car's properties:
  showDescription() {
    console.log(
      "The all new ",
      model,
      " is built by ",
      company,
      " and has an engine capacity of ",
      size,
      " CC "
    );
  },
});
//Use the 'createCar' interface to create a car
const challenger = createCar({
  company: "Dodge",
  model: "Challenger",
  size: 6162,
});
//print out this object's traits:
challenger.showDescription();

Let’s break down this code piece by piece:createCarCar

  • Each Car has three properties: company , model and size. Additionally, we have also defined a showDescription function, which will log out the properties of the object. Furthermore, notice that the createCar method demonstrates how we can have granular control when it comes to instantiating objects in memory
  • Later on, we used our createCar instance to initialize an object called challenger
  • Finally, in the last line, we invoked the showDescription on our challenger instance

Let’s test it out! We should expect the program to log out the details of our newly-created Car instance: JavaScript design patterns guide

Builder

The builder method lets us build objects using step-by-step object construction. As a result, this design pattern is great for situations where we want to create an object and only apply necessary functions. As a result, this allows for greater flexibility.

Here is a block of code that uses the builder pattern to create a Car object:

//builder-pattern.js
//Step 1: Create a class reperesentation for our toy car:
class Car {
  constructor({ model, company, size }) {
    this.model = model;
    this.company = company;
    this.size = size;
  }
}
//Use the 'builder' pattern to extend this class and add functions
//note that we have seperated these functions in their entities.
//this means that we have not defined these functions in the 'Car' definition.
Car.prototype.showDescription = function () {
  console.log(
    this.model  
      " is made by "  
      this.company  
      " and has an engine capacity of "  
      this.size  
      " CC "
  );
};
Car.prototype.reduceSize = function () {
  const size = this.size - 2; //function to reduce the engine size of the car.
  this.size = size;
};
const challenger = new Car({
  company: "Dodge",
  model: "Challenger",
  size: 6162,
});
//finally, print out the properties of the car before and after reducing the size:
challenger.showDescription();
console.log('reducing size...');
//reduce size of car twice:
challenger.reduceSize();
challenger.reduceSize();
challenger.showDescription();

Here’s what we’re doing in the code block above:

  • As a first step, we created a Car class which will help us instantiate objects. Notice that earlier in the factory pattern, we used a createCar function, but here we are using classes. This is because classes in JavaScript let developers construct objects in pieces. Or, in simpler words, to implement the JavaScript builder design pattern, we have to opt for the object-oriented paradigm
  • Afterwards, we used the prototype object to extend the Car class. Here, we created two functions — showDescription and reduceSize
  • Later on, we then created our Car instance, named it challenger, and then logged out its information
  • Finally, we invoked the reduceSize method on this object to decrement its size, and then we printed its properties once more

The expected output should be the properties of the challenger object before and after we reduced its size by four units: JavaScript design patterns guide   This confirms that our builder pattern implementation in JavaScript was successful!

Structural design patterns

Structural design patterns focus on how different components of our program work together.

Adapter

The adapter method allows objects with conflicting interfaces to work together. A great use case for this pattern is when we want to adapt old code to a new codebase without introducing breaking changes:

//adapter-pattern.js
//create an array with two fields: 
//'name' of a band and the number of 'sold' albums
const groupsWithSoldAlbums = [
  {
    name: "Twice",
    sold: 23,
  },
  { name: "Blackpink", sold: 23 },
  { name: "Aespa", sold: 40 },
  { name: "NewJeans", sold: 45 },
];
console.log("Before:");
console.log(groupsWithSoldAlbums);
//now we want to add this object to the 'groupsWithSoldAlbums' 
//problem: Our array can't accept the 'revenue' field
// we want to change this field to 'sold'
var illit = { name: "Illit", revenue: 300 };
//Solution: Create an 'adapter' to make both of these interfaces..
//..work with each other
const COST_PER_ALBUM = 30;
const convertToAlbumsSold = (group) => {
  //make a copy of the object and change its properties
  const tempGroup = { name: group.name, sold: 0 };
  tempGroup.sold = parseInt(group.revenue / COST_PER_ALBUM);
  //return this copy:
  return tempGroup;
};
//use our adapter to make a compatible copy of the 'illit' object:
illit = convertToAlbumsSold(illit);
//now that our interfaces are compatible, we can add this object to the array
groupsWithSoldAlbums.push(illit);
console.log("After:");
console.log(groupsWithSoldAlbums);

Here’s what’s happening in this snippet:

  • First, we created an array of objects called groupsWithSoldAlbums. Each object will have a name and sold property
  • We then made an illit object which had two properties — name and revenue. Here, we want to append this to the groupsWithSoldAlbums array. This might be an issue, since the array doesn’t accept a revenue property
  • To mitigate this problem, use the adapter method. The convertToAlbumsSold function will adjust the illit object so that it can be added to our array

When this code is run, we expect our illit object to be part of the groupsWithSoldAlbums list: JavaScript design patterns guide

Decorator

This design pattern lets you add new methods and properties to objects after creation. This is useful when we want to extend the capabilities of a component during runtime.

If you come from a React background, this is similar to using Higher Order Components. Here is a block of code that demonstrates the use of the JavaScript decorator design pattern:

//file name: decorator-pattern.js
//Step 1: Create an interface
class MusicArtist {
  constructor({ name, members }) {
    this.name = name;
    this.members = members;
  }
  displayMembers() {
    console.log(
      "Group name",
      this.name,
      " has",
      this.members.length,
      " members:"
    );
    this.members.map((item) => console.log(item));
  }
}
//Step 2: Create another interface that extends the functionality of MusicArtist
class PerformingArtist extends MusicArtist {
  constructor({ name, members, eventName, songName }) {
    super({ name, members });
    this.eventName = eventName;
    this.songName = songName;
  }
  perform() {
    console.log(
      this.name  
        " is now performing at "  
        this.eventName  
        " They will play their hit song "  
        this.songName
    );
  }
}
//create an instance of PerformingArtist and print out its properties:
const akmu = new PerformingArtist({
  name: "Akmu",
  members: ["Suhyun", "Chanhyuk"],
  eventName: "MNET",
  songName: "Hero",
});
akmu.displayMembers();
akmu.perform();

Let's explain what's happening here:

  • In the first step, we created a MusicArtist class which has two properties: name and members. It also has a displayMembers method, which will print out the name and the members of the current music band
  • Later on, we extended MusicArtist and created a child class called PerformingArtist. In addition to the properties of MusicArtist, the new class will have two more properties: eventName and songName. Furthermore, PerformingArtist also has a perform function, which will print out the name and the songName properties to the console
  • Afterwards, we created a PerformingArtist instance and named it akmu
  • Finally, we logged out the details of akmu and invoked the perform function

The output of the code should confirm that we successfully added new capabilities to our music band via the PerformingArtist class: JavaScript design patterns guide

Behavioral design patterns

This category focuses on how different components in a program communicate with each other.

Chain of Responsibility

The Chain of Responsibility design pattern allows for passing requests through a chain of components. When the program receives a request, components in the chain either handle it or pass it on until the program finds a suitable handler.

Here’s an illustration that explains this design pattern: JavaScript design patterns guide The bucket, or request, is passed down the chain of components until a capable component is found. When a suitable component is found, it will process the request. Source: Refactoring Guru.[/caption] The best use for this pattern is a chain of Express middleware functions, where a function would either process an incoming request or pass it to the next function via the next() method:

//Real-world situation: Event management of a concert
//implement COR JavaScript design pattern:
//Step 1: Create a class that will process a request
class Leader {
  constructor(responsibility, name) {
    this.responsibility = responsibility;
    this.name = name;
  }
  //the 'setNext' function will pass the request to the next component in the chain.
  setNext(handler) {
    this.nextHandler = handler;
    return handler;
  }
  handle(responsibility) {
  //switch to the next handler and throw an error message:
    if (this.nextHandler) {
      console.log(this.name   " cannot handle operation: "   responsibility);
      return this.nextHandler.handle(responsibility);
    }
    return false;
  }
}
//create two components to handle certain requests of a concert
//first component: Handle the lighting of the concert:
class LightsEngineerLead extends Leader {
  constructor(name) {
    super("Light management", name);
  }
  handle(responsibility) {
  //if 'LightsEngineerLead' gets the responsibility(request) to handle lights,
  //then they will handle it
    if (responsibility == "Lights") {
      console.log("The lights are now being handled by ", this.name);
      return;
    }
    //otherwise, pass it to the next component.
    return super.handle(responsibility);
  }
}

//second component: Handle the sound management of the event:
class SoundEngineerLead extends Leader {
  constructor(name) {
    super("Sound management", name);
  }
  handle(responsibility) {
  //if 'SoundEngineerLead' gets the responsibility to handle sounds,
  // they will handle it
    if (responsibility == "Sound") {
      console.log("The sound stage is now being handled by ", this.name);
      return;
    }
    //otherwise, forward this request down the chain:
    return super.handle(responsibility);
  }
}
//create two instances to handle the lighting and sounds of an event:
const minji = new LightsEngineerLead("Minji");
const danielle = new SoundEngineerLead("Danielle");
//set 'danielle' to be the next handler component in the chain.
minji.setNext(danielle);
//ask Minji to handle the Sound and Lights:
//since Minji can't handle Sound Management, 
// we expect this request to be forwarded 
minji.handle("Sound");
//Minji can handle Lights, so we expect it to be processed
minji.handle("Lights");

In the above code, we’ve modeled a situation at a music concert. Here, we want different people to handle different responsibilities. If a person cannot handle a certain task, it’s delegated to the next person in the list.

Initially, we declared a Leader base class with two properties:

  • responsibility — the kind of task the leader can handle
  • name — the name of the handler

Additionally, each Leader will have two functions:

  • setNext: As the name suggests, this function will add a Leader to the responsibility chain
  • handle: The function will check if the current Leader can process a certain responsibility; otherwise, it will forward that responsibility to the next person via the setNext method

Next, we created two child classes called LightsEngineerLead (responsible for lighting), and SoundEngineerLead (handles audio). Later on, we initialized two objects — minji and danielle. We used the setNext function to set danielle as the next handler in the responsibility chain.

Lastly, we asked minji to handle Sound and Lights.

When the code is run, we expect minji to attempt at processing our Sound and Light responsibilities. Since minji is not an audio engineer, it should hand over Sound to a capable handler. In this case, it is danielle: JavaScript design patterns guide

Strategy

The strategy method lets you define a collection of algorithms and swap between them during runtime. This pattern is useful for navigation apps. These apps can leverage this pattern to switch between routes for different user types (cycling, driving, or running):

This code block demonstrates the strategy design pattern in JavaScript code:

//situation: Build a calculator app that executes an operation between 2 numbers.
//depending on the user input, change between division and modulus operations

class CalculationStrategy {
  performExecution(a, b) {}
}
//create an algorithm for division
class DivisionStrategy extends CalculationStrategy {
  performExecution(a, b) {
    return a / b;
  }
}
//create another algorithm for performing modulus
class ModuloStrategy extends CalculationStrategy {
  performExecution(a, b) {
    return a % b;
  }
}
//this class will help the program switch between our algorithms:
class StrategyManager {
  setStrategy(strategy) {
    this.strategy = strategy;
  }
  executeStrategy(a, b) {
    return this.strategy.performExecution(a, b);
  }
}

const moduloOperation = new ModuloStrategy();
const divisionOp = new DivisionStrategy();
const strategyManager = new StrategyManager();
//use the division algorithm to divide two numbers:
strategyManager.setStrategy(divisionOp);
var result = strategyManager.executeStrategy(20, 4);
console.log("Result is: ", result);
//switch to the modulus strategy to perform modulus:
strategyManager.setStrategy(moduloOperation);
result = strategyManager.executeStrategy(20, 4);
console.log("Result of modulo is ", result);

Here’s what we did in the above block:

  • First we created a base CalculationStrategy abstract class which will process two numbers — a and b
  • We then defined two child classes — DivisionStrategy and ModuloStrategy. These two classes consist of division and modulo algorithms and return the output
  • Next, we declared a StrategyManager class which will let the program alternate between different algorithms
  • In the end, we used our DivisionStrategy and ModuloStrategy algorithms to process two numbers and return its output. To switch between these strategies, the strategyManager instance was used

When we execute this program, the expected output is strategyManager first using DivisionStrategy to divide two numbers and then switching to ModuloStrategy to return the modulo of those inputs: JavaScript design patterns guide

Conclusion

In this article, we learned about what design patterns are, and why they are useful in the software development industry. Furthermore, we also learned about different categories of JavaScript design patterns and implemented them in code.


LogRocket: Debug JavaScript errors more easily by understanding the context

Debugging code is always a tedious task. But the more you understand your errors, the easier it is to fix them.

LogRocket allows you to understand these errors in new and unique ways. Our frontend monitoring solution tracks user engagement with your JavaScript frontends to give you the ability to see exactly what the user did that led to an error.

JavaScript design patterns guide

LogRocket records console logs, page load times, stack traces, slow network requests/responses with headers bodies, browser metadata, and custom logs. Understanding the impact of your JavaScript code will never be easier!

Try it for free.

版本聲明 本文轉載於:https://dev.to/logrocket/javascript-design-patterns-guide-3p8k?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • PART# 使用 HTTP 進行大型資料集的高效能檔案傳輸系統
    PART# 使用 HTTP 進行大型資料集的高效能檔案傳輸系統
    让我们分解提供的HTML、PHP、JavaScript和CSS代码对于分块文件上传仪表板部分。 HTML 代码: 结构概述: Bootstrap for Layout:代码使用 Bootstrap 4.5.2 创建一个包含两个主要部分的响应式布局: 分块上传部分:用于...
    程式設計 發佈於2024-11-06
  • 比較:Lithe 與其他 PHP 框架
    比較:Lithe 與其他 PHP 框架
    如果您正在為下一個專案探索 PHP 框架,很自然會遇到 Laravel、Symfony 和 Slim 等選項。但是,是什麼讓 Lithe 與這些更強大、更知名的框架區分開來呢?以下是一些突出 Lithe 如何脫穎而出的注意事項。 1. 輕量級與性能 Lithe 的設計重點在於輕量級...
    程式設計 發佈於2024-11-06
  • 程式設計風格指南:編寫簡潔程式碼的實用指南
    程式設計風格指南:編寫簡潔程式碼的實用指南
    在过去的五年里,我一直在不断尝试提高我的编码技能,其中之一就是学习和遵循最推荐的编码风格。 本指南旨在帮助您编写一致且优雅的代码,并包含一些提高代码可读性和可维护性的建议。它的灵感来自于社区中最受接受的流行指南,但进行了一些修改以更适合我的喜好。 值得一提的是,我是一名全栈 JavaScript 开...
    程式設計 發佈於2024-11-06
  • 檢查類型是否滿足 Go 中的接口
    檢查類型是否滿足 Go 中的接口
    在Go中,開發人員經常使用介面來定義預期的行為,使程式碼靈活且健壯。但是如何確保類型真正實現接口,尤其是在大型程式碼庫中? Go 提供了一種簡單有效的方法來在編譯時驗證這一點,防止執行時間錯誤的風險並使您的程式碼更加可靠和可讀。 您可能看過類似的文法 var _ InterfaceName = ...
    程式設計 發佈於2024-11-06
  • 掌握 JavaScript 中的 &#this&# 關鍵字
    掌握 JavaScript 中的 &#this&# 關鍵字
    JavaScript 中的 this 關鍵字如果不理解的話可能會非常棘手。這是即使是經驗豐富的開發人員也很難輕鬆掌握的事情之一,但一旦你掌握了,它可以為你節省大量時間。 在本文中,我們將了解它是什麼、它在不同情況下如何運作以及使用它時不應陷入的常見錯誤。 在 JavaScript...
    程式設計 發佈於2024-11-06
  • PHP 中的使用者瀏覽器偵測可靠嗎?
    PHP 中的使用者瀏覽器偵測可靠嗎?
    使用 PHP 進行可靠的用戶瀏覽器檢測確定用戶的瀏覽器對於定制 Web 體驗至關重要。 PHP 提供了兩種可能的方法: $_SERVER['HTTP_USER_AGENT'] 和 get_browser() 函數。 $_SERVER['HTTP_USER_AGENT'...
    程式設計 發佈於2024-11-06
  • 增強您的 Web 動畫:像專業人士一樣最佳化 requestAnimationFrame
    增強您的 Web 動畫:像專業人士一樣最佳化 requestAnimationFrame
    流畅且高性能的动画在现代 Web 应用程序中至关重要。然而,管理不当可能会使浏览器的主线程过载,导致性能不佳和动画卡顿。 requestAnimationFrame (rAF) 是一种浏览器 API,旨在将动画与显示器的刷新率同步,从而确保与 setTimeout 等替代方案相比更流畅的运动。但有效...
    程式設計 發佈於2024-11-06
  • 為什麼MySQL伺服器在60秒內就消失了?
    為什麼MySQL伺服器在60秒內就消失了?
    MySQL 伺服器已消失- 恰好在60 秒內在此場景中,之前成功運行的MySQL 查詢現在遇到了60 秒後逾時,顯示錯誤「MySQL 伺服器已消失」。即使調整了 wait_timeout 變量,問題仍然存在。 分析:超時正好發生在 60 秒,這表明是設置而不是資源限制是原因。直接從 MySQL 客戶...
    程式設計 發佈於2024-11-06
  • 為什麼帶有“display: block”和“width: auto”的按鈕無法拉伸以填充其容器?
    為什麼帶有“display: block”和“width: auto”的按鈕無法拉伸以填充其容器?
    了解具有“display: block”和“width: auto”的按鈕的行為當您設定“display: block”時一個按鈕,它會調整其佈局以佔據可用的整個寬度。但是,如果將其與“width: auto”結合使用,則按鈕會出現意外行為,並且無法拉伸以填充其容器。此行為源自於按鈕作為替換元素的基...
    程式設計 發佈於2024-11-06
  • 為 Bluesky Social 創作機器人
    為 Bluesky Social 創作機器人
    How the bot will work We will develop a bot for the social network Bluesky, we will use Golang for this, this bot will monitor some hashtags ...
    程式設計 發佈於2024-11-06
  • 為什麼 PHP 的浮點運算會產生意外的結果?
    為什麼 PHP 的浮點運算會產生意外的結果?
    PHP 中的浮點數計算精度:為什麼它很棘手以及如何克服它在PHP 中處理浮點數時,這一點至關重要了解其固有的準確性限制。如程式片段所示:echo("success");} else {echo("error");} 您可能會驚訝地發現,儘管值之間的差異小於0....
    程式設計 發佈於2024-11-06
  • Python中可以透過變數ID逆向取得物件嗎?
    Python中可以透過變數ID逆向取得物件嗎?
    從 Python 中的變數 ID 擷取物件參考Python 中的 id() 函數傳回物件的唯一識別。人們很容易想知道是否可以反轉此過程並從其 ID 取得物件。 具體來說,我們想要檢查取消引用變數的ID 是否會擷取原始物件:dereference(id(a)) == a瞭解引用的概念及其在Python...
    程式設計 發佈於2024-11-06
  • Go 的 Defer 關鍵字如何在函數執行順序中發揮作用?
    Go 的 Defer 關鍵字如何在函數執行順序中發揮作用?
    了解 Go 的 Defer 關鍵字的功能使用 Go 時,了解 defer 關鍵字的行為至關重要。此關鍵字允許開發人員推遲函數的執行,直到周圍的函數返回。但是,需要注意的是,函數的值和參數在執行 defer 語句時進行評估。 範例:評估 Defer Order為了說明這一點,請考慮以下內容代碼:pac...
    程式設計 發佈於2024-11-06
  • WordPress Gutenberg 全域狀態管理初學者指南
    WordPress Gutenberg 全域狀態管理初學者指南
    构建复杂的 WordPress 块编辑器 (Gutenberg) 应用程序时,有效管理状态变得至关重要。这就是 @wordpress/data 发挥作用的地方。它允许您跨 WordPress 应用程序中的不同块和组件管理和共享全局状态。 如果您不熟悉管理全局状态或使用@wordpress/data,...
    程式設計 發佈於2024-11-06
  • 亞馬遜解析簡單且完全由您自己完成
    亞馬遜解析簡單且完全由您自己完成
    I came across a script on the Internet that allows you to parse product cards from Amazon. And I just needed a solution to a problem like that. I wrac...
    程式設計 發佈於2024-11-06

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3